diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2017-08-11 13:32:52 -0700 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2017-08-11 13:32:52 -0700 |
commit | fd31899f01a5e486c0f457318a5f9b4fe0915271 (patch) | |
tree | 88e9570021a1b44da41d4f385b30d5f365772df1 /src/google | |
parent | 34ebca0ca8c925a58e70d204a06e3b69b078769b (diff) |
implement remove strdup usage and implement our own
strdup is not part C or C++ standard but POXIS function. Several
compilers failed to compile strdup or generate warnings.
Diffstat (limited to 'src/google')
-rw-r--r-- | src/google/protobuf/compiler/subprocess.cc | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/google/protobuf/compiler/subprocess.cc b/src/google/protobuf/compiler/subprocess.cc index df2aeb1c..2a8688d6 100644 --- a/src/google/protobuf/compiler/subprocess.cc +++ b/src/google/protobuf/compiler/subprocess.cc @@ -52,6 +52,16 @@ namespace google { namespace protobuf { namespace compiler { +namespace { +char* portable_strdup(const char* s) { + char* ns = malloc(strlen(s) + 1); + if (ns) { + 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) { |