Invalid read of size 1 in strstr() using valgrind

c, strstr, valgrind

Solution

Your `buffer` is not nul-terminated. Usually, you can just `bzero()` it before the `fread()` to ensure that it will work with `strstr()` and similar functions.

Problem

I'm inspecting some piece of code with valgrind and I get this error: ``` ==7001== Invalid read of size 1 ==7001== at 0x402E21B: strstr (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==7001== by 0x8049742: replace_in_file (functions.c:191) ==7001== by 0x8049C55: parse_dir (functions.c:295) ==7001== by 0x8049059: main (main.c:214) ==7001== Address 0x42018c3 is 0 bytes after a block of size 3 alloc'd ==7001== at 0x402B018: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==7001== by 0x80496EC: replace_in_file (functions.c:183) ==7001== by 0x8049C55: parse_dir (functions.c:295) ==7001== by 0x8049059: main (main.c:214) ``` The code snippet looks as follows: ``` long int replace_in_file(char **sets, FILE *file, char *path, const char *mode){ long int n = 0, size = 0, len, remaining_len; char *buffer, *found, *before, *after; size = file_size(file); if(-1 != size){ buffer = malloc(size * sizeof(char)); rewind(file); fread(buffer, 1, size, file); int i=0; do{ do{ found = strstr(buffer, sets[i]); // LINE 191 if(NULL != found && '\0' != sets[i][0]){ //rest of code... ``` I cannot realise why I get that error, because everything works as expected and in the debugger every variable seems fine. What is wrong, how can I fix it?

Original source