<commit_before>#include "bson.h"
#include "__bson.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

off_t fsize(const char* path)
{
    struct stat st;

    if (stat(path, &st))
        return -1;

    return st.st_size;
}

int bson_make_readable(struct bson_info* bson_info)
{
    if (bson_info == NULL || bson_info->buffer == NULL || bson_info->size < 5)
        return EXIT_FAILURE;
    bson_info->size = *((int32_t*) bson_info->buffer) - 4;
    bson_info->position = 4;
    return EXIT_SUCCESS;
}

int bson_read(struct bson_info* bson_info, const char* fname)
{
    if (bson_info == NULL)
        return EXIT_FAILURE;

    off_t size = fsize(fname);
    FILE* file = fopen(fname, "r");

    if (file == NULL)
        return EXIT_FAILURE;

    bson_info->buffer = malloc(size);
    if (bson_info->buffer)
    {
        if(fread(bson_info->buffer, 1, size, file) != size)
        {
            fclose(file);
            return EXIT_FAILURE;<commit_msg>bson: when reading free old buffer (old bug fix)<commit_after>#include "bson.h"
#include "__bson.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

off_t fsize(const char* path)
{
    struct stat st;

    if (stat(path, &st))
        return -1;

    return st.st_size;
}

int bson_make_readable(struct bson_info* bson_info)
{
    if (bson_info == NULL || bson_info->buffer == NULL || bson_info->size < 5)
        return EXIT_FAILURE;
    bson_info->size = *((int32_t*) bson_info->buffer) - 4;
    bson_info->position = 4;
    return EXIT_SUCCESS;
}

int bson_read(struct bson_info* bson_info, const char* fname)
{
    if (bson_info == NULL)
        return EXIT_FAILURE;

    off_t size = fsize(fname);
    FILE* file = fopen(fname, "r");

    if (file == NULL)
        return EXIT_FAILURE;

    if (bson_info->buffer != NULL)
        free(bson_info->buffer);

    bson_info->buffer = malloc(size);
    if (bson_info->buffer)
    {
        if(fread(bson_info->buffer, 1, size, file) != size)
        {
            fclose(file);
            return EXIT_FAILURE;
{'fraction_non_alphanumeric': 0.13902205177372962, 'fraction_numerical': 0.006711409395973154, 'mean_word_length': 2.8293577981651374, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, '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': '4590806', 'n_tokens_mistral': 812, 'n_tokens_neox': 792, 'n_words': 177}