我正在编写这个函数:
int main(int argc, char const *argv[]) {
char* hack[];
hack[0] = "/bin/sh";
hack[1] = NULL;
execve(hack[0], &hack, &hack[1]);
return 0;
}我的问题是,写这行有什么区别:
execve(hack[0], &hack, &hack[1]);而不是:
execve(hack[0], hack, NULL);如果有什么不同,哪一个是最好的方式?编写第一种类型的表达式时,我会得到一个警告:
warning: passing argument 2 of ‘execve’ from incompatible pointer type [-Wincompatible-pointer-types]
note: expected ‘char * const*’ but argument is of type ‘char * (*)[3]’这是什么意思?
发布于 2021-04-19 16:49:48
编辑:我将从一个关于execve的解释开始,让它更清晰一些。
execve将运行程序路径名,替换当前进程、堆栈帧和堆。为此,它需要三个参数:
char*。main的argv的用法相同:获取命令行参数,第一个这样的参数是程序的pathname。argv和envp都是char **,这意味着它们是char * (以NULL结尾的字符串)的列表,最后一个元素是NULL。然后,程序可以迭代它们,直到它们找到一个空指针来收集它的参数和环境。
--
您的两个语句有两个不同之处:
- In the first case, a **pointer** to the `hack` array, meaning you have a `char***`, not compatible with the declaration which expects a `char**`
- In the second case, the `hack` array itself, which is a `char **` (AKA a string array)- In the first case, a pointer to the second element of the `hack` array, which translates to "A string array whose first element is NULL".
- In the second case, NULL, which **on LINUX systems** is interpreted like the above, but will cause an error in most other UNIX system.引用execve(2)手册页:
可以将Linux、argv和envp上的
指定为NULL。在这两种情况下,这与将参数指定为指向包含单个空指针的列表的指针具有相同的效果。在许多其他系统上,将argv指定为NULL将导致错误()。其他一些UNIX系统对envp==NULL的处理方式与Linux相同。
总而言之,在您的情况下,正确的调用是
execve(hack[0], hack, &hack[1]);https://stackoverflow.com/questions/67158460
复制相似问题