Question

I'm getting segfault in this line:

if(memcmp(datap, 0x38 , 1) == 0)

This is a trace from gdb, you can see datap here:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004010f1 in processMTMHeader (
    datap=0x2aaaab0b001c "1\34\66\63\36\65\34\66.\36\70\34AAAA1.ETR\36\67\64\34U\35\36\61\60\63\34\61\36\62\65\70\34\60\71:00:00\36\62\70\61\34\60\71:00:00\36\64\62\67\34\63\60\60\60\36\65\63\34\63\36\66\63\34\63\36\66\67\34\63\36\70\60\34\63\36\70\61\34\61\60\60\60\36\70\62\34\60\71:00:00\36\70\63\34\61\60\60\60\3Ea", h=0x7fffffffb960,
    endmmsgp=0x2aaaab0b0090 "\3Ea") at unzipper.c:91
91      if(memcmp(datap, 0x38 , 1) == 0)
Was it helpful?

Solution

You're using the integer value 0x38 as a pointer, which is very likely not a good idea.

You should probably have:

const uint8_t data[] = { 0x38 };

if(memcmp(datap, data, sizeof data) == 0)

Or, of course, since it's just a single byte:

if(((uint8_t *) datap)[0] == 0x38)

UPDATE Assuming that datap is declared to be unsigned char *datap, we can drop the cast and just do:

if(*datap == 0x38)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top