C – Error is “free(): invalid next size (normal) “

When you encounter errors, start with the 1. one , it might be the cause of the following errors.

==17917== Invalid write of size 8
==17917==    at 0x5E53E04: __GI_mempcpy (in /usr/lib/libc-2.21.so)
==17917==    by 0x5E44BDD: _IO_default_xsputn (in /usr/lib/libc-2.21.so)
==17917==    by 0x5E18C61: vfprintf (in /usr/lib/libc-2.21.so)
==17917==    by 0x5E3AC2A: vsprintf (in /usr/lib/libc-2.21.so)
==17917==    by 0x5E1F266: sprintf (in /usr/lib/libc-2.21.so)
==17917==    by 0x401647: startup (server_func.c:32)
==17917==    by 0x4012BB: main (server.c:23)
==17917==  Address 0x6c8709e is 14 bytes inside a block of size 20 alloc'd
==17917==    at 0x4C2C29E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17917==    by 0x40160B: startup (server_func.c:28)
==17917==    by 0x4012BB: main (server.c:23)

In this stack trace, the interresting pieces is the code of your program, as we can assume the code in the runtime/standard library to work.

Valgrind tells you 2 things here:

  1. You’re accessing memory you’re not supposed to (Invalid write of size 8), in the startup function in the file server_func.c at line 32
  2. The memory you’re accessing is 14 bytes past a buffer of 20 bytes that you allocated at server_func.c line 28

Line 28: path = (unsigned char *) realloc(path, size_of_path + sizeof(STARTUP_FILE));

Line 32: sprintf(path, "%s/%s", path_to_file, STARTUP_FILE);

So on line 28 you don’t allocate room for the “/” separator, and possibly not the nul terminator.

You also need to ensure size_of_path is correct, and that sizeof(STARTUP_FILE) gives you the correct size, which it will if STARTUP_FILE is a char array, but it’ll be wrong if it’s a char pointer. Perhaps you need to use strlen(STARTUP_FILE)

Assuming size_of_path is correct, you might need to calculate the buffer size as size_of_path + strlen(STARTUP_FILE) + 2

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)