gpt4 book ai didi

c - %p 在 printf 中的用法?

转载 作者:太空狗 更新时间:2023-10-29 15:58:15 28 4
gpt4 key购买 nike

我正在通过此链接在线阅读以下代码:http://www.cse.scu.edu/~tschwarz/coen152_05/Lectures/BufferOverflow.html

我对这一行中 %p 的用法感到困惑:

 printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");

取自这段代码:

 /*
StackOverrun.c
This program shows an example of how a stack-based
buffer overrun can be used to execute arbitrary code. Its
objective is to find an input string that executes the function bar.
*/

#pragma check_stack(off)

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

void foo(const char* input)
{
char buf[10];

printf("My stack looks like:\n%p\n%p\n%p\n%p\n%p\n% p\n\n");

strcpy(buf, input);
printf("%s\n", buf);

printf("Now the stack looks like:\n%p\n%p\n%p\n%p\n%p\n%p\n\n");
}

void bar(void)
{
printf("Augh! I've been hacked!\n");
}

int main(int argc, char* argv[])
{
//Blatant cheating to make life easier on myself
printf("Address of foo = %p\n", foo);
printf("Address of bar = %p\n", bar);
if (argc != 2)
{
printf("Please supply a string as an argument!\n");
return -1;
}
foo(argv[1]);
return 0;
}

我知道 %p 是指针的格式化程序,但为什么格式化程序后面没有值?这里实际打印了什么值?如果它正在打印提供给 foo 函数的参数的地址,那么为什么有 5 个 '%p' 并且为什么不是所有的 '%p' 都格式化相同的值?

非常感谢。

最佳答案

这是在利用未定义的行为。

通过故意不向 printf 提供值,va_arg 将从要打印的堆栈中提取任意值。这不是正确的代码。事实上,这段代码片段似乎是在试图解释黑客技术,这些技术通常会利用未定义行为发生时出现的故障。

关于c - %p 在 printf 中的用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22521064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com