我最近在C中使用了函数指针.
继续回答你自己的问题的传统,我决定对那些需要快速深入研究这个主题的人进行一些基本的总结.
我正在查看来自不安全编程的示例abo3.c,并且我没有在下面的示例中讨论转换.有人可以开导我吗?
int main(int argv,char **argc)
{
extern system,puts;
void (*fn)(char*)=(void(*)(char*))&system;
char buf[256];
fn=(void(*)(char*))&puts;
strcpy(buf,argc[1]);
fn(argc[2]);
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
那么 - 系统的投射和投注是什么?他们都回归了int,为什么要把它变成无效?
我非常感谢对整个计划的解释.
[编辑]
感谢您的投入!
Jonathan Leffler,实际上代码有"坏"的原因.它应该是可利用的,溢出缓冲区和函数指针等.mishou.org有一篇关于如何利用上述代码的博客文章.其中很多仍然在我头上.
bta,我从上面的博客文章中得知,转换系统会以某种方式阻止链接器删除它.
有一点不能立即明确的是系统和放置地址都写在同一个位置,我想这可能是gera所说的"所以链接器不会删除它".
虽然我们讨论的是函数指针的主题,但现在我想问一个后续问题,即语法更清晰.我正在使用函数指针查看一些更高级的示例,并偶然发现这个可憎的东西,取自托管shellcode的站点.
#include <stdio.h>
char shellcode[] = "some shellcode";
int main(void)
{
fprintf(stdout,"Length: %d\n",strlen(shellcode));
(*(void(*)()) shellcode)();
}
所以数组被强制转换为函数返回void,引用和调用?这看起来很讨厌 - 所以上面代码的目的是什么?
[/编辑]
我之前看过stackoverflow上的问题,但这是我第一次问,所以我提前为任何格式错误道歉.我已经在C编程上上了大约一个月的课程,并且我已经被赋予了在我的main函数中使用do/while循环来循环displayMenu()的赋值,它允许用户输入1, 2,或3显示某一块信息.
int main(void)
{
int option = 0;
do
{
option = displayMenu();
}
while (option == displayName() || displayColor() || displayFood());
}
//Display the menu for choosing an option or exiting the program
int displayMenu()
{
int choice = 1;
while (choice == 1 || 2 || 3)
{
puts("Choose which piece of information you would like to know:");
printf("%s", "1 - My name, 2 - My favorite color, 3 - My favorite food\n");
printf("%s", "Or type in …Run Code Online (Sandbox Code Playgroud)