aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/subprocess.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/subprocess.cc')
-rw-r--r--src/google/protobuf/compiler/subprocess.cc17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc
index 933450fa..2e5a89ac 100644
--- a/src/google/protobuf/compiler/subprocess.cc
+++ b/src/google/protobuf/compiler/subprocess.cc
@@ -33,6 +33,7 @@
#include <google/protobuf/compiler/subprocess.h>
#include <algorithm>
+#include <cstring>
#include <iostream>
#ifndef _WIN32
@@ -47,11 +48,20 @@
#include <google/protobuf/message.h>
#include <google/protobuf/stubs/substitute.h>
-
namespace google {
namespace protobuf {
namespace compiler {
+namespace {
+char* portable_strdup(const char* s) {
+ char* ns = (char*) malloc(strlen(s) + 1);
+ if (ns != NULL) {
+ strcpy(ns, s);
+ }
+ return ns;
+}
+} // namespace
+
#ifdef _WIN32
static void CloseHandleOrDie(HANDLE handle) {
@@ -115,7 +125,7 @@ void Subprocess::Start(const string& program, SearchMode search_mode) {
}
// CreateProcess() mutates its second parameter. WTF?
- char* name_copy = strdup(program.c_str());
+ char* name_copy = portable_strdup(program.c_str());
// Create the process.
PROCESS_INFORMATION process_info;
@@ -299,7 +309,7 @@ void Subprocess::Start(const string& program, SearchMode search_mode) {
GOOGLE_CHECK(pipe(stdin_pipe) != -1);
GOOGLE_CHECK(pipe(stdout_pipe) != -1);
- char* argv[2] = { strdup(program.c_str()), NULL };
+ char* argv[2] = { portable_strdup(program.c_str()), NULL };
child_pid_ = fork();
if (child_pid_ == -1) {
@@ -347,7 +357,6 @@ void Subprocess::Start(const string& program, SearchMode search_mode) {
bool Subprocess::Communicate(const Message& input, Message* output,
string* error) {
-
GOOGLE_CHECK_NE(child_stdin_, -1) << "Must call Start() first.";
// The "sighandler_t" typedef is GNU-specific, so define our own.