Author Topic: Malloc() / Heap issue with debug build target  (Read 1065 times)

Offline Marnix

  • Multiple posting newcomer
  • *
  • Posts: 19
Malloc() / Heap issue with debug build target
« on: April 22, 2019, 12:30:08 pm »
I run Code::Blocks 17.12 on Windows 10

I have different behavior with the debug and release build targets in my CB. Because the program runs ok outside CB (release and debug builds), I want to rule out a CB setting issue.

I have a routine that reads a string from a file:

Code: [Select]
char *ReadString(void)
{
  int32_t len;   /* length of the string */
  char    *str = NULL;

  /* First, read the length of the string. */
  if (!GetNextCode32(&len)) {
    PrintError(14, NULL, "ReadString()");
    return(NULL);
  }

  /* create space on heap */
  if ((str = (char *) malloc(len*sizeof(char))) == NULL) {
    PrintError(15, NULL, "ReadString()");
    return(NULL);
  }

  /* read the string */
  if (fread((void *) str, sizeof(char), len, datafile) != len) {
    PrintError(15, NULL, "ReadString()");
    return(NULL);
  }

  /* all went well */
  /* don't forget to free(str) in calling function */
  return(str);
}

The program is a console application (.exe)

When I build the program without debug info it runs fine inside and outside CB.

When I build it with the Debug build target in CB:
- the program runs fine outside CB
- the program runs fine inside CB when I start it with Run (ctrl-F10, the green triangle)
- the program crashes on a malloc() when I start it with Debug/continue (ctrl F8, red triangle)

This is the stack trace:

Code: [Select]
#0 0x772b8a76 ntdll!RtlRunOnceBeginInitialize() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#1 0x772a7799 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#2 0x772a5ec9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#3 0x772a5d3e ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#4 0x7734250d ntdll!RtlpNtSetValueKey() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#5 0x772a6dd9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#6 0x772a5ec9 ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#7 0x772a5d3e ntdll!RtlAllocateHeap() (C:\WINDOWS\SYSTEM32\ntdll.dll:??)
#8 0x770772a0 msvcrt!malloc() (C:\WINDOWS\System32\msvcrt.dll:??)
#9 0x409904 ReadString() (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c:117)
#10 0x40aa4a ReadFlags(offset=706786) (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c:553)
#11 0x406f45 InitDirs() (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\dirs.c:92)
#12 0x40d540 main(argc=1, argv=0xbe0dd8) (D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\init.c:286)

This is part of the output from the Dr Memory tool for the debug build target:

Code: [Select]
Error #1: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x012f3a28-0x012f3a2c 4 byte(s)
# 0 ReadFlags               [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/fileio.c:553]
# 1 InitDirs                [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/dirs.c:92]
# 2 main                    [D:/Marnix/Codeblocks/XVAN 2.4/01 - Interpreter/init.c:286]
Note: @0:00:06.109 in thread 11816
Note: next higher malloc: 0x012f3a50-0x012f3a54
Note: refers to 0 byte(s) beyond last valid byte in prior malloc
Note: prev lower malloc:  0x012f3a28-0x012f3a28
Note: instruction: mov    %eax -> (%ebx)

I hope someone can help me out with this.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6610
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Marnix

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Malloc() / Heap issue with debug build target
« Reply #2 on: April 22, 2019, 05:41:08 pm »

I did that before I started the thread (I read the read-this-before-you-post message). Nothing strange, only 3 warnings for using int64_t for a long int.


Code: [Select]
-------------- Clean: Debug in Interpreter (compiler: GNU GCC Compiler)---------------

Cleaned "Interpreter - Debug"

-------------- Build: Debug in Interpreter (compiler: GNU GCC Compiler)---------------

mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\arithmtc.c" -o obj\Debug\arithmtc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\checkpar.c" -o obj\Debug\checkpar.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\cleanup.c" -o obj\Debug\cleanup.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c" -o obj\Debug\debug.o
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintLocationDirectory':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:135:28: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'int64_t {aka long long int}' [-Wformat=]
     sprintf(text_to_print, "\nOffset: %ld\n\n", loc_dir[i].offset);
                            ^
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintObjectDirectory':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:181:28: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'int64_t {aka long long int}' [-Wformat=]
     sprintf(text_to_print, "\nOffset: %ld\n\n", obj_dir[i].offset);
                            ^
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c: In function 'PrintVerbDir':
D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\debug.c:598:12: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int64_t {aka long long int}' [-Wformat=]
     printf("offset: %ld\n", verb_dir[i].offset);
            ^
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\dirs.c" -o obj\Debug\dirs.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\disambig.c" -o obj\Debug\disambig.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\endian.c" -o obj\Debug\endian.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\errors.c" -o obj\Debug\errors.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\execute.c" -o obj\Debug\execute.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\fileio.c" -o obj\Debug\fileio.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\init.c" -o obj\Debug\init.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\input.c" -o obj\Debug\input.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\json.c" -o obj\Debug\json.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-article.c" -o obj\Debug\Languages\ENG\ENG-article.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-checksyntax.c" -o obj\Debug\Languages\ENG\ENG-checksyntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-moreinfo.c" -o obj\Debug\Languages\ENG\ENG-moreinfo.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\ENG\ENG-xeqfun.c" -o obj\Debug\Languages\ENG\ENG-xeqfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-article.c" -o obj\Debug\Languages\NL\NL-article.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-checksyntax.c" -o obj\Debug\Languages\NL\NL-checksyntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-moreinfo.c" -o obj\Debug\Languages\NL\NL-moreinfo.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\Languages\NL\NL-xeqfun.c" -o obj\Debug\Languages\NL\NL-xeqfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\lets-try.c" -o obj\Debug\lets-try.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\letsplay.c" -o obj\Debug\letsplay.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\output.c" -o obj\Debug\output.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readfun.c" -o obj\Debug\readfun.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readloc.c" -o obj\Debug\readloc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readobj.c" -o obj\Debug\readobj.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readtrig.c" -o obj\Debug\readtrig.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\readverb.c" -o obj\Debug\readverb.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\resglobs.c" -o obj\Debug\resglobs.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\restart.c" -o obj\Debug\restart.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\restore.c" -o obj\Debug\restore.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\save.c" -o obj\Debug\save.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\spanTree.c" -o obj\Debug\spanTree.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\syntax.c" -o obj\Debug\syntax.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\sysfunc.c" -o obj\Debug\sysfunc.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\timers.c" -o obj\Debug\timers.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\trnslate.c" -o obj\Debug\trnslate.o
mingw32-gcc.exe -Wall -g -ILanguages\ENG -ILanguages\NL -I"..\01 - Interpreter" -c "D:\Marnix\Codeblocks\XVAN 2.4\01 - Interpreter\xeqfun.c" -o obj\Debug\xeqfun.o
mingw32-g++.exe  -o bin\Debug\Interpreter.exe obj\Debug\arithmtc.o obj\Debug\checkpar.o obj\Debug\cleanup.o obj\Debug\debug.o obj\Debug\dirs.o obj\Debug\disambig.o obj\Debug\endian.o obj\Debug\errors.o obj\Debug\execute.o obj\Debug\fileio.o obj\Debug\init.o obj\Debug\input.o obj\Debug\json.o obj\Debug\Languages\ENG\ENG-article.o obj\Debug\Languages\ENG\ENG-checksyntax.o obj\Debug\Languages\ENG\ENG-moreinfo.o obj\Debug\Languages\ENG\ENG-xeqfun.o obj\Debug\Languages\NL\NL-article.o obj\Debug\Languages\NL\NL-checksyntax.o obj\Debug\Languages\NL\NL-moreinfo.o obj\Debug\Languages\NL\NL-xeqfun.o obj\Debug\lets-try.o obj\Debug\letsplay.o obj\Debug\output.o obj\Debug\readfun.o obj\Debug\readloc.o obj\Debug\readobj.o obj\Debug\readtrig.o obj\Debug\readverb.o obj\Debug\resglobs.o obj\Debug\restart.o obj\Debug\restore.o obj\Debug\save.o obj\Debug\spanTree.o obj\Debug\syntax.o obj\Debug\sysfunc.o obj\Debug\timers.o obj\Debug\trnslate.o obj\Debug\xeqfun.o   
Output file is bin\Debug\Interpreter.exe with size 403.39 KB
Process terminated with status 0 (0 minute(s), 5 second(s))
0 error(s), 3 warning(s) (0 minute(s), 5 second(s))
 

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6610
    • My Best Post
Re: Malloc() / Heap issue with debug build target
« Reply #3 on: April 22, 2019, 05:57:28 pm »
You are going to have to wait for an CB Debugging person. They will likely ask for a full debugging log.
I have no idea how to turn on the debugging log or how to find the log. I do not use the CB Debugger very much.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2457
Re: Malloc() / Heap issue with debug build target
« Reply #4 on: April 22, 2019, 10:14:33 pm »
Have you enabled watch local variables, or watch function arguments?
What is the exact crash reason? SIGSEG or an other sig?
Have you set a breakpoint?
Does it reach this breakpoint?
Have you tried to set a breakpoint at the beginning of main, and step until your application crashes?

I suspect you watch a variable that is not initialized and as soon as the debugger tries to access it the application crashes....

