views:

80

answers:

3

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?

+3  A: 

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
You're right. Temporarily forgot that the 1st arg is mandatory (executable name). Doh!
jameszhao00
+2  A: 

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
A: 

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

execl(cmdFullPath, "", NULL)
ecik