Fix NULL reference in option parser#149
Conversation
If an option that needs an argument is given, but the argument is actually missing, the optind will increase by 1 and return, and if the option is the last option, a NULL reference occurs at setfont.c:290. This bug is fixed by returning '!' instead of '?' to main and process this condition separately. Signed-off-by: Bohai Li <lbhlbhlbh2002@icloud.com>
|
@000lbh Yeah, you're right. But why not make it simpler and reduce optind if an argument is required? --- a/src/setfont.c
+++ b/src/setfont.c
@@ -159,6 +159,7 @@ kbd_getopt(int argc, char **argv, const struct kbd_option *opts)
required_argument:
kbd_warning(0, "option '%s' requires an argument", name);
+ optind--;
return '?';
}
This will eliminate the need to create a new magic return code. |
|
Because this simple method will print two lines of warning: Edited: |
I completely forgot about the second message. Yes, if it can be avoided, then it should be avoided. Your changes totally make sense. I'll apply it tomorrow. |
If an option that needs an argument is given, but the argument is actually missing, the optind will increase by 1 and return, and if the option is the last option, a NULL reference occurs at setfont.c:290. This bug is fixed by returning '!' instead of '?' to main and process this condition separately.