YuYang
YuYang

Reputation: 321

memcpy segment fault, what's wrong with this code?

my software is a Web Crawler,when I get the body from the http response, it cracks.

resp->body = Malloc(content_len);
memcpy(resp->body, body_start, content_len); //THIS IS THE FAULTY LINE

Malloc is a wrapper function of malloc,so resp->body is not NULL, and content_len is the length of memory area begin with body_start,but its content is "PK\003\004\024", "\003" is ETX(end of text), "\004" is EOT(end of transmission),"\024" is device control 4,I really don't know what 's the meaning of these strange chracters,why does it crack?

Upvotes: 1

Views: 319

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180877

The body is ZIP encoded, from the ZIP wikipedia page;

Magic number
none, though PK\003\004, PK\005\006 (empty archive), or PK\007\008 (spanned archive) are common.

You'll need to check the header and unzip the body before reading it.

As for the segmentation fault, any of the 3 parameters to memcpy could be the culprit, code showing their initialisation is required to spot the exact problem. If you're using any of the string functions (strlen/strcpy) on the body in a non shown part of the code, they're likely to break with binary input like this.

Upvotes: 2

Related Questions