C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-28-2008, 10:49 PM   #1
Registered User
 
Join Date: Dec 2005
Location: german border
Posts: 72
why is strncpy segfaulting here?

It's ok my question is already answered here: fscanf causes a SEGMENTATION FAULT in post #5. Sorry for the repost

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
The pointer arithmetic is copied from a website, but it makes sense to me. What has me stumped is that if I declare temp as an array it works fine, both lines below fix the problem:

Code:
char temp[15];
Code:
char temp[index];
I tried the later one but gdb shows that it doesn't get declared any smaller than the first, which was my intention, is using a number the only decent way to do it? index is 2 for the above example of "20-25".

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   Reply With Quote
Old 12-28-2008, 10:57 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 12-28-2008, 11:00 PM   #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   Reply With Quote
Old 12-29-2008, 03:27 PM   #4
cas
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22