diff options
author | Matthias Huetsch [mhu] <matthias.huetsch@oracle.com> | 2010-11-25 14:24:19 +0100 |
---|---|---|
committer | Matthias Huetsch [mhu] <matthias.huetsch@oracle.com> | 2010-11-25 14:24:19 +0100 |
commit | 1bd0f73be5802157f5ccd3ba4b79ce76150d0114 (patch) | |
tree | 2339d84083c76344381ebcf0f1c2e1f057d827ab /idlc/source/options.cxx | |
parent | 0ff6b9a26dd3916e7e5f29f37ec2220b95bf5180 (diff) |
#i115784# idlc: fix memory errors uncovered by valgrind and other tools.
Diffstat (limited to 'idlc/source/options.cxx')
-rw-r--r-- | idlc/source/options.cxx | 81 |
1 files changed, 39 insertions, 42 deletions
diff --git a/idlc/source/options.cxx b/idlc/source/options.cxx index c90bce43b3bc..0c6da3bb8ec0 100644 --- a/idlc/source/options.cxx +++ b/idlc/source/options.cxx @@ -28,9 +28,10 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_idlc.hxx" +#include "idlc/options.hxx" + #include <stdio.h> -#include /*MSVC trouble: <cstring>*/ <string.h> -#include <idlc/options.hxx> +#include <string.h> using namespace rtl; @@ -226,7 +227,7 @@ sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile) exit(0); } case 's': - if (/*MSVC trouble: std::*/strcmp(&av[j][2], "tdin") == 0) + if (strcmp(&av[j][2], "tdin") == 0) { m_stdin = true; break; @@ -246,53 +247,49 @@ sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile) ret = sal_False; } else { - int rargc=0; - char* rargv[512]; - char buffer[512]=""; + std::vector< std::string > args; + + std::string buffer; + buffer.reserve(256); - int i=0; - int found = 0; - char c; - while ( fscanf(cmdFile, "%c", &c) != EOF ) + bool quoted = false; + int c = EOF; + while ((c = fgetc(cmdFile)) != EOF) { - if (c=='\"') { - if (found) { - found=0; - } else { - found=1; - continue; - } - } else { - if (c!=13 && c!=10) { - if (found || c!=' ') { - buffer[i++]=c; - continue; + switch(c) + { + case '\"': + quoted = !quoted; + break; + case ' ': + case '\t': + case '\r': + case '\n': + if (!quoted) + { + if (!buffer.empty()) + { + // append current argument. + args.push_back(buffer); + buffer.clear(); } + break; } - if (i==0) - continue; + default: + // quoted white-space fall through + buffer.push_back(sal::static_int_cast<char>(c)); + break; } - buffer[i]='\0'; - found=0; - i=0; - rargv[rargc]= strdup(buffer); - rargc++; - buffer[0]='\0'; } - if (buffer[0] != '\0') { - buffer[i]='\0'; - rargv[rargc]= strdup(buffer); - rargc++; - } - fclose(cmdFile); - - ret = initOptions(rargc, rargv, bCmdFile); - - long ii = 0; - for (ii=0; ii < rargc; ii++) + if (!buffer.empty()) { - free(rargv[ii]); + // append unterminated argument. + args.push_back(buffer); + buffer.clear(); } + (void) fclose(cmdFile); + + ret = initOptions(args.size(), args.data(), bCmdFile); } } else { |