![]() |
| | #1 |
| Registered User Join Date: Dec 2005 Location: german border
Posts: 72
| why is strncpy segfaulting here? Hi, I am writing some code to parse a port range given as a string eg: "20-25". After I figure out where the first port ends and I try to copy it however I get a segfault: Code: if( ( dash = strchr(ports, '-')) != NULL )
{
/* port range given */
int index = dash - ports;
if(index < 0)
index = -index;
char *temp;
fnshPort = atoi( (dash + 1) );
strncpy( temp, ports, index ); /* here is the segfault */
strtPort = atoi(temp);
Code: Breakpoint 1, main (argc=3, argv=0xbfbf5e84) at starshine.c:59 59 strncpy( temp, ports, index ); /* here is the segfault */ (gdb) print temp $1 = 0xb80e9e00 "U\211�WVS�p\207" (gdb) print ports $2 = 0xbfbf7c13 "20-25" (gdb) step Program received signal SIGSEGV, Segmentation fault. 0xb7fee41c in strncpy () from /lib/libc.so.6 Code: char temp[15]; Code: char temp[index]; So my question is: why doesn't a plain old c string work? Last edited by Calef13; 12-28-2008 at 10:56 PM. |
| Calef13 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Because char temp[15] is a plain old C-string, and char *temp isn't? More to the point, temp will point to a C-string, but you have to have one around to point to; right now you are pointing at New Jersey (some people claim that it's nowhere) and that's no place to try to write a string to. temp[index] is valid in C99 -- but temp[2] is not nearly big enough to hold "20", which requires three characters. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Dec 2008 Location: Loch Ness
Posts: 6
| your temp pointer is pointing to some memory not allocated. if u read the man page for strncpy, it says that your destination should be a buffer i.e. u cant pass a pointer that has no memory allocated. obviously if u declare an array it will solve the problem as it is now pointing to a legal block of memory. |
| gooey_kablooie is offline | |
| | #4 |
| Registered User Join Date: Sep 2007
Posts: 372
| And do be careful with strncpy(). It doesn't necessarily create a string, despite its name; and in your code, it definitely won't create one (without some help). The problem is that if strncpy() doesn't see a null character in the source string, it won't put one in the destination string (which means it's not actually a string at all). As far as I can see, your code will essentially be doing strncpy(temp, "20-25", 2). Since neither '2' nor '0' is a null character, no string will be created. An easy way to fix that is to do temp[index] = 0 after your strncpy(). |
| cas is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Replace strncpy with snprintf, how? | garton | C Programming | 19 | 09-11-2008 03:21 AM |
| strncpy adavnced :P | Joke | C Programming | 3 | 07-14-2008 11:14 AM |
| strncpy | zmaker5 | C Programming | 12 | 07-28-2007 04:15 AM |
| strncpy question, size -1?? | fayte | C Programming | 16 | 03-16-2006 11:32 PM |
| strncpy doesn't seem to work | linucksrox | C++ Programming | 3 | 09-08-2005 01:34 PM |