You're never checking the value of i
, yet it is indexing into your string array in the wrap
function. Here is where the i
value grows into the 1000's on my machine before crashing. I would recommend putting it on a debugger, simplifying your code, or at least check the variable that indexes into your string buffer to ensure it is not past the end of the buffer. HTH!
EDIT
Since you asked, I'll show you how I debugged this, just a little, on Ubuntu 12.04 LTS.
From the shell, file called sotest.c
# use the -g for debug info
gcc -g sotest.c -o sotest
# invoke the gnu debugger
gdb sotest
You'll see:
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-linux-gnu". For bug reporting instructions, please see: http://bugs.launchpad.net/gdb-linaro/... Reading symbols from /home/macduff/sotest...done. (gdb)
Then, invoke the program with the input file:
run < wraptest
Then you'll get some output text with the stack trace
Program received signal SIGSEGV, Segmentation fault. 0x08048666 in wrap ( s=0xbfffebd4 "asd\n\030\313\375\267\254|\376\267\f\356\377\277", l=3) at sotest.c:58 58 for (i = c + WRAP - 1; i >= c && s[i] != ' ' && s[i] != '\t'; --i)