https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81235
Bug ID: 81235 Summary: Realloc returns NULL while ti should not Product: gcc Version: 5.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: carljohnson95 at gmx dot com Target Milestone: --- Created attachment 41638 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41638&action=edit This attachment contains source file which causes the issue and log file which contains alloc/realloc data. system type: Windows 8.1 complete command line: >gcc 1.c -Wall -Wextra -std=c99 -Wmissing-prototypes -Wstrict-prototypes >-Wold-style-definition -fno-strict-aliasing -fwrapv -o 1.exe the compiler output: / The source file which causes the issue is named "1.c" (in attachment). I am not allowed to share whole code, so I renamed every single identifier and removed extra space. It may be hard to read, but I don't have other way to share it. The script is compiled correctly (I mean it doesn't throw any errors or warnings). However, when executed, the process crashes. I spent a lot of time trying to figure out what is actually causing the crash. After debugging, I noticed that the crash is because function `realloc` returned NULL. As I know, there might be two situations when `realloc` returns NULL: 1. When a pointer is passed which didn't came from `alloc` or `calloc` 2. When there is no enough free space The second possibility is not the case, because I ensured that there is enough space. Also, I debugged it and noticed that `realloc` returns NULL when requested size is 36 bytes. So, the only left possibility is that I mistakenly passed bad pointer to `realloc`. So, I wrote a function for debugging to inspect what pointers are allocated/reallocated (you can see it in file "2.c" from attachment). I analyzed the output log file and I didn't notice anything suspicious. Every single time I call `realloc` I pass correct pointer. But, for some weird reason when `36` bytes are reached, the NULL is received. I have a lot of experience with mistakes with memory management, so I know how to deal with that. However, this is not the case. I strongly believe this is a bug with GCC. It usually takes me a few minutes to figure out where I made a mistake, but this code took me a week and I still think I did everything right and GCC messed something up. The last thing I did is to compile it using VisualStudio. As I though, the program works correctly and doesn't throw any error, wanrning or segfault. In the file "log.txt" from the attachment you can see every single call of alloc/realloc functions and its parameters and output. Maybe it helps. Also, the crash doesn't happen every time. Approximatelly 50% of times I run the same program it crashes.