Skip to content

Commit

Permalink
Fix memory errors arround application_path in argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
OgreTransporter committed May 29, 2020
1 parent 2d4a8d8 commit 10d7e9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
27 changes: 22 additions & 5 deletions source/app/common/argument_parser/argument_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ using namespace std;
#define OPERATING_SYSTEM "[Unknown-OS]"
#endif

#ifdef _WIN32
#define PATH_SEPERATOR '\\'
#define PATH_ZERO 0
#else
#define PATH_SEPERATOR '/'
#define PATH_ZERO 1
#endif

#define NUMBER_OF_BITS "[%d bit] ", (sizeof(void*) == 8 ? 64 : 32) ///< used for checking 64-bit O/S

argument_parser::argument_parser(bool verbose)
Expand All @@ -56,16 +64,25 @@ void argument_parser::set_options()

int argument_parser::parse(int argc, char *argv [], const char* application_text, const char* prefix_text)
{
argument_count = argc;

for (int i = 0; i < argument_count; i++)
arguments.resize(argc);
for (int i = 0; i < argc; i++)
arguments[i] = argv[i];

application_path = argv[0];
for (int i = application_path.length() - 1; i > 0; i--)
{
if (application_path[i] == PATH_SEPERATOR)
{
application_path.resize(i + PATH_ZERO);
break;
}
}

set_options();

program_options_lite::setDefaults(command_options);

const list<const char*>& argv_unhandled = program_options_lite::scanArgv(command_options, argument_count, (const char**) arguments);
const list<const char*>& argv_unhandled = program_options_lite::scanArgv(command_options, argc, (const char**)argv);

for (list<const char*>::const_iterator it = argv_unhandled.begin(); it != argv_unhandled.end(); it++)
{
Expand All @@ -85,7 +102,7 @@ int argument_parser::parse(int argc, char *argv [], const char* application_text
fprintf( stderr, "\n" );
}

printf("Executable: %s \n", get_application_path() );
printf("Executable: %s \n", get_argument(0) );
printf("Arguments: ");

for (int i = 1; i < get_argument_count(); i++)
Expand Down
12 changes: 6 additions & 6 deletions source/app/common/argument_parser/argument_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@
#define MAX_ARGC 100

#include "program_options_lite.h"
#include <vector>

class argument_parser
{
private:
char* application_path;
int argument_count;
char* arguments[MAX_ARGC];
std::string application_path;
std::vector<std::string> arguments;

protected:
program_options_lite::Options command_options;

public:
argument_parser(bool verbose = true);

const int get_argument_count() { return argument_count; }
const char* get_argument(int index) { return arguments[index]; }
const int get_argument_count() { return (const int)arguments.size(); }
const char* get_argument(int index) { return arguments[index].c_str(); }

const char* get_application_path() { return application_path; }
const char* get_application_path() { return application_path.c_str(); }

virtual int parse(int argc, char *argv [], const char* application_text = NULL, const char* prefix_text = NULL );

Expand Down

0 comments on commit 10d7e9c

Please sign in to comment.