Offline Marnix

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Malloc() / Heap issue with debug build target
« Reply #5 on: April 22, 2019, 10:44:30 pm »
Watch variables was enabled. I turned it off, but without success.

The crash reason is a SIGSEGV.

I F7-ed through the code and I narrowed it down to this for loop:

Code: [Select]
    for (i=0; i<nr_of_lflags; i++) {
      if ( ((loc_flag_dbug[i]).name = ReadString()) == NULL)
        return(ERROR);

      if (!GetNextCode32(&(loc_flag_dbug[i]).owner))
        return(ERROR);
    }

The ReadString() function (listed in the opening post) has a malloc() that crashes.

loc_flag_dbug is an array of structs wirh a char* and an int32_t, this array is malloced as well with length nr_of_lflags:


Code: [Select]
    /* Malloc() space for local flags debug info. */
    if ((loc_flag_dbug = (debugInfo *) malloc(nr_of_lflags*sizeof(debugInfo))) == NULL) {
      PrintError(15, NULL, "local flags debug info");
      return(ERROR);
    }

Strange thing is that the iteration of the loop when it crashes differs. This afternoon after I restarted my computer it did not crash at all. Now, after I saw your post I tried again and now it crashes again.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2457
Re: Malloc() / Heap issue with debug build target
« Reply #6 on: April 22, 2019, 10:54:37 pm »
So it crashes also if no watches are enables and the watch view is empty?

The debug log is not about the warnings, but about the difference between release and debug build, so you should post a build log of the release build and the debug build, so we can compare the compiler flags. But this is only for the future, or for other reader, i do not suspect that this is the problem here...

