可能溢出的缓冲区

可能溢出的缓冲区

Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我对此并不陌生,似乎无法理解,您能帮我确定可能溢出的缓冲区吗?为什么?

#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;
}

最佳答案

问题在这里:

sprintf(out, "argument %d is %s\n", argc - 1, argv[argc - 1]);


当您遇到这样的问题时,遍历代码注释行直到程序停止崩溃总是一个好主意。然后,您将知道错误的位置:D

关于c - 可能溢出的缓冲区的名称是buf,msg,len或out ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26584087/

10-11 13:32