<commit_before>/*
 * author: <NAME> <<EMAIL>>
 */

#include "AllocatedSocketAddress.hxx"
#include "address_quark.h"

#include <socket/resolver.h>

#include <glib.h>

#include <assert.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>

void
AllocatedSocketAddress::SetSize(size_t new_size)
{
    if (size == new_size)
        return;

    free(address);
    size = new_size;
    address = (struct sockaddr *)malloc(size);
}

void
AllocatedSocketAddress::SetLocal(const char *path)
{
    const size_t path_length = strlen(path);

    struct sockaddr_un *sun;
    SetSize(sizeof(*sun) - sizeof(sun->sun_path) + path_length);
    sun = (struct sockaddr_un *)address;
    sun->sun_family = AF_UNIX;
    memcpy(sun->sun_path, path, path_length + 1);
}

bool
AllocatedSocketAddress::Parse(const char *p, int default_port,
                              bool passive, GError **error_r)
{
    if (*p == '/') {
        SetLocal(p);
        return true;
    }

    if (*p == '@') {
#ifdef __linux
        /* abstract unix domain socket */

        SetLocal(p);

        /* replace the '@' with a null byte to make it "abstract" */
        struct sockaddr_un *sun = (struct sockaddr_un *)address;
        assert(sun->sun_path[0] == '@');
        sun->sun_path[0] = '\0';
        return true;
#else
        /* Linux specific feature */
        g_set_error_literal(error_r, resolver_quark(), 0,
                            "Abstract sockets supported only on Linux");
        return false;<commit_msg>net/AllocatedSocketAddress: fix off-by-one bug in SetLocal()<commit_after>/*
 * author: <NAME> <<EMAIL>>
 */

#include "AllocatedSocketAddress.hxx"
#include "address_quark.h"

#include <socket/resolver.h>

#include <glib.h>

#include <assert.h>
#include <sys/un.h>
#include <netdb.h>
#include <string.h>

void
AllocatedSocketAddress::SetSize(size_t new_size)
{
    if (size == new_size)
        return;

    free(address);
    size = new_size;
    address = (struct sockaddr *)malloc(size);
}

void
AllocatedSocketAddress::SetLocal(const char *path)
{
    const size_t path_length = strlen(path);

    struct sockaddr_un *sun;
    SetSize(sizeof(*sun) - sizeof(sun->sun_path) + path_length + 1);
    sun = (struct sockaddr_un *)address;
    sun->sun_family = AF_UNIX;
    memcpy(sun->sun_path, path, path_length + 1);
}

bool
AllocatedSocketAddress::Parse(const char *p, int default_port,
                              bool passive, GError **error_r)
{
    if (*p == '/') {
        SetLocal(p);
        return true;
    }

    if (*p == '@') {
#ifdef __linux
        /* abstract unix domain socket */

        SetLocal(p);

        /* replace the '@' with a null byte to make it "abstract" */
        struct sockaddr_un *sun = (struct sockaddr_un *)address;
        assert(sun->sun_path[0] == '@');
        sun->sun_path[0] = '\0';
        return true;
#else
        /* Linux specific feature */
        g_set_error_literal(error_r, resolver_quark(), 0,
                            "Abstract sockets supported only on Linux");
        return false;
{'fraction_non_alphanumeric': 0.13957307060755336, 'fraction_numerical': 0.00361247947454844, 'mean_word_length': 2.710109622411693, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3379384', 'n_tokens_mistral': 1015, 'n_tokens_neox': 992, 'n_words': 241}