4

I am trying to get a secure "run-like" program in node.js that runs C programs. I understand that I must use a child process to achieve my goal... And I choose exec because it has a callback arguments:

exec.js

const { exec } = require('child_process');

var options = {
    timeout: 100,
    stdio: 'inherit',
    shell: true,
}

exec('gcc teste.c -o teste', (error, stdout, stderr) => {
    exec('./teste', options, (error,stdout,stderr)=>{
        console.log(`stdout: ${stdout}`);
        console.log(`stderr: ${stderr}`);

        if (error) {
            console.error(`exec error: ${error}`);
            return;
          }
    });  
});

teste.c

#include <stdio.h>
void main(){
    int i;
    printf("Hello World\n");
}

this is the output I am getting:

stdout: Hello World

stderr:
exec error: Error: Command failed: ./teste

Someone knows why this is happening? There is a better way of doing that? How can I really get timout working?

Thanks

4
  • @tadman The path it correct since the program does run, else there would be output to std out. Thassya Abreu can you try to add a return 0 to your c program? Edit: tadman was faster Commented Aug 27, 2018 at 18:36
  • That#s apparently not related to C. Commented Aug 27, 2018 at 19:48
  • @PatrickHollweck I did it: picture Commented Aug 27, 2018 at 21:39
  • @Thassya Abreu Your return type is still void, change it to int Commented Aug 28, 2018 at 10:44

1 Answer 1

4

Your executable should return a zero value (no error) on success:

#include <stdio.h>
int main(){
  int i;
  printf("Hello World\n");

  return 0;
}

If it doesn't you may have a random value assigned and that indicates some kind of error.

Sign up to request clarification or add additional context in comments.

3 Comments

Now is a good time to point out that, per the language definition, main is supposed to return int, not void. The behavior of void main() is undefined.
I put "return 0" but the error message is still there... picture
It's worth checking process.cwd() to see if the executable is being generated where you expect it, and if not, to specify the full path to the target. That it's running and still producing an error is odd, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.