Skip to content

Commit

Permalink
Merge pull request ElektraInitiative#4435 from hannes99/fix_possible_…
Browse files Browse the repository at this point in the history
…inv_pointer

close ElektraInitiative#4434: opts: fix possible 'invalid pointer' error
  • Loading branch information
markus2330 authored Aug 17, 2022
2 parents 14d6500 + 39a4e2e commit 63e6fc4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ The text below summarizes updates to the [C (and C++)-based libraries](https://w

### <<Library>>

- <<TODO>>
- opts: fix possible 'free(): invalid pointer' error and add test for it _(@hannes99)_
- <<TODO>>
- <<TODO>>

Expand Down
8 changes: 4 additions & 4 deletions src/libs/opts/opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,13 +889,13 @@ bool processShortOptSpec (struct Specification * spec, struct OptionData * optio

if (!hidden)
{
char * argString = "";
char * argString = NULL;
if (elektraStrCmp (hasArg, "required") == 0)
{
argString = argName == NULL ? " ARG" : elektraFormat (" %s", argName);
}

char * newShortOptLine = elektraFormat ("%s-%c%s, ", *shortOptLine, shortOpt, argString);
char * newShortOptLine = elektraFormat ("%s-%c%s, ", *shortOptLine, shortOpt, argString == NULL ? "" : argString);
elektraFree (*shortOptLine);
if (argName != NULL)
{
Expand Down Expand Up @@ -970,7 +970,7 @@ bool processLongOptSpec (struct Specification * spec, struct OptionData * option

if (!hidden)
{
char * argString = "";
char * argString = NULL;
if (elektraStrCmp (hasArg, "required") == 0)
{
argString = argName == NULL ? "=ARG" : elektraFormat ("=%s", argName);
Expand All @@ -981,7 +981,7 @@ bool processLongOptSpec (struct Specification * spec, struct OptionData * option
}


char * newLongOptLine = elektraFormat ("%s--%s%s, ", *longOptLine, longOpt, argString);
char * newLongOptLine = elektraFormat ("%s--%s%s, ", *longOptLine, longOpt, argString == NULL ? "" : argString);
elektraFree (*longOptLine);
if (argName != NULL)
{
Expand Down
18 changes: 17 additions & 1 deletion tests/ctest/test_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ static void test_help (void)
" --help Print this help message\n"
" -a, -b BANANA, -C, --apple, --banana=BANANA, --cherry=[ARG]\n"
" Apple/Banana/Cherry description\n"
" -i, -I, --interactive=[WHEN]prompt according to WHEN: never, once (-I), or always (-i), without WHEN, prompt always\n"
" -p ARG A pear is not an apple, nor a banana, nor a cherry.\n"
"\n"
"PARAMETERS\n"
Expand Down Expand Up @@ -1024,7 +1025,22 @@ static void test_help (void)
keySetMeta (k, "env/#1", "BANANA");
keySetMeta (k, "env/#2", "CHERRY");
keySetMeta (k, "description", "Apple/Banana/Cherry description");
ks = ksNew (4, k,


// example from the tutorial
Key * k2 = keyNew (SPEC_BASE_KEY "/interactive", KEY_END);
keySetMeta (k2, "description", "prompt according to WHEN: never, once (-I), or always (-i), without WHEN, prompt always");
keySetMeta (k2, "opt", "#1");
keySetMeta (k2, "opt/#0", "i");
keySetMeta (k2, "opt/#0/arg", "optional");
keySetMeta (k2, "opt/#0/flagvalue", "always");
keySetMeta (k2, "opt/#0/long", "interactive");
keySetMeta (k2, "opt/#0/arg/help", "WHEN");
keySetMeta (k2, "opt/#1", "I");
keySetMeta (k2, "opt/#1/arg", "none");
keySetMeta (k2, "opt/#1/flagvalue", "once");

ks = ksNew (5, k, k2,
keyNew (SPEC_BASE_KEY "/pear", KEY_META, "opt", "p", KEY_META, "description",
"A pear is not an apple, nor a banana, nor a cherry.", KEY_END),
keyNew (SPEC_BASE_KEY "/param1", KEY_META, "args", "indexed", KEY_META, "args/index", "0", KEY_META, "description",
Expand Down

0 comments on commit 63e6fc4

Please sign in to comment.