Have you checked if the len variable has a reasonable size?
Code: [Select]
if (!GetNextCode32(&len)) {
    PrintError(14, NULL, "ReadString()");
    return(NULL);
  }

  /* create space on heap */
  if ((str = (char *) malloc(len*sizeof(char))) == NULL) {

using uninitialized variables is never the best idea, and no range checking for a malloc input also not....

Offline Marnix

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Malloc() / Heap issue with debug build target
« Reply #7 on: April 23, 2019, 09:22:21 pm »
Regarding the watch view, I have disabled it but I cannot delete the function arguments and local variables from it.

Before I continue, I'm not sure any more if this is a CB issue, so if any of the mods decide to close this thread, I'll regret it but the forum rules are clear.

I did some more testing and I see unpredictable behavior:

- if I declare one additional local variable in the function that calls the failing ReadString() it works.
- but if, additionaly to the extra declaration, I initialize the nr_of_lflags var it crashes again.

Below is the function code, see the 3 COMMENTS IN CAPS.


Code: [Select]
int32_t ReadFlags(int64_t offset)
{
  int32_t code        = NO_ID;
  int32_t com_loc_len = 0; /* Length of common location flags string.                                */
  int32_t com_obj_len = 0; /* Length of common object flags string.                                  */
                           /* Length of local flags string is a global variable for save() function. */

  int32_t nr_of_lflags;  /* IF I INITIALIZE THIS VAR THE EXTRA LOCAL DECLARATION HAS NO EFFECT */
  int i  = 0;

  int test = 0;  /* WITH THIS LINE IT WORKS, IF I REMOVE IT, OR INTIALIZE nr_of_lflags THE PROBLEM RETURNS */

  /* go to offset in datafile */
  if (fseek(datafile, offset, 0) == -1) {
    PrintError(16, NULL, "ReadFlags()");
    return(ERROR);
  }

  /* check for the keyword */
  if (!GetNextCode32(&code)) {
    PrintError(23, NULL, "keyword record");
    return(ERROR);
  }
  if (code != FLAGS) {
    PrintError(21, NULL, "flags");
    return(ERROR);
  }
  /* Read number of common flagbits.                  */
  /* We need the number for the SetBitVal() function. */
  if (!GetNextCode32(&nr_of_cflags)) {
    PrintError(23, NULL, "nr_of_cflags");
    return(ERROR);
  }

  /* calculate com_loc_len and com_obj_len. */
  com_loc_len = ((nr_of_cflags*nr_of_locs)/WORD_LEN)+1;
  com_obj_len = ((nr_of_cflags*nr_of_objs)/WORD_LEN)+1;

  /* Create space on heap for com_loc_flags. */
  if ((com_loc_flags = (int32_t *) malloc(com_loc_len*sizeof(int32_t))) == NULL) {
    PrintError(15, NULL, "ReadFlags()");
    return(ERROR);
  }

  /* Create space on heap for com_obj_flags. */
  if ((com_obj_flags = (int32_t *) malloc(com_obj_len*sizeof(int32_t))) == NULL) {
    PrintError(15, NULL, "ReadFlags()");
    return(ERROR);
  }

  /* Read the com_loc_flags. */
  if (fread((void *) com_loc_flags, sizeof(int32_t), com_loc_len, datafile)
      != com_loc_len) {
    PrintError(23, NULL, "common location flags");
    return(ERROR);
  }

  /* Read the com_obj_flags. */
  if (fread((void *) com_obj_flags, sizeof(int32_t), com_obj_len, datafile)
      != com_obj_len) {
    PrintError(23, NULL, "common object flags");
    return(ERROR);
  }

  /* Read length of local flagbits string.           */
  /* We don't need the actual number of local flags. */
  if (!GetNextCode32(&loc_flags_string_len)) {
    PrintError(23, NULL, "loc_flags_string_len");
    return(ERROR);
  }

  /* Create space on heap.             */
  if ((local_flags = (int32_t *) malloc(loc_flags_string_len*sizeof(int32_t))) == NULL) {
    PrintError(15, NULL, "ReadFlags()");
    return(ERROR);
  }

  /* read the information */
  if (fread((void *) local_flags, sizeof(int32_t), loc_flags_string_len, datafile)
      != loc_flags_string_len) {
    PrintError(23, NULL, "local flags");
    return(ERROR);
  }

  /* Check if we must read debug info */  /* @@ */
  if (debug) {
    /* check for the keyword */
    if (!GetNextCode32(&code)) {
      PrintError(23, NULL, "keyword record");
      return(ERROR);
    }
    if (code != DEBUG) {
      PrintError(21, NULL, "debug");
      return(ERROR);
    }

    /* Malloc() space for common flags debug info. */
    if ((com_flag_dbug = (debugInfo *) malloc(nr_of_cflags*sizeof(debugInfo))) == NULL) {
      PrintError(15, NULL, "common flags debug info");
      return(ERROR);
    }

    /* Malloc() space for local flags debug info. */
    if ((loc_flag_dbug = (debugInfo *) malloc(nr_of_lflags*sizeof(debugInfo))) == NULL) {
      PrintError(15, NULL, "local flags debug info");
      return(ERROR);
    }

    for (i=0; i<nr_of_cflags; i++) {
      if ( ((com_flag_dbug[i]).name = ReadString()) == NULL)
        return(ERROR);
    }
    /* read the number of local flags */
    /* is is only needed when we have debug info */
    if (!GetNextCode32(&nr_of_lflags)) {
      PrintError(23, NULL, "nr_of_lflags");
      return(ERROR);
    }
    for (i=0; i<nr_of_lflags; i++) {
      if ( ((loc_flag_dbug[i]).name = ReadString()) == NULL)    /* THIS rEADsTRING() CALL FAILS */
        return(ERROR);
      if (!GetNextCode32(&(loc_flag_dbug[i]).owner))
        return(ERROR);
    }
  }

  return(OK);
}

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11932
    • Travis build status
Re: Malloc() / Heap issue with debug build target
« Reply #8 on: April 23, 2019, 10:22:50 pm »
You are going to have to wait for an CB Debugging person. They will likely ask for a full debugging log.
If you want to get help you have to accomplish this.
Full log is enabled in Settings -> Debugger -> General -> Full (debug) log.
Then copy-paste the contents of the debugger tab in the log and others.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Krice

  • Almost regular
  • **
  • Posts: 125
Re: Malloc() / Heap issue with debug build target
« Reply #9 on: April 23, 2019, 10:42:02 pm »
That's an indication of memory corruption. It's quite easy to do with C using malloc etc. It just hides problems so easily. And the best part is that the actual problem may not even be in that part of the code.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2457
Re: Malloc() / Heap issue with debug build target
« Reply #10 on: April 23, 2019, 10:47:22 pm »
Quote
That's an indication of memory corruption. It's quite easy to do with C using malloc etc. It just hides problems so easily. And the best part is that the actual problem may not even be in that part of the code.
Yes. this are very strong signs for this kind of error:
Quote
- if I declare one additional local variable in the function that calls the failing ReadString() it works.
- but if, additionaly to the extra declaration, I initialize the nr_of_lflags var it crashes again.

Have you introduced an range check for the malloc size? I do not see how the crash source should be in malloc... One possibility would be that you kill your call stack and it only seems to be that the crash is in malloc...

Boy how i am happy that i can use smart pointers  :)

Offline Marnix

  • Multiple posting newcomer
  • *
  • Posts: 19
Re: Malloc() / Heap issue with debug build target
« Reply #11 on: April 25, 2019, 03:30:09 pm »
Well, I found the culprit. It was a malloc() with length 0 in a part of the code I recently added. It had nothing to do with Code::Blocks.

I donated USD 20 for the inconvenience caused.

Thanks everybody.