why is strncpy segfaulting here?

This is a discussion on why is strncpy segfaulting here? within the C Programming forums, part of the General Programming Boards category; It's ok my question is already answered here: http://cboard.cprogramming.com/showthread.php?t=110612 in post #5. Sorry for the repost Hi, I am writing ...

  1. #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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #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.

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace strncpy with snprintf, how?
    By garton in forum C Programming
    Replies: 19
    Last Post: 09-11-2008, 04:21 AM
  2. strncpy adavnced :P
    By Joke in forum C Programming
    Replies: 3
    Last Post: 07-14-2008, 12:14 PM
  3. strncpy
    By zmaker5 in forum C Programming
    Replies: 12
    Last Post: 07-28-2007, 05:15 AM
  4. strncpy question, size -1??
    By fayte in forum C Programming
    Replies: 16
    Last Post: 03-16-2006, 11:32 PM
  5. strncpy doesn't seem to work
    By linucksrox in forum C++ Programming
    Replies: 3
    Last Post: 09-08-2005, 02:34 PM

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