#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20

    Strncmp segmentation fault.


    Hi,
    I'm just a beginner in C programming so hopefully you can help me out.
    I have a problem that I have ran into numerous times. The code below tries to compare two hashes with eachother, when I print them I can see that both "first" and "second" are a 28 character long hash (not counting the '\0').

    However, when I try to compare them using strncmp I get a segmentation fault. Am I doing something wrong with the strncmp? or am I doing something wrong before that but it just doesn't crash yet?

    I am especially interested at what I am doing wrong rather than the correct way to do it. Both would be nice though :).

    Thanks in advance!
    Code:
    char *first;
    char *second;
    
    strtok( someText, ":" ); // someText is an existing variable
    first = strtok( NULL, ":" ); // printf("%s",first) now prints a hash 
    second = crypt(second, salt ); // printf("%s",second) also prints a hash
    
    // I know the hash is 28 characters so:
    if( strncmp( first, second, 28) == 0 ){
    	printf("the same\n");
    } else{
    	printf("not the same\n");
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,151
    Rep Power
    2222
    Code:
    char *first;
    char *second;
    
    strtok( someText, ":" ); // someText is an existing variable
    first = strtok( NULL, ":" ); // printf("%s",first) now prints a hash 
    second = crypt(second, salt ); // printf("%s",second) also prints a hash
    
    // I know the hash is 28 characters so:
    if( strncmp( first, second, 28) == 0 ){
    	printf("the same\n");
    } else{
    	printf("not the same\n");
    }
    What memory is allocated to second? More specifically, to what was second initialized to point to before you called crypt()?

    Of course, I'm making assumptions about crypt(), but normally when I see a char pointer being passed into a function that means that that pointer is already pointing to a string that it's passing in to the function; if it were meant to have memory allocated to it in the function, then that would have been a char** instead of a char*.

    Bottom line: second is an uninitialized pointer that's being used by crypt(). If it was declared locally, then it contains garbage which is likely to be randomly pointing outside of the program's memory space, thus causing a SEGFAULT or access error that leads to the program being terminated immediately with extreme prejudice. If it was declared globally, then it is by default initialized to NULL, which points to address zero, which undoubtedly belongs to the OS, thus triggering immediate termination with extreme prejudice.

    Initialize second with malloc or calloc. If you make it a char array, then that should not compile because of the assignment (you cannot change the address of an array). Or you could pass a different string to crypt whose address could then be assigned to second -- that's assuming that crypt simply returns the address of the first parameter as a courtesy (like strcpy, so that the function could itself be used as an argument in a function call). Or, making that same assumption about crypt, you could forego the assignment which is redundant anyway, but that would at least allow second to be a char array (though you still would need to have initialized second before calling crypt).
    Last edited by dwise1_aol; April 4th, 2013 at 11:38 AM.

IMN logo majestic logo threadwatch logo seochat tools logo