<commit_before>/**
 * @file memory.c
 * @author <NAME>, <NAME>
 * @date July 27, 2013
 */

#include "bina.h"

memory_t*
memory_create(const char* filepath)
{
    memory_t* memory = NULL;
    FILE* fd = NULL;

    LOGD("Loading: %s into memory", filepath);

    memory = (memory_t*) calloc(1, sizeof(memory_t));
    if (!memory) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto error;
    }

    /* Copying filepath */
    strncpy(memory->filepath, filepath, MAX_PATH);

#ifdef ANDROID
    AAsset* asset = NULL;
    /* char buf[BUFSIZ]; */
    /* int nb_read = 0; */
    off_t asset_length = 0;
    const void* buffer = NULL;

    if (!asset_manager_g) {
        LOGE("Cannot load: %s, AssetManager is not loaded", filepath);
        goto android_error;
    }

    asset = AAssetManager_open(asset_manager_g, filepath,
                               AASSET_MODE_BUFFER);
    if (!asset) {
        LOGE("Cannot load: %s, Could not found asset", filepath); 
        goto android_error;
    }

    /* Getting the pointer of the asset */
    buffer = AAsset_getBuffer(asset);

    /* Getting buffer size */
    memory->size = AAsset_getLength(asset); 

    /* Allocating enough memory for buffer */
    memory->buffer = (unsigned char *) calloc(1, memory->size + 1);
    if (!memory->buffer) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto android_error;
    }

    /* Copying buffer into memory object buffer */
    memcpy(memory->buffer, buffer, memory->size);
    memory->buffer[memory->size] = 0;

    AAsset_close(asset);

    return memory;

android_error:
    if (asset) {
        AAsset_close(asset);
    }

    goto error;
#else
    if ((fd = fopen(filepath, "rb")) == NULL) {
        LOGE("Unable to open: %s for reading", filepath);
        goto else_error;
    }

    /* Getting buffer size */
    fseek(fd, 0, SEEK_END);
    memory->size = ftell(fd);
    fseek(fd, 0, SEEK_SET);

    /* Allocating enough memory for buffer */
    memory->buffer = (unsigned char *) calloc(1, memory->size + 1);
    if (!memory->buffer) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto else_error;
    }

    /* Reading buffer */
    fread(memory->buffer, memory->size, 1, fd);
    memory->buffer[memory->size] = 0;

    fclose(fd);
    return memory;

else_error:
    if (fd) {
        fclose(fd);
    }

    goto error;
#endif

error:
    memory_delete(&memory);

    return NULL;
}

void
memory_delete(memory_t** memory)
{
    memory_t* tmp = *memory;
    free(tmp->buffer);
    free(tmp);
    *memory = NULL;
}


unsigned int
memory_read(memory_t* memory, void* dest, unsigned int size)
{
	if ((memory->position + size) > memory->size) {
        size = memory->size - memory->position;
    }

	memcpy(dest, &memory->buffer[memory->position], size);

	memory->position += size;

	return size;
}
<commit_msg>memory: fix potentially bad free call<commit_after>/**
 * @file memory.c
 * @author <NAME>, <NAME>
 * @date July 27, 2013
 */

#include "bina.h"

memory_t*
memory_create(const char* filepath)
{
    memory_t* memory = NULL;
    FILE* fd = NULL;

    LOGD("Loading: %s into memory", filepath);

    memory = (memory_t*) calloc(1, sizeof(memory_t));
    if (!memory) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto error;
    }

    /* Copying filepath */
    strncpy(memory->filepath, filepath, MAX_PATH);

#ifdef ANDROID
    AAsset* asset = NULL;
    /* char buf[BUFSIZ]; */
    /* int nb_read = 0; */
    off_t asset_length = 0;
    const void* buffer = NULL;

    if (!asset_manager_g) {
        LOGE("Cannot load: %s, AssetManager is not loaded", filepath);
        goto android_error;
    }

    asset = AAssetManager_open(asset_manager_g, filepath,
                               AASSET_MODE_BUFFER);
    if (!asset) {
        LOGE("Cannot load: %s, Could not found asset", filepath); 
        goto android_error;
    }

    /* Getting the pointer of the asset */
    buffer = AAsset_getBuffer(asset);

    /* Getting buffer size */
    memory->size = AAsset_getLength(asset); 

    /* Allocating enough memory for buffer */
    memory->buffer = (unsigned char *) calloc(1, memory->size + 1);
    if (!memory->buffer) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto android_error;
    }

    /* Copying buffer into memory object buffer */
    memcpy(memory->buffer, buffer, memory->size);
    memory->buffer[memory->size] = 0;

    AAsset_close(asset);

    return memory;

android_error:
    if (asset) {
        AAsset_close(asset);
    }

    goto error;
#else
    if ((fd = fopen(filepath, "rb")) == NULL) {
        LOGE("Unable to open: %s for reading", filepath);
        goto else_error;
    }

    /* Getting buffer size */
    fseek(fd, 0, SEEK_END);
    memory->size = ftell(fd);
    fseek(fd, 0, SEEK_SET);

    /* Allocating enough memory for buffer */
    memory->buffer = (unsigned char *) calloc(1, memory->size + 1);
    if (!memory->buffer) {
        LOGE(BINA_NOT_ENOUGH_MEMORY);
        goto else_error;
    }

    /* Reading buffer */
    fread(memory->buffer, memory->size, 1, fd);
    memory->buffer[memory->size] = 0;

    fclose(fd);
    return memory;

else_error:
    if (fd) {
        fclose(fd);
    }

    goto error;
#endif

error:
    memory_delete(&memory);

    return NULL;
}

void
memory_delete(memory_t** memory)
{
    memory_t* tmp = *memory;

    if (tmp) {
        if (tmp->buffer) {
            free(tmp->buffer);
        }
        free(tmp);
        tmp = NULL;
    }

    *memory = tmp;
}


unsigned int
memory_read(memory_t* memory, void* dest, unsigned int size)
{
	if ((memory->position + size) > memory->size) {
        size = memory->size - memory->position;
    }

	memcpy(dest, &memory->buffer[memory->position], size);

	memory->position += size;

	return size;
}

{'fraction_non_alphanumeric': 0.1391743598676189, 'fraction_numerical': 0.006270684549730012, 'mean_word_length': 2.514075887392901, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 60, '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': '6089192', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1904, 'n_words': 475}