I want to process the contents of a configuration file. The configuration file can be of any size. I get a bus error message after a program freezes when I run the following code:
FILE *fp;
struct stat st;
char *buffer;
fp = fopen(CONFIG_FILE, "r");
if (fp == NULL) {
}
fstat(fileno(fp), &st);
fread(buffer, sizeof(char), st.st_size, fp);
fprintf(stderr, "%s\n", *buffer);
fclose(fp);
I read that a bus error could be caused by a buffer overflow. I am sure I get buffer overflow with char *buffer. But then, how can I specify the size of the buffer at runtime?
EDIT . A bus error was caused by my laziness of hardcoding 1in passing. Sample code has been updated to fix this using sizeof(char).
source
share