eric.frederich
eric.frederich

Reputation: 1668

Safe version of popen()?

I use fork()/exec()/wait() rather than system() when the command has user input as some of its arguments so the user can't put something like...

&rm -rf /home/* && echo HAHA

... as an argument.

I'm assuming popen is as dangerous as system() because it takes a single string and not a list of strings like the exec family of functions do.

I can only get the return value from the exec functions though. Is there a "safe" version of popen that I can run with user input and process stdout / stderr back in the parent process?

Upvotes: 1

Views: 2158

Answers (1)

unwind
unwind

Reputation: 399803

The safe way is to set up the necessary pipes yourself, using straight pipe() calls directly.

That's what popen() does under the hood, but it also invokes the shell in order to run the child process. Skipping that step should make it safer.

Upvotes: 5

Related Questions