From 70189cdfe2179abf9a98547c572af5cb5c24b9aa Mon Sep 17 00:00:00 2001 From: micio Date: Mon, 24 Jan 2011 22:33:19 +0000 Subject: [PATCH] Bazaar/SysExec : fixed a nasty memory bug in ArgEnv module git-svn-id: svn://ultimatepp.org/upp/trunk@3084 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- bazaar/SysExec/ArgEnv.cpp | 221 ++++++++++++++++++------------------ bazaar/SysExec/ArgEnv.h | 36 +++--- bazaar/SysExec/ShellLib.cpp | 1 - bazaar/SysExec/SysExec.cpp | 8 +- 4 files changed, 133 insertions(+), 133 deletions(-) diff --git a/bazaar/SysExec/ArgEnv.cpp b/bazaar/SysExec/ArgEnv.cpp index ddf56263c..342144c22 100644 --- a/bazaar/SysExec/ArgEnv.cpp +++ b/bazaar/SysExec/ArgEnv.cpp @@ -1,110 +1,111 @@ -#include "ArgEnv.h" - -NAMESPACE_UPP - -/////////////////////////////////////////////////////////////////////////////////////////////// -// parses an args line to be useable by spawnxx functions -BufferBuildArgs(String const &command, String const &argline) -{ - Array args; - int pos = 0; - char c; - int buflen = 0; - - // first arg should be command name - args.Add(command); - - // skips leading spaces - while ((c = argline[pos]) != 0 && isspace(c)) - pos++; - - // loop reading args and putting to array - while (c) - { - String &s = args.Add(); - buflen++; - while (c && !isspace(c)) - { - // reads enquoted strings - if (c == '"') - { - c = argline[++pos]; - while (c && c != '"') - { - s << c; - buflen++; - c = argline[++pos]; - } - if (c) - c = argline[++pos]; - } - else - { - s << c; - buflen++; - c = argline[++pos]; - } - } - - // skips trailing spaces - while (c && isspace(c)) - c = argline[++pos]; - } - buflen += (args.GetCount() + 1) * sizeof(char *); - - // here we've got an array of args and the total size (in bytes) of them - // we allocates a buffer for arg array - Bufferbuf(buflen); - - // we fill the buffer with arg strings - char **bufindex = buf; - char *bufpos = (char *)(buf + args.GetCount() + 1); - int i = 0; - while (i < args.GetCount()) - { - String &s = args[i]; - strcpy(bufpos, (const char *)s); - *bufindex++ = bufpos; - bufpos += s.GetCount() + 1 ; - i++; - } - *bufindex = 0; - - // returns array of args - return buf; - -} // END _BuildArgs() - - -/////////////////////////////////////////////////////////////////////////////////////////////// -// parses environment map and builds env array -BufferBuildEnv(const VectorMap &env) -{ - // calculates total environment size - int envSize = 0; - for (int i = 0; i < env.GetCount(); i++) - envSize += env.GetKey(i).GetCount() + env[i].GetCount() + 2 + sizeof(char *); - - // we allocates a buffer for env array - Bufferbuf(envSize); - - // we fill the buffer with env strings - char **bufindex = buf; - char *bufpos = (char *)(buf + env.GetCount() + 1); - int i = 0; - while (i < env.GetCount()) - { - const String &s = env.GetKey(i) + "=" + env[i]; - strcpy(bufpos, (const char *)s); - *bufindex++ = bufpos; - bufpos += s.GetCount() + 1 ; - i++; - } - *bufindex = 0; - - // returns array of args - return buf; - -} // END _BuildEnv() - -END_UPP_NAMESPACE +#include "ArgEnv.h" + +NAMESPACE_UPP + +/////////////////////////////////////////////////////////////////////////////////////////////// +// parses an args line to be useable by spawnxx functions +char **BuildArgs(String const &command, String const &argline) +{ + Array args; + + // first arg should be command name + args.Add(command); + int buflen = command.GetCount() + 1; + + // skips leading spaces + char c; + int pos = 0; + while ((c = argline[pos]) != 0 && isspace(c)) + pos++; + + // loop reading args and putting to array + while (c) + { + String &s = args.Add(); + buflen++; + while (c && !isspace(c)) + { + // reads enquoted strings + if (c == '"') + { + c = argline[++pos]; + while (c && c != '"') + { + s << c; + buflen++; + c = argline[++pos]; + } + if (c) + c = argline[++pos]; + } + else + { + s << c; + buflen++; + c = argline[++pos]; + } + } + + // skips trailing spaces + while (c && isspace(c)) + c = argline[++pos]; + } + buflen += (args.GetCount() + 1) * sizeof(char *); + + // here we've got an array of args and the total size (in bytes) of them + // we allocates a buffer for arg array + char **buf = (char **)malloc(buflen); + + // we fill the buffer with arg strings + char **bufindex = buf; + char *bufpos = (char *)(buf + args.GetCount() + 1); + int i = 0; + while (i < args.GetCount()) + { + String s = args[i]; + strcpy(bufpos, ~s); + *bufindex++ = bufpos; + bufpos += s.GetCount() + 1 ; + i++; + } + *bufindex = 0; + + // returns array of args + return buf; + +} // END _BuildArgs() + + +/////////////////////////////////////////////////////////////////////////////////////////////// +// parses environment map and builds env array +char **BuildEnv(const VectorMap &env) +{ + // calculates total environment size + int envSize = 0; + for (int i = 0; i < env.GetCount(); i++) + envSize += env.GetKey(i).GetCount() + env[i].GetCount() + 2 + sizeof(char *); + envSize+=2; + + // we allocates a buffer for env array + char **buf = (char **)malloc(envSize); + + // we fill the buffer with env strings + char **bufindex = buf; + char *bufpos = (char *)(buf + env.GetCount() + 1); + int i = 0; + while (i < env.GetCount()) + { + String s = env.GetKey(i) + "=" + env[i]; + strcpy(bufpos, ~s); + *bufindex++ = bufpos; + bufpos += s.GetCount() + 1 ; + i++; + } + *bufindex = 0; + + // returns array of args + return buf; + +} // END _BuildEnv() + +END_UPP_NAMESPACE diff --git a/bazaar/SysExec/ArgEnv.h b/bazaar/SysExec/ArgEnv.h index 19424c3fd..9f5186f6f 100644 --- a/bazaar/SysExec/ArgEnv.h +++ b/bazaar/SysExec/ArgEnv.h @@ -1,18 +1,18 @@ -#ifndef _SysExec_ArgEnv_h_ -#define _SysExec_ArgEnv_h_ - -#include - -NAMESPACE_UPP - -/////////////////////////////////////////////////////////////////////////////////////////////// -// parses an args line to be useable by spawnxx functions -BufferBuildArgs(String const &command, String const &argline); - -/////////////////////////////////////////////////////////////////////////////////////////////// -// parses environment map and builds env array -BufferBuildEnv(const VectorMap &env); - -END_UPP_NAMESPACE - -#endif +#ifndef _SysExec_ArgEnv_h_ +#define _SysExec_ArgEnv_h_ + +#include + +NAMESPACE_UPP + +/////////////////////////////////////////////////////////////////////////////////////////////// +// parses an args line to be useable by spawnxx functions +char **BuildArgs(String const &command, String const &argline); + +/////////////////////////////////////////////////////////////////////////////////////////////// +// parses environment map and builds env array +char **BuildEnv(const VectorMap &env); + +END_UPP_NAMESPACE + +#endif diff --git a/bazaar/SysExec/ShellLib.cpp b/bazaar/SysExec/ShellLib.cpp index c3bf9880f..15ef0fe57 100644 --- a/bazaar/SysExec/ShellLib.cpp +++ b/bazaar/SysExec/ShellLib.cpp @@ -13,7 +13,6 @@ NAMESPACE_UPP static bool IsVistaOrLater(void) { OSVERSIONINFO osvi; - BOOL bIsWindowsXPorLater; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); diff --git a/bazaar/SysExec/SysExec.cpp b/bazaar/SysExec/SysExec.cpp index 7510860d7..1fced6e2a 100644 --- a/bazaar/SysExec/SysExec.cpp +++ b/bazaar/SysExec/SysExec.cpp @@ -48,8 +48,8 @@ bool SysExec(String const &command, String const &args, const VectorMapargv = BuildArgs(GetFileName(command), args); - Bufferenvv = BuildEnv(Environ); + char **argv = BuildArgs(GetFileName(command), args); + char **envv = BuildEnv(Environ); // executes the command int result = 0; @@ -169,8 +169,8 @@ bool SysExec(String const &command, String const &args) bool SysStart(String const &command, String const &args, const VectorMap &Environ, intptr_t *pid) { // builds the arguments and the environment - Bufferargv = BuildArgs(GetFileName(command), args); - Bufferenvv = BuildEnv(Environ); + char **argv = BuildArgs(GetFileName(command), args); + char **envv = BuildEnv(Environ); // executes the command int result = 0;