From d1a7a367acf7cc4ce000b6ad9eb8bc3da82d8084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:38:28 +0200 Subject: [PATCH] fix px4_getopt: add argc check for options that take an argument Fixes the following corner case: mpu9250 start -R This would return a valid result (myoptind < argc), but myoptind pointed to a NULL argument (and thus mpu9250 would crash). With this patch, px4_getopt will return '?', indicating a parser error. --- src/platforms/common/px4_getopt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/platforms/common/px4_getopt.c b/src/platforms/common/px4_getopt.c index d446a1fadc..41e8543c12 100644 --- a/src/platforms/common/px4_getopt.c +++ b/src/platforms/common/px4_getopt.c @@ -82,6 +82,10 @@ static int reorder(int argc, char **argv, const char *options) tmpidx++; if (takesarg) { + if (idx + 1 >= argc) { //Error: option takes an argument, but there is no more argument + return 1; + } + tmp_argv[tmpidx] = argv[idx + 1]; // printf("tmp_argv[%d] = %s\n", tmpidx, tmp_argv[tmpidx]); tmpidx++;