From 4a00f2ddb2b1f5e29e239553ae7db74795286a85 Mon Sep 17 00:00:00 2001 From: Marius Kintel Date: Wed, 6 Apr 2011 17:18:09 +0200 Subject: [PATCH] bugfix: check size of program options before indexing --- src/openscad.cc | 92 ++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 43 deletions(-) diff --git a/src/openscad.cc b/src/openscad.cc index e10f6070..54e80ae2 100644 --- a/src/openscad.cc +++ b/src/openscad.cc @@ -56,14 +56,13 @@ #define snprintf _snprintf #endif -using namespace boost; namespace po = boost::program_options; static void help(const char *progname) { fprintf(stderr, "Usage: %s [ { -s stl_file | -o off_file | -x dxf_file } [ -d deps_file ] ]\\\n" - "%*s[ -m make_command ] [ -D var=val [..] ] filename\n", - progname, int(strlen(progname))+8, ""); + "%*s[ -m make_command ] [ -D var=val [..] ] filename\n", + progname, int(strlen(progname))+8, ""); exit(1); } @@ -82,6 +81,9 @@ QString currentdir; QString examplesdir; QString librarydir; +using std::string; +using std::vector; + void handle_dep(QString filename) { if (filename.startsWith("/")) @@ -138,21 +140,20 @@ int main(int argc, char **argv) desc.add_options() ("help,h", "help message") ("version,v", "print the version") - ("s", po::value(), "stl-file") - ("o", po::value(), "off-file") - ("x", po::value(), "dxf-file") - ("d", po::value(), "deps-file") - ("m", po::value(), "make file") - ("D", po::value >(), "var=val") + ("s", po::value(), "stl-file") + ("o", po::value(), "off-file") + ("x", po::value(), "dxf-file") + ("d", po::value(), "deps-file") + ("m", po::value(), "make file") + ("D", po::value >(), "var=val") ; po::positional_options_description p; p.add("input-file", -1); po::variables_map vm; - po::store(po::command_line_parser(argc, argv). - options(desc).positional(p).run(), vm); - //po::notify(vm); + po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); +// po::notify(vm); if (vm.count("help")) help(argv[0]); if (vm.count("version")) version(); @@ -160,43 +161,44 @@ int main(int argc, char **argv) if (vm.count("s")) { if (stl_output_file || off_output_file || dxf_output_file) help(argv[0]); - stl_output_file = vm["s"].as().c_str(); + stl_output_file = vm["s"].as().c_str(); } if (vm.count("o")) { if (stl_output_file || off_output_file || dxf_output_file) help(argv[0]); - off_output_file = vm["o"].as().c_str(); + off_output_file = vm["o"].as().c_str(); } if (vm.count("x")) { if (stl_output_file || off_output_file || dxf_output_file) help(argv[0]); - dxf_output_file = vm["x"].as().c_str(); + dxf_output_file = vm["x"].as().c_str(); } if (vm.count("d")) { if (deps_output_file) help(argv[0]); - deps_output_file = vm["d"].as().c_str(); + deps_output_file = vm["d"].as().c_str(); } if (vm.count("m")) { if (make_command) help(argv[0]); - make_command = vm["m"].as().c_str(); + make_command = vm["m"].as().c_str(); } if (vm.count("D")) { - const std::vector &commands = vm["D"].as >(); + const vector &commands = vm["D"].as >(); - for (std::vector::const_iterator i = commands.begin(); i != commands.end(); i++) { + for (vector::const_iterator i = commands.begin(); i != commands.end(); i++) { commandline_commands.append(i->c_str()); commandline_commands.append(";\n"); } } - if (vm.count("input-file")) - filename = vm["input-file"].as >().begin()->c_str(); + if (vm.count("input-file")) { + filename = vm["input-file"].as< vector >().begin()->c_str(); + } #ifndef ENABLE_MDI - if (vm["input-file"].as >().size() > 1) + if (vm["input-file"].as >().size() > 1) help(argv[0]); #endif @@ -210,16 +212,16 @@ int main(int argc, char **argv) if (exdir.cd("../share/openscad/examples")) { examplesdir = exdir.path(); } else - if (exdir.cd("../../share/openscad/examples")) { - examplesdir = exdir.path(); - } else - if (exdir.cd("../../examples")) { - examplesdir = exdir.path(); - } else + if (exdir.cd("../../share/openscad/examples")) { + examplesdir = exdir.path(); + } else + if (exdir.cd("../../examples")) { + examplesdir = exdir.path(); + } else #endif - if (exdir.cd("examples")) { - examplesdir = exdir.path(); - } + if (exdir.cd("examples")) { + examplesdir = exdir.path(); + } QDir libdir(QApplication::instance()->applicationDirPath()); #ifdef Q_WS_MAC @@ -229,16 +231,16 @@ int main(int argc, char **argv) if (libdir.cd("../share/openscad/libraries")) { librarydir = libdir.path(); } else - if (libdir.cd("../../share/openscad/libraries")) { - librarydir = libdir.path(); - } else - if (libdir.cd("../../libraries")) { - librarydir = libdir.path(); - } else + if (libdir.cd("../../share/openscad/libraries")) { + librarydir = libdir.path(); + } else + if (libdir.cd("../../libraries")) { + librarydir = libdir.path(); + } else #endif - if (libdir.cd("libraries")) { - librarydir = libdir.path(); - } + if (libdir.cd("libraries")) { + librarydir = libdir.path(); + } if (stl_output_file || off_output_file || dxf_output_file) { @@ -343,9 +345,13 @@ int main(int argc, char **argv) #endif #ifdef ENABLE_MDI new MainWindow(qfilename); - std::vector inputFiles = vm["input-files"].as >(); - for (std::vector::const_iterator i = inputFiles.begin()+1; i != inputFiles.end(); i++) - new MainWindow(QFileInfo(original_path, i->c_str()).absoluteFilePath()); + vector inputFiles; + if (vm.count("input-file")) { + inputFiles = vm["input-files"].as >(); + for (vector::const_iterator i = inputFiles.begin()+1; i != inputFiles.end(); i++) { + new MainWindow(QFileInfo(original_path, i->c_str()).absoluteFilePath()); + } + } app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit())); #else MainWindow *m = new MainWindow(qfilename);