From 2a7cd392b1ea01f7ee9e873edf897ae537da06ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:26:23 +0200 Subject: [PATCH] sf0x: add argc check and fix argv index --- src/drivers/distance_sensor/sf0x/sf0x.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/drivers/distance_sensor/sf0x/sf0x.cpp b/src/drivers/distance_sensor/sf0x/sf0x.cpp index 665e004339..2960f59b1a 100644 --- a/src/drivers/distance_sensor/sf0x/sf0x.cpp +++ b/src/drivers/distance_sensor/sf0x/sf0x.cpp @@ -947,25 +947,27 @@ info() int sf0x_main(int argc, char *argv[]) { - // check for optional arguments int ch; uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING; int myoptind = 1; const char *myoptarg = nullptr; - while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'R': rotation = (uint8_t)atoi(myoptarg); - PX4_INFO("Setting distance sensor orientation to %d", (int)rotation); break; default: PX4_WARN("Unknown option!"); + return -1; } } + if (myoptind >= argc) { + goto out_error; + } + /* * Start/load the driver. */ @@ -1002,10 +1004,11 @@ sf0x_main(int argc, char *argv[]) /* * Print driver information. */ - if (!strcmp(argv[myoptind], "info") || !strcmp(argv[1], "status")) { + if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) { sf0x::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); - return PX4_ERROR; + return -1; }