<commit_before>/*
 * File-Descriptor List
 */

#include <c-macro.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "fdlist.h"
#include "error.h"

int fdlist_new_with_fds(FDList **listp, const int *fds, size_t n_fds) {
        FDList *list;

        list = malloc(sizeof(*list) + CMSG_SPACE(n_fds * sizeof(int)));
        if (!list)
                return error_origin(-ENOMEM);

        list->consumed = false;
        list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
        list->cmsg->cmsg_level = SOL_SOCKET;
        list->cmsg->cmsg_type = SCM_RIGHTS;
        memcpy(fdlist_data(list), fds, n_fds * sizeof(int));

        *listp = list;
        return 0;
}

int fdlist_new_consume_fds(FDList **listp, const int *fds, size_t n_fds) {
        int r;

        r = fdlist_new_with_fds(listp, fds, n_fds);
        if (!r)
                (*listp)->consumed = true;

        return r;
}

FDList *fdlist_free(FDList *list) {
        size_t i, n;
        int *p;

        if (list) {
                p = fdlist_data(list);
                n = fdlist_count(list);

                if (list->consumed)
                        for (i = 0; i < n; ++i)
                                close(p[i]);

                free(list);
        }

        return NULL;
}

void fdlist_truncate(FDList *list, size_t n_fds) {
        size_t i, n;
        int *p;

        p = fdlist_data(list);
        n = fdlist_count(list);

        assert(n_fds <= n);

        if (list->consumed)
                for (i = n_fds; i < n; ++i)
                        close(p[i]);

        list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
}
<commit_msg>fdlist: don't try to close stolen fds<commit_after>/*
 * File-Descriptor List
 */

#include <c-macro.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "fdlist.h"
#include "error.h"

int fdlist_new_with_fds(FDList **listp, const int *fds, size_t n_fds) {
        FDList *list;

        list = malloc(sizeof(*list) + CMSG_SPACE(n_fds * sizeof(int)));
        if (!list)
                return error_origin(-ENOMEM);

        list->consumed = false;
        list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
        list->cmsg->cmsg_level = SOL_SOCKET;
        list->cmsg->cmsg_type = SCM_RIGHTS;
        memcpy(fdlist_data(list), fds, n_fds * sizeof(int));

        *listp = list;
        return 0;
}

int fdlist_new_consume_fds(FDList **listp, const int *fds, size_t n_fds) {
        int r;

        r = fdlist_new_with_fds(listp, fds, n_fds);
        if (!r)
                (*listp)->consumed = true;

        return r;
}

FDList *fdlist_free(FDList *list) {
        size_t i, n;
        int *p;

        if (list) {
                p = fdlist_data(list);
                n = fdlist_count(list);

                if (list->consumed)
                        for (i = 0; i < n; ++i)
                                c_close(p[i]);

                free(list);
        }

        return NULL;
}

void fdlist_truncate(FDList *list, size_t n_fds) {
        size_t i, n;
        int *p;

        p = fdlist_data(list);
        n = fdlist_count(list);

        assert(n_fds <= n);

        if (list->consumed)
                for (i = n_fds; i < n; ++i)
                        close(p[i]);

        list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
}

{'fraction_non_alphanumeric': 0.13669501822600244, 'fraction_numerical': 0.001215066828675577, 'mean_word_length': 1.7282518641259321, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7512289', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1186, 'n_words': 250}