Segment fault related to strlen.S

Hello,
This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!):

void get_hashes(unsigned int hash[],  unsigned char *in)
{
    unsigned char *str = in;
    int pos = strlen((char *) in);    //This line is of problem!
    hash[0] = RSHash(str, pos);
    hash[1] = DJBHash(str, pos);
    hash[2] = FNVHash(str, pos);
    hash[3] = JSHash(str, pos);
    hash[4] = PJWHash(str, pos);
    hash[5] = SDBMHash(str, pos);
    hash[6] = DEKHash(str, pos);
    hash[7] = murmur(str, (uint64_t) pos, (uint64_t) pos);
}

Then I used gdb to debug, and I got this message:

Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106    ../sysdeps/x86_64/strlen.S: No such file or directory.

The problem line has been highlighted. Code for the whole program is attached. I used type cast to suppress the warnings at compiling without full understanding.
Can someone explain the error for me, and show me the correct fix?
Thanks a lot!

Now you have some understanding of why you shouldn't use type casts to suppress warnings.

You're trying to take the length of a string - but it's not a string.

Invalid loop count. Change

    for (j = 0; j < 23; j++) {

to

   for (j = 0; j < 22; j++) {

Thanks achenle, you said it is not a string , but it seems the program has done what I expected except the "segmentation fault" error. Can you please show me the fix, if possible?
Murphy! I made the change, but the error still there!

Also I changed all the corresponding parameters from unsigned char to const char to have consistency and avoid typecasting:

void get_hashes(unsigned int hash[],  const char *in)
{
    int pos = strlen(in);            //causing Segmentation fault at end of run!
    hash[0] = RSHash(in, pos);
    hash[1] = DJBHash(in, pos);
}

but the problem is still the same. Anyone more clues, please?

Running the code with Qt creator it seems limiting the bound in the outer loop:

for (j = 0; j < 22; j++)

allows the code to run with out any memory violation errors. This might be a clue to the problem ( not sure what the code does )..

Edit: Opps! when I change the outer loop bound to 22 the code seems to run without any memory access violations on Fedora 23 using g++ version: 5.3.1 20151207 . Not sure what the problem is on your end. I would find which loop indexes the program crashes for you and then trace the code from there using a debugger.

1 Like

Thanks cman!
Finally the problem was found to be the third last entry of my str[], for which the comma was missing!

*str[]={......,
"......cagt",
"....acgatc"         //Here the comma was left out!
"...ggggcta",
"....acgcat"}
};

Each single string was 500 char long wrapped in two lines but concatenated with a "\n" in the middle. The missing comma make the third last string 1000 char long, which violated the macro definition:

#define WORD_BUF_SIZE 512

However, I'd like to know your way to debug I would find which loop indexes the program crashes for you! Thanks a lot!

That's just how I would do the debugging. Once I knew the index where the program crashed , you could use a debugger to trace forward at the beginning of the loop(s) to find what caused the crash ( this way you can eliminate a lot of code as the problem - as long as something in the prior code did not cause the problem in the first place, in which case you could go backward in the indexes to find what the problem is ).