Skip to content

Commit d2b629b

Browse files
committed
Pass commandvec by reference
This changes to pass the vector of strings for the piped_process command by reference instead of by value.
1 parent f60acac commit d2b629b

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/util/piped_process.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@
102102
# define BUFSIZE 2048
103103

104104
#ifdef _WIN32
105-
std::wstring process_windows_args(const std::vector<std::string> commandvec)
105+
std::wstring process_windows_args(const std::vector<std::string> *commandvec)
106106
{
107-
std::wstring result = widen(commandvec[0]);
108-
for(int i = 1; i < commandvec.size(); i++)
107+
std::wstring result = widen(commandvec->at(0));
108+
for(int i = 1; i < commandvec->size(); i++)
109109
{
110110
result.append(L" ");
111-
result.append(quote_windows_arg(widen(commandvec[i])));
111+
result.append(quote_windows_arg(widen(commandvec->at(i))));
112112
}
113113
return result;
114114
}
115115
#endif
116116

117-
piped_processt::piped_processt(const std::vector<std::string> commandvec)
117+
piped_processt::piped_processt(const std::vector<std::string> *commandvec)
118118
{
119119
# ifdef _WIN32
120120
// Security attributes for pipe creation
@@ -261,16 +261,16 @@ piped_processt::piped_processt(const std::vector<std::string> commandvec)
261261
// Create a char** for the arguments (all the contents of commandvec
262262
// except the first element, i.e. the command itself).
263263
char **args =
264-
reinterpret_cast<char **>(malloc((commandvec.size()) * sizeof(char *)));
264+
reinterpret_cast<char **>(malloc((commandvec->size()) * sizeof(char *)));
265265
// Add all the arguments to the args array of char *.
266266
unsigned long i = 0;
267-
while(i < commandvec.size())
267+
while(i < commandvec->size())
268268
{
269-
args[i] = strdup(commandvec[i].c_str());
269+
args[i] = strdup(commandvec->at(i).c_str());
270270
i++;
271271
}
272272
args[i] = NULL;
273-
execvp(commandvec[0].c_str(), args);
273+
execvp(commandvec->at(0).c_str(), args);
274274
// The args variable will be handled by the OS if execvp succeeds, but
275275
// if execvp fails then we should free it here (just in case the runtime
276276
// error below continues execution.)
@@ -283,7 +283,7 @@ piped_processt::piped_processt(const std::vector<std::string> commandvec)
283283
// Only reachable if execvp failed
284284
// Note that here we send to std::cerr since we are in the child process
285285
// here and this is received by the parent process.
286-
std::cerr << "Launching " << commandvec[0]
286+
std::cerr << "Launching " << commandvec->at(0)
287287
<< " failed with error: " << std::strerror(errno) << std::endl;
288288
abort();
289289
}

src/util/piped_process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class piped_processt
7878
/// Initiate a new subprocess with pipes supporting communication
7979
/// between the parent (this process) and the child.
8080
/// \param commandvec The command and arguments to create the process
81-
explicit piped_processt(const std::vector<std::string> commandvec);
81+
explicit piped_processt(const std::vector<std::string> *commandvec);
8282

8383
// Deleted due to declaring an explicit destructor and not wanting copy
8484
// constructors to be implemented.

unit/util/piped_process.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ TEST_CASE(
2222
commands.push_back("/bin/echo");
2323
commands.push_back(to_be_echoed);
2424
#endif
25-
piped_processt process(commands);
25+
piped_processt process(&commands);
2626

2727
// This is an indirect way to detect when the pipe has something. This
2828
// could (in theory) also return when there is an error, but this unit
@@ -45,7 +45,7 @@ TEST_CASE(
4545
const std::string expected_error("Launching abcde failed");
4646
commands.push_back("abcde");
4747
#endif
48-
piped_processt process(commands);
48+
piped_processt process(&commands);
4949

5050
// This is an indirect way to detect when the pipe has something. This
5151
// could (in theory) also return when there is an error, but this unit
@@ -81,7 +81,7 @@ TEST_CASE(
8181
std::chrono::steady_clock::now();
8282
{
8383
// Scope restriction to cause destruction
84-
piped_processt process(commands);
84+
piped_processt process(&commands);
8585
}
8686
std::chrono::steady_clock::time_point end_time =
8787
std::chrono::steady_clock::now();
@@ -93,7 +93,7 @@ TEST_CASE(
9393
// Currently not working under Linxu/MacOS?!
9494
// commands.push_back("sleep 6");
9595
// time_t calc = time(NULL);
96-
// piped_processt process(commands);
96+
// piped_processt process(&commands);
9797
// process.~piped_processt();
9898
// calc = time(NULL) - calc;
9999
size_t calc = 0;
@@ -112,7 +112,7 @@ TEST_CASE(
112112
std::vector<std::string> commands;
113113
commands.push_back("z3");
114114
commands.push_back("-in");
115-
piped_processt process(commands);
115+
piped_processt process(&commands);
116116

117117
REQUIRE(
118118
process.send("(echo \"hi\")\n") ==
@@ -134,7 +134,7 @@ TEST_CASE(
134134
commands.push_back("z3");
135135
commands.push_back("-in");
136136
const std::string termination_statement = "(exit)\n";
137-
piped_processt process(commands);
137+
piped_processt process(&commands);
138138

139139
REQUIRE(
140140
process.send("(echo \"hi\")\n") ==
@@ -164,7 +164,7 @@ TEST_CASE(
164164
commands.push_back("z3");
165165
commands.push_back("-in");
166166
commands.push_back("-smt2");
167-
piped_processt process(commands);
167+
piped_processt process(&commands);
168168

169169
std::string message =
170170
"(set-logic QF_LIA) (declare-const x Int) (declare-const y Int) (assert (> "
@@ -188,7 +188,7 @@ TEST_CASE(
188188
commands.push_back("z3");
189189
commands.push_back("-in");
190190
commands.push_back("-smt2");
191-
piped_processt process(commands);
191+
piped_processt process(&commands);
192192

193193
std::string statement =
194194
"(set-logic QF_LIA) (declare-const x Int) (declare-const y Int) (assert (> "
@@ -210,7 +210,7 @@ TEST_CASE(
210210
std::vector<std::string> commands;
211211
commands.push_back("z3");
212212
commands.push_back("-in");
213-
piped_processt process(commands);
213+
piped_processt process(&commands);
214214

215215
REQUIRE(
216216
process.send("(echo \"hi\")\n") ==
@@ -240,7 +240,7 @@ TEST_CASE(
240240
commands.push_back("z3");
241241
commands.push_back("-in");
242242
commands.push_back("-smt2");
243-
piped_processt process(commands);
243+
piped_processt process(&commands);
244244

245245
std::string statement =
246246
"(set-logic QF_LIA) (declare-const x Int) (declare-const y Int) (assert (> "
@@ -296,7 +296,7 @@ TEST_CASE(
296296
commands.push_back("z3");
297297
commands.push_back("-in");
298298
commands.push_back("-smt2");
299-
piped_processt process(commands);
299+
piped_processt process(&commands);
300300

301301
std::string statement =
302302
"(set-logic QF_LIA) (declare-const x Int) (declare-const y Int) (assert (> "

0 commit comments

Comments
 (0)