cpp-argparse
cpp-argparse copied to clipboard
testprog.cpp: test case and fix for segfaults when using OptionGroup
Class OptionParser stored only pointers to option groups when using add_option_group(). This can lead to the case where arguments are parsed long after an OptionGroup went out of scope, leading to memory access to freed memory and often to segmentation faults (the changed testprog.cpp demonstrates this).
This can easily happen if a parser is configured in an external function
like this
auto parser = optparse::OptionParser();
setupParser(parser);
Fix: Class OptionParser now stores option groups instead of pointers to them.
Pro:
- least amount of changes to the code
Con:
- Option groups cannot be changed after they were added to the parser via add_option_group(). Should this be needed, then a bigger rewrite would be necessary where the parser uses a factory function which creates and stores new option groups and returns a pointer/reference to them.