char* 的 printf 出现分段错误

发布于 2024-10-06 07:42:41 字数 662 浏览 0 评论 0原文

我正在尝试从套接字读取并使用 printf 打印到标准输出(必须);

然而,每次我从正常的网站读取特定文件(HTML)时,我都会遇到分段错误。

请看一下这段代码并告诉我哪里出了问题。

int total_read = 0;
 char* read_buff = malloc(BUF_SIZE);
 char* response_data = NULL;
 if (read_buff == NULL){
  perror("malloc");
  exit(1);
 }
 while((nbytes = read(fd, read_buff, BUF_SIZE)) > 0){
  int former_total = total_read;
  total_read += nbytes;
  response_data = realloc(response_data, total_read);
  memmove(response_data + former_total, read_buff, nbytes); //start writing at the end of spot before the increase.
 }
 if (nbytes < 0){
  perror("read");
  exit(1);
 }

 printf(response_data);

谢谢。

I'm Trying to read from a socket and print to stdout using printf (a must);

However I get a Segmentation Fault every time I read a specific file (an HTML) from the sane web site.

Please, take a look at this code and tell me what wrong.

int total_read = 0;
 char* read_buff = malloc(BUF_SIZE);
 char* response_data = NULL;
 if (read_buff == NULL){
  perror("malloc");
  exit(1);
 }
 while((nbytes = read(fd, read_buff, BUF_SIZE)) > 0){
  int former_total = total_read;
  total_read += nbytes;
  response_data = realloc(response_data, total_read);
  memmove(response_data + former_total, read_buff, nbytes); //start writing at the end of spot before the increase.
 }
 if (nbytes < 0){
  perror("read");
  exit(1);
 }

 printf(response_data);

Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-10-13 07:42:41

response_data 可能不是 NUL ('\0') 终止,因此 printf 会继续到字符串末尾。或者它可能包含 % 指令,但 printf 无法找到更多参数。

相反,告诉 printf 读取多远,并且解释字符串中的任何 % 指令。

printf("%.*s", total_read, response_data);

请注意,如果 response_data 包含嵌入的 NUL,即使 total_read 更长,printf 也会在那里停止。

response_data is probably not NUL ('\0') terminated, so printf continues past the end of the string. Or possibly it contains a % directive but printf can't find further arguments.

Instead, tell printf how far to read, and not to interpret any % directives in the string.

printf("%.*s", total_read, response_data);

Note that if response_data contains an embedded NUL, printf will stop there even if total_read is longer.

孤寂小茶 2024-10-13 07:42:41

response_data 中可能包含什么?如果它包含 printf 格式字符(即 % 后跟常用选项之一),printf 将尝试访问一些您未传递的参数,并出现分段错误很有可能。尝试使用 puts 来代替?

如果必须使用 printf,请执行 printf("%s", response_data) (并首先以 NUL 终止)

What's likely to be in response_data? If it contains printf-formatting characters (i.e. % followed by one of the usual options), printf will try to access some parameters you've not passed, and a segmentation fault is quite likely. Try puts instead?

If you must use printf, do printf("%s", response_data) (and NUL-terminate it first)

不回头走下去 2024-10-13 07:42:41

我从您的帖子中了解到,响应是 HTML 数据。
由于它是文本,因此您尝试打印它。不要像您那样使用 printf。
相反,请执行以下操作:

for(int i = 0; i < total_read; i++)
   putc(response_data[i],stdout);

My understanding from your post is that the response is the HTML data.
And since it is text you attempt to print it. Do not use printf the way you do.
Instead do the following:

for(int i = 0; i < total_read; i++)
   putc(response_data[i],stdout);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文