为什么sprintf(命令,“rm%s”,newfile)之后所有字符串都发生了变化,我说“命令”与“allclear”没有关系
(gdb) p allclear
$18 = "/home/river/Desktop/stage2/bin/config/02_allclear_12HD", '\000' <repeats 45 times>
(gdb) p &allclear
$19 = (char (*)[100]) 0xbfffea0c
(gdb) p &command
$20 = (char (*)[50]) 0xbfffe9da
**(gdb) n
65 sprintf( command, "rm %s", newfile );**
(gdb) p allclear
$21 = "/home/river/Desktop/stage2/bin/config/02_allclear_12HD", '\000' <repeats 45 times>
(gdb) n
66 if( argc < 1) return 1;
**(gdb) p allclear
$22 = "001005/controlpage\000/stage2/bin/config/02_allclear_12HD", '\000' <repeats 45 times>**
(gdb) p $allclear
$23 = void
(gdb) p &allclear
$24 = (char (*)[100]) 0xbfffea0c
(gdb) p newfile
$25 = "/home/river/Desktop/stage2/test_case/01_SES/SES001005/controlpage", '\000' <repeats 34 times>
(gdb) p &command
$26 = (char (*)[50]) 0xbfffe9da
我的代码的一部分是:
char allclear[MAXPATHSIZE];
memset( allclear, 0, MAXPATHSIZE);
sprintf( allclear, "%s/config/02_allclear_12HD", curfilepathdir);
char command[MAXCOMMAMDSIZE];
memset( command, 0, MAXCOMMAMDSIZE);
sprintf( command, "rm %s", newfile );
答案 0 :(得分:2)
从GDB输出中可以清楚地看出MAXCOMMANDSIZE
是50。
"rm /home/river/Desktop/stage2/test_case/01_SES/SES001005/controlpage"
多长时间?
您可能希望阅读buffer overflows,并开始使用sprintf
的更安全变体,即snprintf
。