1

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)
alk
  • 69,737
  • 10
  • 105
  • 255
MaMu
  • 1,837
  • 4
  • 20
  • 36

1 Answers1

4

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)
unwind
  • 391,730
  • 64
  • 469
  • 606
  • but is not [0] position. it is somewhere inside of datap. and datap is also much longer, so that why i use 1. – MaMu Feb 10 '14 at 10:20
  • @MaMu : `memcmp(datap, data , 1);` where `data = { 0x38 };` is same as `((uint8_t *) datap)[0] == 0x38)`. If it is somewhere inside `datap` then `memcmp` would need to be done for each byte or equivalently `==` comparison need to be done for each byte in `datap`. – 0xF1 Feb 10 '14 at 10:22
  • @nos i said this wrong. i want exactly this position, which is specified , but datap is longer. – MaMu Feb 10 '14 at 10:29
  • @unwind - can i do this also without casting? datap is of type `unsigned char`. – MaMu Feb 10 '14 at 10:29
  • @MaMu I included the cast since you don't show the declaration of `datap`. Without, I really can't tell. :) – unwind Feb 10 '14 at 10:30