0

So I've combed through this thing pretty thoroughly using printf statements (on mobile) and have narrowed the location of the error down to the nested if (ret == EOF) statement in the read_file function. The data is successfully copied but segfaults on exit of the function. Any chance someone might be able to take a look for me?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BYTES 1024

typedef int8_t byte;

typedef struct file_buffer {
    
    char * file_name;
    void * raw_data;
    
    uint64_t file_size;
    uint64_t max_size;
    uint64_t temps_copied;
    uint16_t bytes_in_temp;
    uint8_t filled;
    
    FILE * fp;
    
} FB;



void * open_file(const char * file_name);

static FB * new_buffer(const char * file_name);

static void read_file(FB * fb, FILE * fp);

static void copy_buffer(FB * fb, byte * temp, const int size);

static void check_size(FB * fb, const int size);



int main()
{
    open_file("./src/MOCK_DATA.csv");
    return 0;
}


void * open_file(const char * file_name)
{
    FILE * fp;
    fp = fopen(file_name, "rb");
    //
    
    FB * buffer = new_buffer(file_name);
    
    read_file(buffer, fp);
    
    return buffer->raw_data;
}




static FB * new_buffer(const char * file_name)
{
    FB * fb = calloc(1, sizeof(FB));
    //
    
    fb->raw_data = calloc(MAX_BYTES, sizeof(byte));
    //
    
    size_t name_size = strlen(file_name) + 1;
    fb->file_name = malloc(sizeof(char) * name_size);
    //
    memcpy(fb->file_name, file_name,  name_size);
    
    return fb;
}



static void read_file(FB * fb, FILE * fp)
{
    byte temp[MAX_BYTES];
    byte nb = 0;
   
    int i = 0;
    int j = 0;
    int ret;
 
    while (ret = (nb = (fgetc(fp))))
    {
     if ((ret == EOF) || (j == (MAX_BYTES - 1)))
        {
            if (ret == EOF) {
                copy_buffer(fb, &temp[0], i);                
                return;
        }
        
        else {
            copy_buffer(fb, &temp[0], i);
            
            fb->bytes_in_temp = 0;
            j = 0;
            i = i + 1;
        }
        }
        fb->bytes_in_temp);
        
        temp[j] = nb;
        
        fb->bytes_in_temp++;
        j = j + 1;
    }
}



static void copy_buffer(FB * fb, byte * temp, const int size)
{
    check_size(fb, size);
    
    int start = MAX_BYTES * size;
    
    memcpy(&fb->raw_data[start], temp, fb->bytes_in_temp);
}



static void check_size(FB * fb, const int size)
{
    if (fb->temps_copied < size)
    {
        void * temp = realloc(fb->raw_data, (MAX_BYTES + fb->max_size) * sizeof(byte));
        
        fb->raw_data = temp;        
        fb->temps_copied = size;
        fb->max_size += MAX_BYTES;
    }
}
7
  • Missing #include <stdint.h>, implement error check fopen(), fb->bytes_in_temp) is a syntax error (and does nothing if the ) is removed), ` memcpy(&fb->raw_data[start], temp, fb->bytes_in_temp);` dereferences a void pointer. You rely on a particular file we don't have so can't test this for you. Run it in a debugger, if that doesn't enlight valgrind to find heap corruption. Commented Jul 2, 2022 at 1:49
  • Why do you open a .csv file in "rb" opposed to "r" mode? Commented Jul 2, 2022 at 1:54
  • I can't reproduce the issue (after adding a file and/or implementing fopen error checking). Might depend on size of .csv. Commented Jul 2, 2022 at 2:00
  • open_file()a return a void *raw_data but caller cannot tell how much data is being returned. Commented Jul 2, 2022 at 2:01
  • Ahhh, so if I just malloc a bunch of space and return it to the calling function it has no idea how large the space it points to is? Commented Jul 2, 2022 at 2:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.