]> git.tuebingen.mpg.de Git - lopsub.git/commit - lopsub.c
lls_parse_arg(): Avoid NULL pointer dereference.
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 13 Feb 2022 19:46:20 +0000 (20:46 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 28 Jun 2023 14:28:06 +0000 (16:28 +0200)
commit6a4c0e8fe5ba1b8060132b12d549e82dbd3411e2
treef4dcf1bff2b529a1e036b6e33108565f40f610c6
parenta927190dc47ea0048a482030dccd2981b6310530
lls_parse_arg(): Avoid NULL pointer dereference.

If a string option with multiple=false is given twice at the command
line, the second and all subsequent calls to lls_parse_arg() discard
the previous value stored in lor->value[0].

However, if the option takes an optional argument and was first
specified without argument, lor->value remains NULL because the
first call to lls_parse_arg() returned early due to the shortcut
at the beginning of the function while the second call skips the
allocation because lor->given is increased also when no argument is
given, so it equals one during the second call. Thus, the attempt
to free lor->value[0] further down in the function results in a NULL
pointer dereference.

Fix this by checking lor->value rather then lor->given. To make this
work we have to move up the code which frees the old string value.

While at it, reduce memory usage by not over-sizing the array. We
now only allocate space for idx + 1 values rather than lor->given +
1. This is different in the case mentioned above.
lopsub.c