小编ale*_*lin的帖子

在 gdb 中调用时 strlen 返回不正确的值

所以我注意到调用 GDB 中的标准库函数时出现了一些严重不正确的行为。我有以下程序来说明:

#include <stdio.h> 
#include <stdlib.h>
#include <string.h> 

int main(int argc, char *argv[]) {
  char *s1 = "test";
  char *s2 = calloc(strlen("test")+1,sizeof(char));

  snprintf(s2,strlen("test")+1,"test");

  printf("string constant: %lu\n", strlen(s1));
  printf("allocated string: %lu\n", strlen(s2));

  free(s2);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当从命令行运行时,这个程序输出你所期望的:

string constant: 4
allocated string: 4
Run Code Online (Sandbox Code Playgroud)

但是,在 GDB 中,我从对 strlen() 的调用中得到以下不正确的输出:

(gdb) p strlen(s1)
$1 = -938856896
(gdb) p strlen(s2)
$2 = -938856896
Run Code Online (Sandbox Code Playgroud)

我很确定这是 Ubuntu 附带的 glibc 的问题(我使用的是 10.10),但对于我们这些在 GDB 上花费大量时间的人来说,这是一个严重的问题。

  1. 有没有其他人遇到这种错误?

  2. 修复它的最佳方法是什么?从源代码构建 glibc?(我已经在运行从源代码构建的 GDB 版本)

programming debugging c

6
推荐指数
1
解决办法
4736
查看次数

标签 统计

c ×1

debugging ×1

programming ×1