7

According to exec reference, calls to exec (or stack checking vararg functions in general) requires a (char*)NULL aka 0 at the end of the parameter list. GCC, however, is complaining about the following code

char cmdFullPath[4096]; //yes this 4096 thing is bad coding practice 
...
execl(cmdFullPath, (char*)NULL);

//warning: not enough variable arguments to fit a sentinel

Anyone know what's wrong?

jameszhao00
  • 7,213
  • 15
  • 62
  • 112
  • Possible duplicate: http://stackoverflow.com/questions/2050961/is-argv0-name-of-executable-an-accepted-standard-or-just-a-common-conventio –  Sep 28 '10 at 20:40

3 Answers3

11

That reference says that the prototype is

execl(const char * path, const char * arg, ...)

I read that as 2 parameters + (char*)NULL

something like :

execl(cmdFullPath, (const char*)NULL, (char*)NULL);

from the page:

#include <unistd.h>

int main() {
    execl("/bin/ls", "ls", "-l", (char *)NULL);
    return 0;
}
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 1
    You're right. Temporarily forgot that the 1st arg is mandatory (executable name). Doh! – jameszhao00 Sep 28 '10 at 20:35
  • 1
    what if the arg list is like arg1=val1, arg2=val2, arg3=val3, etc... or arg1 val1 arg2 val2 arg3 val3,etc ... ? – enthusiasticgeek Mar 12 '12 at 21:31
  • if i'm correct `return 0;` is never executed .. is it a convention to write it? – philx_x Oct 19 '18 at 13:58
  • @philx_x - see here https://www.codeproject.com/Questions/693038/why-do-we-have-to-use-return - it's needed in C but optional in C++ – Preet Sangha Oct 23 '18 at 02:00
  • @PreetSangha this is addressing a different question. My concern is that anything after `execl` is unnecessary because at that point the process's program text is literally overwritten with the one of `ls`. – philx_x Oct 24 '18 at 07:12
  • 1
    @philx_x - please feel free to edit the question to improve! – Preet Sangha Oct 24 '18 at 23:55
2

Its usual to pass the executable name in as the first parameter

So if the executable you are executing is "/bin/ls" (as per the link you posted) then the first parameter is "ls" and you'd THEN pass (char*)NULL in as the last (ie in this case 3rd) parameter.

Goz
  • 61,365
  • 24
  • 124
  • 204
0

You have to pass at least three arguments. The second one is argv[0] so it cannot be null.

execl(cmdFullPath, "", NULL)
ecik
  • 819
  • 5
  • 11