Skip to content

Commit

Permalink
Don't use std::vector<String>
Browse files Browse the repository at this point in the history
Summary:
Its not gc safe.

Also fix a leak when pre_proc_open throws or returns false.

Reviewed By: edwinsmith

Differential Revision: D5356189

fbshipit-source-id: b973475f57f6c426a6bd09e3345580abafb4fe88
  • Loading branch information
Mark Williams authored and hhvm-bot committed Jun 30, 2017
1 parent 668b54f commit 30d14ad
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions hphp/runtime/ext/std/ext_std_process.cpp
Expand Up @@ -97,21 +97,19 @@ static char* build_envp(const Array& envs) {
return envpw;
}
#else
static char **build_envp(const Array& envs, std::vector<String> &senvs) {
static char **build_envp(const Array& envs, std::vector<std::string> &senvs) {
char **envp = nullptr;
int size = envs.size();
if (size) {
envp = (char **)malloc((size + 1) * sizeof(char *));
for (ArrayIter iter(envs); iter; ++iter) {
senvs.push_back(folly::sformat("{}={}",
iter.first().toString(),
iter.second().toString()));
}
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();
for (auto& env : senvs) {
*(envp + i++) = (char *)env.data();
}
*(envp + i) = nullptr;
}
Expand Down Expand Up @@ -942,13 +940,14 @@ Variant HHVM_FUNCTION(proc_open,
return post_proc_open(cmd, pipes, enva, items, child);
}

std::vector<String> senvs; // holding those char *
auto const envp = build_envp(enva, senvs);
std::vector<std::string> senvs; // holding those char *
char** envp = nullptr;

{
/* the unix way */
Lock lock(DescriptorItem::s_mutex);
if (!pre_proc_open(descriptorspec, items)) return false;
envp = build_envp(enva, senvs);
child = fork();
if (child) {
// the parent process
Expand Down

0 comments on commit 30d14ad

Please sign in to comment.