Skip to content

Commit 135af05

Browse files
committed
Fix include scoping and use unique_ptr
This fixes the including of windows.h to limited scope. Requires some forward definition and uses unique_ptr.
1 parent dc6281e commit 135af05

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/util/piped_process.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
# include "run.h" // for Windows arg quoting
8080
# include "unicode.h" // for widen function
8181
# include <tchar.h> // library for _tcscpy function
82+
# include <util/make_unique.h>
83+
# include <windows.h>
8284
#else
8385
# include <fcntl.h> // library for fcntl function
8486
# include <poll.h> // library for poll function
@@ -185,7 +187,8 @@ piped_processt::piped_processt(const std::vector<std::string> commandvec)
185187
}
186188
// Create the child process
187189
STARTUPINFOW start_info;
188-
ZeroMemory(&proc_info, sizeof(PROCESS_INFORMATION));
190+
proc_info = util_make_unique<PROCESS_INFORMATION>();
191+
ZeroMemory(proc_info.get(), sizeof(PROCESS_INFORMATION));
189192
ZeroMemory(&start_info, sizeof(STARTUPINFOW));
190193
start_info.cb = sizeof(STARTUPINFOW);
191194
start_info.hStdError = child_std_OUT_Wr;
@@ -211,7 +214,7 @@ piped_processt::piped_processt(const std::vector<std::string> commandvec)
211214
NULL, // use parent's environment
212215
NULL, // use parent's current directory
213216
&start_info, // STARTUPINFO pointer
214-
&proc_info); // receives PROCESS_INFORMATION
217+
proc_info.get()); // receives PROCESS_INFORMATION
215218
// Close handles to the stdin and stdout pipes no longer needed by the
216219
// child process. If they are not explicitly closed, there is no way to
217220
// recognize that the child process has ended (but maybe we don't care).
@@ -298,7 +301,7 @@ piped_processt::piped_processt(const std::vector<std::string> commandvec)
298301
piped_processt::~piped_processt()
299302
{
300303
# ifdef _WIN32
301-
TerminateProcess(proc_info.hProcess, 0);
304+
TerminateProcess(proc_info->hProcess, 0);
302305
// Disconnecting the pipes also kicks the client off, it should be killed
303306
// by now, but this will also force the client off.
304307
// Note that pipes are cleaned up by Windows when all handles to the pipe
@@ -307,8 +310,8 @@ piped_processt::~piped_processt()
307310
DisconnectNamedPipe(child_std_IN_Wr);
308311
CloseHandle(child_std_OUT_Rd);
309312
CloseHandle(child_std_IN_Wr);
310-
CloseHandle(proc_info.hProcess);
311-
CloseHandle(proc_info.hThread);
313+
CloseHandle(proc_info->hProcess);
314+
CloseHandle(proc_info->hThread);
312315
# else
313316
// Close the parent side of the remaining pipes
314317
fclose(command_stream);
@@ -363,6 +366,8 @@ std::string piped_processt::receive()
363366
success = ReadFile(child_std_OUT_Rd, buff, BUFSIZE, &nbytes, NULL);
364367
#else
365368
nbytes = read(pipe_output[0], buff, BUFSIZE);
369+
// Added the status back in here to keep parity with old implementation
370+
// TODO: check which statuses are really used/needed.
366371
if(nbytes == 0) // Update if the pipe is stopped
367372
process_state = statet::STOPPED;
368373
success = nbytes > 0;

src/util/piped_process.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
#define CPROVER_UTIL_PIPED_PROCESS_H
77

88
#ifdef _WIN32
9-
# include <windows.h>
9+
# include <memory>
10+
// The below are forward declarations for Windows APIs
11+
struct _PROCESS_INFORMATION;
12+
typedef struct _PROCESS_INFORMATION PROCESS_INFORMATION;
13+
typedef void *HANDLE;
1014
#endif
1115

1216
# include <vector>
@@ -87,7 +91,7 @@ class piped_processt
8791
protected:
8892
#ifdef _WIN32
8993
// Process information handle for Windows
90-
PROCESS_INFORMATION proc_info;
94+
std::unique_ptr<PROCESS_INFORMATION> proc_info;
9195
// Handles for communication with child process
9296
HANDLE child_std_IN_Rd;
9397
HANDLE child_std_IN_Wr;

0 commit comments

Comments
 (0)