Skip to content

Commit

Permalink
Use req::vector in pcntl_exec
Browse files Browse the repository at this point in the history
Summary:
std::vector<String> is unsound in general. We get away with it
here because the GC never runs while the argv/envp vectors are
alive. But its worth fixing anyway.

Reviewed By: alexeyt, kodafb

Differential Revision: D5089155

fbshipit-source-id: bcb7002c1fd0c5ca6ec447fc9206e46c304bdc46
  • Loading branch information
edwinsmith authored and hhvm-bot committed May 20, 2017
1 parent 2ee4fa9 commit 65de5fd
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions hphp/runtime/ext/process/ext_process.cpp
Expand Up @@ -61,24 +61,21 @@ namespace HPHP {
///////////////////////////////////////////////////////////////////////////////

// build environment pair list
static char **build_envp(const Array& envs, std::vector<String> &senvs) {
char **envp = NULL;
int size = envs.size();
if (size) {
envp = (char **)malloc((size + 1) * sizeof(char *));
int i = 0;
for (ArrayIter iter(envs); iter; ++iter, ++i) {
StringBuffer nvpair;
nvpair.append(iter.first().toString());
nvpair.append('=');
nvpair.append(iter.second().toString());

String env = nvpair.detach();
senvs.push_back(env);
*(envp + i) = (char *)env.data();
}
*(envp + i) = NULL;
static char** build_envp(const Array& envs, req::vector<String> &senvs) {
auto const size = envs.size();
if (!size) return nullptr;
auto envp = req::make_raw_array<char*>(size + 1);
size_t i = 0;
for (ArrayIter iter(envs); iter; ++iter, ++i) {
StringBuffer nvpair;
nvpair.append(iter.first().toString());
nvpair.append('=');
nvpair.append(iter.second().toString());
String env = nvpair.detach();
senvs.push_back(env);
envp[i] = (char*)env.data();
}
envp[i] = nullptr;
return envp;
}

Expand All @@ -89,7 +86,7 @@ static bool check_cmd(const char *cmd) {
bool allow = false;
while (isblank(*cmd_tmp)) cmd_tmp++;
const char *space = strchr(cmd_tmp, ' ');
unsigned int cmd_len = strlen(cmd_tmp);
auto cmd_len = strlen(cmd_tmp);
if (space) {
cmd_len = space - cmd_tmp;
}
Expand Down Expand Up @@ -213,25 +210,25 @@ void HHVM_FUNCTION(pcntl_exec,
return;
}

// build argumnent list
std::vector<String> sargs; // holding those char *
int size = args.size();
char **argv = (char **)malloc((size + 2) * sizeof(char *));
*argv = (char *)path.data();
// build argument list
req::vector<String> sargs; // holding those char *
auto const size = args.size();
auto argv = req::make_raw_array<char*>(size + 2);
argv[0] = (char*)path.data();
int i = 1;
if (size) {
sargs.reserve(size);
for (ArrayIter iter(args); iter; ++iter, ++i) {
String arg = iter.second().toString();
sargs.push_back(arg);
*(argv + i) = (char *)arg.data();
argv[i] = (char*)arg.data();
}
}
*(argv + i) = NULL;
argv[i] = nullptr;

// build environment pair list
std::vector<String> senvs; // holding those char *
char **envp = build_envp(envs, senvs);
req::vector<String> senvs; // holding those char *
auto envp = build_envp(envs, senvs);
if (execve(path.c_str(), argv, envp) == -1) {
raise_warning("Error has occurred: (errno %d) %s",
errno, folly::errnoStr(errno).c_str());
Expand Down

0 comments on commit 65de5fd

Please sign in to comment.