-1

I'm experiencing something mysterious with a C program which works fine on my pc but fails when I compile it on the server I'm working on. Basically the execution of execve fails. The original program is not too big so I started to cut some parts in order to try to understand where could be the problem.

Here a cut of the program (it's just a cut so of-course it doesn't make any sense), well, in here execve still fails:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>

int main (){
    // Arguments
    char *argv[100] = {"/home/input/input", [1 ... 99] = "A"};

    //The real program would use some pipes later
    int pipestdin[2];
    int pipestderr[2];

    pipe(pipestdin);
    pipe(pipestderr);

    // Call
    char *env = "\xde\xad\xbe\xef=\xca\xfe\xba\xbe";
    execve("/home/input/input",argv,&env);  // Execute the program  
    printf("ERROR\n"); // printed only if execve fails

    return 0;

}

but when I take out this part:

    int pipestdin[2];
    int pipestderr[2];

    pipe(pipestdin);
    pipe(pipestderr);

the program starts to work again.

Here is some information:

The program works normally on the server when I use the version compiled on my pc, that's why I suppose there is a problem with the compiler.

TTK
  • 223
  • 6
  • 21
  • And what error does `execve` fail with? (Hint: Your current program doesn't tell you. You should change it so it tells you, with either perror, or strerror(errno)) – user253751 Jan 14 '16 at 01:51
  • 2
    Your argv and envp are both constructed incorrectly. Read the man page. – nobody Jan 14 '16 at 01:52

1 Answers1

4

argv is an array of char* that should be terminated with a NULL pointer. In other words, the element following the last valid one should be NULL.

envp is a similar construct. You should declare your envp as char*[], just like what you did with argv, and pass it without & to execve.

And note: most of the time when your program doesn't work, it's not a compiler bug. Your compiler and OS libraries has been tested by millions, and if there was a bug, it's usually fixed before it reaches you.

quantum
  • 3,672
  • 29
  • 51