开发者

Segfault in the use of sprintf

开发者 https://www.devze.com 2023-04-03 22:45 出处:网络
I am trying to read from a packet payload just its User agent. I am searching for \"User-Agent\" in packet payload and and from then on copying as many chars as there are between the occurrence of \"

I am trying to read from a packet payload just its User agent.

I am searching for "User-Agent" in packet payload and and from then on copying as many chars as there are between the occurrence of "User-Agent" and the first occurrence of '\n' right after that. Here's my code:

sprintf(
   stat,"%.*s\0",
   strstr(strstr(p->data, "User-Agent:"),"\n") - strstr(p开发者_如何学运维->data, "User-Agent:"),
   strstr(p->data, "User-Agent: ")
);

The trouble is that for a LOT of packets, this is causing seg fault. I am sorry for this silly way to write it; if you know a better way to write (or format), please tell me.


Why are you writing code that is so complex? The pointer arthritic could end end pointing to something that will give a seg fault. Consider refactoring the code.

0

精彩评论

暂无评论...
验证码 换一张
取 消