Skip to content

Commit

Permalink
Fix some memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Dec 12, 2014
1 parent 28f22b4 commit f52b6c9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
38 changes: 9 additions & 29 deletions src/libstore/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,18 +415,6 @@ static void commonChildInit(Pipe & logPipe)
}


/* Convert a string list to an array of char pointers. Careful: the
string list should outlive the array. */
const char * * strings2CharPtrs(const Strings & ss)
{
const char * * arr = new const char * [ss.size() + 1];
const char * * p = arr;
foreach (Strings::const_iterator, i, ss) *p++ = i->c_str();
*p = 0;
return arr;
}


//////////////////////////////////////////////////////////////////////


Expand Down Expand Up @@ -2124,11 +2112,7 @@ void DerivationGoal::runChild()
Strings envStrs;
foreach (Environment::const_iterator, i, env)
envStrs.push_back(rewriteHashes(i->first + "=" + i->second, rewritesToTmp));
const char * * envArr = strings2CharPtrs(envStrs);

Path program = drv.builder.c_str();
std::vector<const char *> args; /* careful with c_str()! */
string user; /* must be here for its c_str()! */
auto envArr = stringsToCharPtrs(envStrs);

/* If we are running in `build-users' mode, then switch to the
user we allocated above. Make sure that we drop all root
Expand All @@ -2152,24 +2136,20 @@ void DerivationGoal::runChild()
}

/* Fill in the arguments. */
Strings args;
string builderBasename = baseNameOf(drv.builder);
args.push_back(builderBasename.c_str());
foreach (Strings::iterator, i, drv.args) {
auto re = rewriteHashes(*i, rewritesToTmp);
auto cstr = new char[re.length()+1];
std::strcpy(cstr, re.c_str());

args.push_back(cstr);
}
args.push_back(0);
args.push_back(builderBasename);
foreach (Strings::iterator, i, drv.args)
args.push_back(rewriteHashes(*i, rewritesToTmp));
auto argArr = stringsToCharPtrs(args);

restoreSIGPIPE();

/* Indicate that we managed to set up the build environment. */
writeFull(STDERR_FILENO, "\n");

/* Execute the program. This should not return. */
execve(program.c_str(), (char * *) &args[0], (char * *) envArr);
execve(drv.builder.c_str(), (char * *) &argArr[0], (char * *) &envArr[0]);

throw SysError(format("executing ‘%1%’") % drv.builder);

Expand Down Expand Up @@ -2797,7 +2777,7 @@ void SubstitutionGoal::tryToRun()
args.push_back("--substitute");
args.push_back(storePath);
args.push_back(destPath);
const char * * argArr = strings2CharPtrs(args);
auto argArr = stringsToCharPtrs(args);

/* Fork the substitute program. */
pid = startProcess([&]() {
Expand All @@ -2807,7 +2787,7 @@ void SubstitutionGoal::tryToRun()
if (dup2(outPipe.writeSide, STDOUT_FILENO) == -1)
throw SysError("cannot dup output pipe into stdout");

execv(sub.c_str(), (char * *) argArr);
execv(sub.c_str(), (char * *) &argArr[0]);

throw SysError(format("executing ‘%1%’") % sub);
});
Expand Down
19 changes: 13 additions & 6 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -912,16 +912,19 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
}


std::vector<const char *> stringsToCharPtrs(const Strings & ss)
{
std::vector<const char *> res;
for (auto & s : ss) res.push_back(s.c_str());
res.push_back(0);
return res;
}


string runProgram(Path program, bool searchPath, const Strings & args)
{
checkInterrupt();

std::vector<const char *> cargs; /* careful with c_str()! */
cargs.push_back(program.c_str());
for (Strings::const_iterator i = args.begin(); i != args.end(); ++i)
cargs.push_back(i->c_str());
cargs.push_back(0);

/* Create a pipe. */
Pipe pipe;
pipe.create();
Expand All @@ -931,6 +934,10 @@ string runProgram(Path program, bool searchPath, const Strings & args)
if (dup2(pipe.writeSide, STDOUT_FILENO) == -1)
throw SysError("dupping stdout");

Strings args_(args);
args_.push_front(program);
auto cargs = stringsToCharPtrs(args_);

if (searchPath)
execvp(program.c_str(), (char * *) &cargs[0]);
else
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ string runProgram(Path program, bool searchPath = false,

MakeError(ExecError, Error)

/* Convert a list of strings to a null-terminated vector of char
*'s. The result must not be accessed beyond the lifetime of the
list of strings. */
std::vector<const char *> stringsToCharPtrs(const Strings & ss);

/* Close all file descriptors except stdin, stdout, stderr, and those
listed in the given set. Good practice in child processes. */
void closeMostFDs(const set<int> & exceptions);
Expand Down

0 comments on commit f52b6c9

Please sign in to comment.