小编J.D*_*Doe的帖子

printf或其他东西有问题

我正在阅读一些关于安全编程的资料,当我输入:./ a.out%.622496x%.622496x%n

./a.out%.. 622496x%n

./a.out%.633x%.63336x%n

./a.out% .6edd33x%.63336x%n

有些人会得到coredump有些不会,也无法弄清楚为什么他们打印所有的0.

这是代码example.c gcc example.c

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

int main(int argc,char *argv[])
{
    char buffer[512]="";
    strncpy(buffer,argv[1],500);
    printf(buffer);

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

c printf

2
推荐指数
1
解决办法
51
查看次数

execvp 的子进程无内存问题

下面的代码来自《操作系统:三个简单的部分》一书。代码让我很困惑。我知道execvp当它运作良好时永远不会回来。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/wait.h>

int
main(int argc, char *argv[])
{
    int rc = fork();
    if (rc < 0) {
        // fork failed; exit
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) {
        // child: redirect standard output to a file
        close(STDOUT_FILENO); 
        open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);

        // now exec "wc"...
        char *myargs[3];
        myargs[0] = strdup("wc");   // program: "wc" (word count)
        myargs[1] = strdup("p4.c"); // argument: file to …
Run Code Online (Sandbox Code Playgroud)

c valgrind

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

标签 统计

c ×2

printf ×1

valgrind ×1