sprintf使用中的段错误
我正在尝试从数据包有效负载中读取其用户代理。
我正在数据包有效负载中搜索“User-Agent”,然后从那时起复制“User-Agent”出现和之后第一次出现“\n”之间的尽可能多的字符。这是我的代码:
sprintf(
stat,"%.*s\0",
strstr(strstr(p->data, "User-Agent:"),"\n") - strstr(p->data, "User-Agent:"),
strstr(p->data, "User-Agent: ")
);
问题是,对于很多数据包,这会导致段错误。我对这种愚蠢的写法感到抱歉;如果您知道更好的书写方式(或格式),请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要编写如此复杂的代码?指针关节炎可能会指向会产生段错误的东西。考虑重构代码。
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.