Author Topic: strlen() fails when the code is compiled with mingw gcc  (Read 10004 times)

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
strlen() fails when the code is compiled with mingw gcc
« on: March 13, 2013, 04:27:53 pm »
Hi !

I've just installed code::block bundled with mingw.
To test the compiler/debugger tools, I've compiled this snippet :

Code: [Select]
#include <tchar.h>
#include <memory.h> // for memset
#include <string>

using namespace std;

const unsigned kuiConfigFileNameMaxLength = 128;

int _tmain(int argc, _TCHAR* argv[])
{
  int iErr = 0;
  char cConfigFileName [kuiConfigFileNameMaxLength];
  memset( (void*)cConfigFileName , 0, kuiConfigFileNameMaxLength );

  // Get input file name
  if ( strlen( (const char *) argv[1] ) > kuiConfigFileNameMaxLength )
  {
    iErr = 1;
  }
  else
  {
    memcpy( (void*)cConfigFileName, (const void*)argv[1], strlen( (const char *) argv[1] ));
  }

  string strConfigFileName( (const char*)(cConfigFileName), strlen( (const char *)cConfigFileName) );

  return iErr;
}


While debugging, the program crahses @ line :
Code: [Select]
  if ( strlen( (const char *) argv[1] ) > kuiConfigFileNameMaxLength )

Here the debugger messages :
Program received signal SIGSEGV, Segmentation fault.
In std::string::c_str() const () ()
Program received signal SIGSEGV, Segmentation fault.

Still, this works perfectly when compiled with Visual Studio.

Any hints ?

Regards

Sylvain


Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5229
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #1 on: March 13, 2013, 04:49:46 pm »
Maybe, you have the same issue like Watching std::string in debugger causes segfault?!?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #2 on: March 13, 2013, 05:38:03 pm »
Dear OllyDbg,

Many thanks for your reply.

I went through the post link you provided to me.
Nevertheless, I'am not expert enough to understand why, in my case, calling the C function strlen() triggers std::string::c_str() C++ method which in turn makes my program crash !

May I ask you some enlightenments or some biblio references about that behaviour ?

Best Regards

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 82
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #3 on: March 13, 2013, 05:46:46 pm »
I would say that argv[1] is NULL. (Do you provide debug args, and do you have a project ?)
and with some optimisation, crash "occurs" in string constructor.

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #4 on: March 13, 2013, 05:56:27 pm »
Dear jarod42,

yes, I provided some non null string into argv[1].
Moreover, following memcpy works fine !

You talk about string constructor crash, but it's invoked / executed later in the code.
The Seg fault that I pointed out occurs at the strlen() code line.

Regards

Sylvain

Online stahta01

  • Lives here!
  • ****
  • Posts: 6800
    • My Best Post
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #5 on: March 13, 2013, 06:26:00 pm »
Try it with the Compiler that worked in the past; using the new CB.
Did it work or fail?

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 jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7253
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #6 on: March 13, 2013, 08:12:35 pm »
Can you turn on the debuggers debug log ("Settings -> Debugger -> Common -> Full (debug) log" and post the content of the debug log here ?

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #7 on: March 13, 2013, 09:30:29 pm »

@ Tim

When I try to compile using MS Visual 2010 compilation - link - run is ok

When I try to compile using MS Visual 2010 tools hooked up to C::B, it fails complaining :
Code: [Select]
cl.exe /nologo /Zi  /Zi   /I..\..\..\CommonIncludes /I"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"  /c BuildCrocusCfgFile.cpp /FoDebug\BuildCrocusCfgFile.o
link.exe /nologo /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib"  /out:Debug\BuildCrocusCfgFile.exe  Debug\BuildCrocusCfgFile.o   /DEBUG /DEBUG /DEBUG
BuildCrocusCfgFile.cpp
BuildCrocusCfgFile.cpp(4) : fatal error C1083: Cannot open include file: 'tchar.h': No such file or directory
Process terminated with status 2 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)


@ Jens

Here the snapshot :

Code: [Select]
Building to ensure sources are up-to-date
Selecting target:
Debug Win32
Adding source dir: S:\svn\ALICE\windows\codeBlockTst1\
Adding source dir: S:\svn\ALICE\windows\codeBlockTst1\
Adding file: S:\svn\ALICE\windows\codeBlockTst1\Debug\BuildCrocusCfgFile.exe
Changing directory to: S:/svn/ALICE/windows/codeBlockTst1/.
Set variable: PATH=.;C:\Program Files (x86)\CodeBlocks\MinGW\bin;C:\Program Files (x86)\CodeBlocks\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Program Files\WIDCOMM\Bluetooth Software;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\TortoiseSVN\bin;C:\Program Files\MATLAB\R2011a\runtime\win64;C:\Program Files\MATLAB\R2011a\bin;C:\Program Files (x86)\MKVToolNix

[debug]Command-line: C:\Program Files (x86)\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname  -quiet  -args S:/svn/ALICE/windows/codeBlockTst1/Debug/BuildCrocusCfgFile.exe
[debug]Working dir : S:\svn\ALICE\windows\codeBlockTst1

Starting debugger: C:\Program Files (x86)\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname  -quiet  -args S:/svn/ALICE/windows/codeBlockTst1/Debug/BuildCrocusCfgFile.exe
done

[debug]> set prompt >>>>>>cb_gdb:

Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints

