我是新手,我似乎无法理解,请你帮我识别可能溢出的缓冲区,为什么?
#include <stdio.h>
#include <string.h>
#define S 100
#define N 1000
int main(int argc, char *argv[]) {
char out[S];
char buf[N];
char msg[] = "Welcome to the argument echoing program\n";
int len = 0;
buf[0] = '\0';
printf(msg);
while (argc) {
sprintf(out, "argument %d is %s\n", argc-1, argv[argc-1]);
argc--;``
strncat(buf,out,sizeof(buf)-len-1);
len = strlen(buf);
}
printf("%s",buf);
return 0;
}
答案 0 :(得分:0)
问题在于:
sprintf(out, "argument %d is %s\n", argc - 1, argv[argc - 1]);
当您遇到类似这样的问题时,在程序停止崩溃之前,通过代码注释行总是一个好主意。然后你会知道bug的位置:D