UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » [SOLVED] some guidance on debugging a segfault originating in printf?

#1 2011-08-16 07:58 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

[SOLVED] some guidance on debugging a segfault originating in printf?

gcc -g -ggdb -g3 -ggdb3 -Wall -Wextra -Wpadded -O0 -I../../external -I../../src -o main ./main.c

Last edited by thinking (2011-08-31 03:28 PM)

Offline

#2 2011-08-17 12:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: [SOLVED] some guidance on debugging a segfault originating in printf?

I doubt it's a pure stack overflow of that sort (ie: just using too much stack space)...  Much more likely is dynamic heap corruption...  Though, I'm not sure why valgrind wouldn't spot it for you...  It's always been great at catching such things for me...  Perhaps it is stack corruption of some sort, such as overflowing a stack buffer and trashing other important stuff also on the stack (like function return addresses)...  It's hard to say without seeing more code...

However, just from the 3 lines you posted, I don't like the look of the third one...  A scanf("%s") is unbounded, so could easily overflow your buffer there...  You'd be far better off using something like fgets() or getline() for input...

Offline

#3 2011-08-17 03:56 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: [SOLVED] some guidance on debugging a segfault originating in printf?

From the lines posted, I would guess that 'buffer' isn't always NUL terminated.
(or line terminated, for that matter.) Make sure the receiver always terminates
the buffer with a NUL before processing the data.

Stack size you can see in /proc/$PID/smaps, for instance. So you could check
that when it's hanging in gdb after a crash.

Took me forever to find out that X crashed with no error messages because
it used more stack than I allowed with ulimit -s.

Offline

#4 2011-08-17 04:07 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: [SOLVED] some guidance on debugging a segfault originating in printf?

I got confused by that scanf, thougth 'buffer' was the input instead of stdin.
That's indeed a strange way of using scanf(), I'd do what Rob suggests and
use something better fitting that's also not unbound.

What I meant is the buffer where the UDP packet is received into. But if the
UDP data aren't strings then never mind about NUL termination.

Offline

#5 2011-08-31 03:22 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: [SOLVED] some guidance on debugging a segfault originating in printf?

char *stackstart = NULL;

int main(){
    int someotherlocalvar;
    char bottom_of_stack;
    stackstart = (char*)&bottom_of_stack;
...

Last edited by thinking (2011-08-31 03:26 PM)

Offline

#6 2011-08-31 08:07 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: [SOLVED] some guidance on debugging a segfault originating in printf?

Offline

  • Index
  • » C
  • » [SOLVED] some guidance on debugging a segfault originating in printf?

Board footer

Powered by FluxBB