[debug]Reading symbols from S:\svn\ALICE\windows\codeBlockTst1\Debug\BuildCrocusCfgFile.exe...done.
[debug](gdb) >>>>>>cb_gdb:
[debug]> show version
[debug]GNU gdb (GDB) 7.5
[debug]Copyright (C) 2012 Free Software Foundation, Inc.
[debug]License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[debug]This is free software: you are free to change and redistribute it.
[debug]There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
[debug]and "show warranty" for details.
[debug]This GDB was configured as "i686-pc-mingw32".
[debug]For bug reporting instructions, please see:
[debug]<http://www.gnu.org/software/gdb/bugs/>.
[debug]>>>>>>cb_gdb:
[debug]> set confirm off

Debugger name and version: GNU gdb (GDB) 7.5

[debug]>>>>>>cb_gdb:
[debug]> set width 0
[debug]>>>>>>cb_gdb:
[debug]> set height 0
[debug]>>>>>>cb_gdb:
[debug]> set breakpoint pending on
[debug]>>>>>>cb_gdb:
[debug]> set print asm-demangle on
[debug]>>>>>>cb_gdb:
[debug]> set unwindonsignal on
[debug]>>>>>>cb_gdb:
[debug]> set print elements 0
[debug]>>>>>>cb_gdb:
[debug]> set new-console on
[debug]>>>>>>cb_gdb:
[debug]> set disassembly-flavor att
[debug]>>>>>>cb_gdb:
[debug]> catch throw
[debug]Catchpoint 1 (throw)
[debug]>>>>>>cb_gdb:
[debug]> source C:\Program Files (x86)\CodeBlocks\share\codeblocks/scripts/stl-views-1.0.3.gdb
[debug]>>>>>>cb_gdb:
[debug]> directory S:/svn/ALICE/windows/codeBlockTst1/
[debug]Source directories searched: S:/svn/ALICE/windows/codeBlockTst1;$cdir;$cwd
[debug]>>>>>>cb_gdb:
[debug]> break "S:/svn/ALICE/windows/codeBlockTst1/BuildCrocusCfgFile.cpp:19"
[debug]Breakpoint 2 at 0x4013b3: file S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp, line 19.
[debug]>>>>>>cb_gdb:
[debug]> run
[debug]Starting program: S:\svn\ALICE\windows\codeBlockTst1\Debug\BuildCrocusCfgFile.exe

Child process PID: 6796

[debug][New Thread 6796.0x1b5c]
[debug]Breakpoint 2, main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013b3
[debug]>>>>>>cb_gdb:

At S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19

[debug]> bt 30
[debug]#0  main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]>>>>>>cb_gdb:
[debug]> next
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In strlen () (C:\Windows\syswow64\msvcrt.dll)

[debug]> bt 30
[debug]#0  0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
[debug]>>>>>>cb_gdb:

#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
At S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19

[debug]> bt 30
[debug]#0  0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
[debug]>>>>>>cb_gdb:

#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9

[debug]> next
[debug]Single stepping until exit from function strlen,
[debug]which has no line number information.
[debug]Program received signal SIGSEGV, Segmentation fault.
[debug]0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]>>>>>>cb_gdb:

Program received signal SIGSEGV, Segmentation fault.
In strlen () (C:\Windows\syswow64\msvcrt.dll)

[debug]> bt 30
[debug]#0  0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
[debug]>>>>>>cb_gdb:

#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
At S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19

[debug]> bt 30
[debug]#0  0x76c643f9 in strlen () from C:\Windows\syswow64\msvcrt.dll
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]>>>>>>cb_gdb:
[debug]> frame 1
[debug]#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
[debug]S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9
[debug]>>>>>>cb_gdb:

#1  0x004013c9 in main (argc=1, argv=0x352a50) at S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19
S:\svn\ALICE\windows\codeBlockTst1\BuildCrocusCfgFile.cpp:19:450:beg:0x4013c9

[debug]> next
[debug]Single stepping until exit from function strlen,
[debug]which has no line number information.

Many thanks for your support.

Sylvain

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7253
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #8 on: March 13, 2013, 09:43:26 pm »
I would say that argv[1] is NULL.
yes, I provided some non null string into argv[1].
Code: [Select]
[...]
[debug]Starting program: S:\svn\ALICE\windows\codeBlockTst1\Debug\BuildCrocusCfgFile.exe
[...]
There is no argument passed to the exe and therefore argv[1] is 0x0.
By the way, you should always ensure you do not have a nullpointer before accessing it.

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #9 on: March 14, 2013, 10:06:20 am »
Dear Jens,

Actually I had to rebuild a new projet from scratch after having set the compiler to MS visual for Tim test purpose since C::B mixed gcc and cl parameters.
Then, you're right, the last test I performed yesterday evening was conducted without filling argv[1].
This morning, I provided a string as argv[1] and it works.
As a consequence, I don't know why my very first test failed since I've double checked that argv[1] was properly set....

I gonna restart from the beginning : checkout my original MS visual mini project, convert it into C::B project and try to compile/debug it with mingw gcc.

Cheers !

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7253
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #10 on: March 14, 2013, 10:28:41 am »
How do you set argv[1] ?
Are you aware of "Project -> Set programs arguments" ?
If you set the argument for one target, than switch to the secon target and set it here also the first one will not be saved.

Actually it seems only the target which is active when pressing the OK-button is saved correctly (at least for the programs arguments).

I will look into it this evening, if I do not forget it.

Offline sylwa

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: strlen() fails when the code is compiled with mingw gcc
« Reply #11 on: March 15, 2013, 09:26:16 am »
Dear Jens,

How do you set argv[1] ?
Are you aware of "Project -> Set programs arguments" ?

yes

If you set the argument for one target, than switch to the secon target and set it here also the first one will not be saved.
Actually it seems only the target which is active when pressing the OK-button is saved correctly (at least for the programs arguments).

that's more clear now.

Concerning my seg fault, I cannot reproduce it. I'am very misled.
Sorry to have disturbed you for a false alarm.

Cheers!