Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spurious "missing sentinel in function call"

Tags:

c++

unix

gcc

g++

exec

This simple snippet generates the "missing sentinel in function call" warning with g++ 4.7.0, both if compiled as Cand C++ source. I believe that it's an error of the compiler, as the final NULL value is there.

#include <unistd.h>

int main() {
    execlp("mkdir", "mkdir", "-p", "test", NULL);
    return 0;
}

Am I right?

like image 919
Lorenzo Pistone Avatar asked Aug 23 '12 16:08

Lorenzo Pistone


1 Answers

No, you're wrong.

In C++ NULL is something like 0 or 0L and might be the same in C. If the type of that constant is smaller than a pointer then it is not safe to pass it to a variadic function, as the high bits will be filled with junk.

On Linux the execlp(1) man page says:

The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

i.e. to portably provide the sentinel you need to do:

    execlp("mkdir", "mkdir", "-p", "test", (char*)NULL);

and this is what GCC is warning you about.

like image 168
Jonathan Wakely Avatar answered Sep 28 '22 10:09

Jonathan Wakely