简体   繁体   中英

Why does this code run fine on my Laptop (OSX 10.9), but segfault on server (Linux)?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char String[256];

typedef struct LinkedListNode {
String Name;
struct LinkedListNode * Next;
struct LinkedListNode * Friend;
} Node;

typedef Node * NodePointer;


NodePointer InputData(NodePointer Head) {

    String PersonName;
    NodePointer Person;
    int StringLength;
    String FormattedName;
do {    
        printf("Enter nation name :");
        fgets(PersonName,256,stdin);
        if (PersonName[0] != '\n') {
            Person = (NodePointer)malloc(sizeof(NodePointer));
            //copy all except trailing \n
            strncpy(Person->Name, PersonName, strlen(PersonName)-1);
            Person->Next = Head;
            Person->Friend = NULL;
            Head = Person;
        }
    } while (strcmp(PersonName, "\n"));
return Head;
}

void InputAllies(NodePointer Head) {
    String AllyName;
    NodePointer Ally;
    String FormattedName;
    do {    
            printf("Enter best ally for %s :", Head->Name);
            fgets(AllyName,256,stdin);  
            if (AllyName[0] != '\n') {
                Ally = (NodePointer)malloc(sizeof(NodePointer));
                //copy all except trailing \n
                strncpy(Ally->Name, AllyName, strlen(AllyName)-1);
                Head->Friend = Ally;
                Head = Head->Next;
            }
        } while (Head != NULL);
}

The part that segfaults specifically is the InputAllies() function, and only with lists with 5 or more elements. I really have no idea what's going on, but I assume it has something to do with the size of my strings. Lowering the size of the typedef String causes a segfault after only 3 elements.

I think that in function InputData , just after the strncpy , you are forgetting to add the final '\\0' to the string Person->Name . The problem is when you print the string. On your Mac, it seems that there is a null character that happens to be magically in the right place; on the Linux server, you're not so lucky.

Copying from the manual :

The strcpy() function copies the string pointed to by src , including the terminating null byte ( '\\0' ), to the buffer pointed to by dest . The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src , the string placed in dest will not be null-terminated.

So your problem is the last sentence, and it will happen only when your source string cannot fit in the first n bytes of your target string. I think that's exactly your case here, as you're setting n to be less than the strlen of your source string. In any case, there may be other bugs in your code too. Check with a debugger or valgrind.

BTW, I don't understand why you want strncpy there, as your source and target strings are necessarily of the same length. Why not use strcpy instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM