From b8bb82d6c43f3f0efc0eecbd0a528a8fdcd8033c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:02:08 +0200 Subject: [PATCH 01/24] bmp280: add argc check and use px4_getopt --- src/drivers/barometer/bmp280/bmp280.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/drivers/barometer/bmp280/bmp280.cpp b/src/drivers/barometer/bmp280/bmp280.cpp index d2b969a1dec9..7d9c160f241a 100644 --- a/src/drivers/barometer/bmp280/bmp280.cpp +++ b/src/drivers/barometer/bmp280/bmp280.cpp @@ -50,7 +50,7 @@ #include #include #include -#include +#include #include #include @@ -849,11 +849,12 @@ usage() int bmp280_main(int argc, char *argv[]) { - enum BMP280_BUS busid = BMP280_BUS_ALL; + int myoptind = 1; int ch; + const char *myoptarg = nullptr; + enum BMP280_BUS busid = BMP280_BUS_ALL; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XISs")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XISs", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': busid = BMP280_BUS_I2C_EXTERNAL; @@ -861,8 +862,6 @@ bmp280_main(int argc, char *argv[]) case 'I': busid = BMP280_BUS_I2C_INTERNAL; - //PX4_ERR("not supported yet"); - //exit(1); break; case 'S': @@ -875,11 +874,16 @@ bmp280_main(int argc, char *argv[]) default: bmp280::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + bmp280::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. @@ -910,5 +914,5 @@ bmp280_main(int argc, char *argv[]) } PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); - exit(1); + return -1; } From 11e41734082c82a05fa77af703b56e17a282c0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:02:46 +0200 Subject: [PATCH 02/24] lps22hb: add argc check and use px4_getopt --- src/drivers/barometer/lps22hb/LPS22HB.hpp | 1 - src/drivers/barometer/lps22hb/lps22hb_main.cpp | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/drivers/barometer/lps22hb/LPS22HB.hpp b/src/drivers/barometer/lps22hb/LPS22HB.hpp index 9d66e35490dd..798cf90bb86c 100644 --- a/src/drivers/barometer/lps22hb/LPS22HB.hpp +++ b/src/drivers/barometer/lps22hb/LPS22HB.hpp @@ -48,7 +48,6 @@ #include #include -#include static constexpr uint8_t WHO_AM_I = 0x0F; static constexpr uint8_t LPS22HB_ID_WHO_AM_I = 0xB1; diff --git a/src/drivers/barometer/lps22hb/lps22hb_main.cpp b/src/drivers/barometer/lps22hb/lps22hb_main.cpp index 1e6259607661..f503da5d48a0 100644 --- a/src/drivers/barometer/lps22hb/lps22hb_main.cpp +++ b/src/drivers/barometer/lps22hb/lps22hb_main.cpp @@ -33,6 +33,8 @@ #include "LPS22HB.hpp" +#include + #include extern "C" __EXPORT int lps22hb_main(int argc, char *argv[]); @@ -234,10 +236,13 @@ usage() int lps22hb_main(int argc, char *argv[]) { - enum LPS22HB_BUS busid = LPS22HB_BUS_ALL; + int myoptind = 1; int ch; + const char *myoptarg = nullptr; + + enum LPS22HB_BUS busid = LPS22HB_BUS_ALL; - while ((ch = getopt(argc, argv, "XIS:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XIS:", &myoptind, &myoptarg)) != EOF) { switch (ch) { #if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_HMC) @@ -256,11 +261,16 @@ lps22hb_main(int argc, char *argv[]) default: lps22hb::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + lps22hb::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. From 3b099512b1870a477254f962b018019487e6f144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:03:10 +0200 Subject: [PATCH 03/24] ms5611: add argc check --- src/drivers/barometer/ms5611/ms5611.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/drivers/barometer/ms5611/ms5611.cpp b/src/drivers/barometer/ms5611/ms5611.cpp index a5285e9b6d98..bcc861d46d2e 100644 --- a/src/drivers/barometer/ms5611/ms5611.cpp +++ b/src/drivers/barometer/ms5611/ms5611.cpp @@ -50,7 +50,6 @@ #include #include #include -#include #include #include @@ -1198,10 +1197,15 @@ ms5611_main(int argc, char *argv[]) default: ms5611::usage(); - exit(0); + return 0; } } + if (myoptind >= argc) { + ms5611::usage(); + return -1; + } + const char *verb = argv[myoptind]; /* @@ -1232,5 +1236,6 @@ ms5611_main(int argc, char *argv[]) ms5611::info(); } - errx(1, "unrecognised command, try 'start', 'test', 'reset' or 'info'"); + PX4_ERR("unrecognised command, try 'start', 'test', 'reset' or 'info'"); + return -1; } From 14b3c0bc1ac5fb057b7aa7995f358a2d350be6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:03:44 +0200 Subject: [PATCH 04/24] ets_airspeed: add argc check and use px4_getopt --- .../ets/ets_airspeed.cpp | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/drivers/differential_pressure/ets/ets_airspeed.cpp b/src/drivers/differential_pressure/ets/ets_airspeed.cpp index 3f05c60026d0..ca070b9fa69a 100644 --- a/src/drivers/differential_pressure/ets/ets_airspeed.cpp +++ b/src/drivers/differential_pressure/ets/ets_airspeed.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -357,38 +358,48 @@ ets_airspeed_main(int argc, char *argv[]) { int i2c_bus = PX4_I2C_BUS_DEFAULT; - int i; + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bus") == 0) { - if (argc > i + 1) { - i2c_bus = atoi(argv[i + 1]); - } + while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + case 'b': + i2c_bus = atoi(myoptarg); + break; + + default: + ets_airspeed_usage(); + return 0; } } + if (myoptind >= argc) { + ets_airspeed_usage(); + return -1; + } + /* * Start/load the driver. */ - if (!strcmp(argv[1], "start")) { + if (!strcmp(argv[myoptind], "start")) { return ets_airspeed::start(i2c_bus); } /* * Stop the driver */ - if (!strcmp(argv[1], "stop")) { + if (!strcmp(argv[myoptind], "stop")) { return ets_airspeed::stop(); } /* * Reset the driver. */ - if (!strcmp(argv[1], "reset")) { + if (!strcmp(argv[myoptind], "reset")) { return ets_airspeed::reset(); } ets_airspeed_usage(); - - return PX4_OK; + return 0; } From 4ea774eadd8994e3141a2969de07b60746655ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:24:38 +0200 Subject: [PATCH 05/24] ms4525: add argc check and use px4_getopt --- .../ms4525/ms4525_airspeed.cpp | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp b/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp index 3c087a60017e..e944555f0328 100644 --- a/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp +++ b/src/drivers/differential_pressure/ms4525/ms4525_airspeed.cpp @@ -50,6 +50,7 @@ */ #include +#include #include @@ -493,38 +494,48 @@ ms4525_airspeed_main(int argc, char *argv[]) { int i2c_bus = PX4_I2C_BUS_DEFAULT; - int i; + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bus") == 0) { - if (argc > i + 1) { - i2c_bus = atoi(argv[i + 1]); - } + while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + case 'b': + i2c_bus = atoi(myoptarg); + break; + + default: + meas_airspeed_usage(); + return 0; } } + if (myoptind >= argc) { + meas_airspeed_usage(); + return -1; + } + /* * Start/load the driver. */ - if (!strcmp(argv[1], "start")) { + if (!strcmp(argv[myoptind], "start")) { return meas_airspeed::start(i2c_bus); } /* * Stop the driver */ - if (!strcmp(argv[1], "stop")) { + if (!strcmp(argv[myoptind], "stop")) { return meas_airspeed::stop(); } /* * Reset the driver. */ - if (!strcmp(argv[1], "reset")) { + if (!strcmp(argv[myoptind], "reset")) { return meas_airspeed::reset(); } meas_airspeed_usage(); - - return PX4_OK; + return 0; } From 5c372046e0da262b3cb134152371707aed27b1db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:24:48 +0200 Subject: [PATCH 06/24] ms5525: add argc check and use px4_getopt --- .../ms5525/MS5525_main.cpp | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/drivers/differential_pressure/ms5525/MS5525_main.cpp b/src/drivers/differential_pressure/ms5525/MS5525_main.cpp index 420fe7453e52..5f17707791a6 100644 --- a/src/drivers/differential_pressure/ms5525/MS5525_main.cpp +++ b/src/drivers/differential_pressure/ms5525/MS5525_main.cpp @@ -33,6 +33,8 @@ #include "MS5525.hpp" +#include + // Driver 'main' command. extern "C" __EXPORT int ms5525_airspeed_main(int argc, char *argv[]); @@ -151,36 +153,48 @@ ms5525_airspeed_main(int argc, char *argv[]) { uint8_t i2c_bus = PX4_I2C_BUS_DEFAULT; - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bus") == 0) { - if (argc > i + 1) { - i2c_bus = atoi(argv[i + 1]); - } + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + + while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + case 'b': + i2c_bus = atoi(myoptarg); + break; + + default: + ms5525_airspeed_usage(); + return 0; } } + if (myoptind >= argc) { + ms5525_airspeed_usage(); + return -1; + } + /* * Start/load the driver. */ - if (!strcmp(argv[1], "start")) { + if (!strcmp(argv[myoptind], "start")) { return ms5525_airspeed::start(i2c_bus); } /* * Stop the driver */ - if (!strcmp(argv[1], "stop")) { + if (!strcmp(argv[myoptind], "stop")) { return ms5525_airspeed::stop(); } /* * Reset the driver. */ - if (!strcmp(argv[1], "reset")) { + if (!strcmp(argv[myoptind], "reset")) { return ms5525_airspeed::reset(); } ms5525_airspeed_usage(); - - return PX4_OK; + return 0; } From b77c4574d48f484af3f2859f52aed4ceec3aaac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:25:00 +0200 Subject: [PATCH 07/24] sdp3x: add argc check and use px4_getopt --- .../sdp3x/SDP3X_main.cpp | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/drivers/differential_pressure/sdp3x/SDP3X_main.cpp b/src/drivers/differential_pressure/sdp3x/SDP3X_main.cpp index b15159a183da..f49cb89e5bf9 100644 --- a/src/drivers/differential_pressure/sdp3x/SDP3X_main.cpp +++ b/src/drivers/differential_pressure/sdp3x/SDP3X_main.cpp @@ -32,6 +32,7 @@ ****************************************************************************/ #include "SDP3X.hpp" +#include // Driver 'main' command. extern "C" __EXPORT int sdp3x_airspeed_main(int argc, char *argv[]); @@ -164,36 +165,49 @@ sdp3x_airspeed_main(int argc, char *argv[]) { uint8_t i2c_bus = PX4_I2C_BUS_DEFAULT; - for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--bus") == 0) { - if (argc > i + 1) { - i2c_bus = atoi(argv[i + 1]); - } + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + + while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) { + switch (ch) { + case 'b': + i2c_bus = atoi(myoptarg); + break; + + default: + sdp3x_airspeed_usage(); + return 0; } } + if (myoptind >= argc) { + sdp3x_airspeed_usage(); + return -1; + } + + /* * Start/load the driver. */ - if (!strcmp(argv[1], "start")) { + if (!strcmp(argv[myoptind], "start")) { return sdp3x_airspeed::start(i2c_bus); } /* * Stop the driver */ - if (!strcmp(argv[1], "stop")) { + if (!strcmp(argv[myoptind], "stop")) { return sdp3x_airspeed::stop(); } /* * Reset the driver. */ - if (!strcmp(argv[1], "reset")) { + if (!strcmp(argv[myoptind], "reset")) { return sdp3x_airspeed::reset(); } sdp3x_airspeed_usage(); - - return PX4_OK; + return 0; } From 335297168fac55307b3102204b0caa3fe7a9324d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:25:50 +0200 Subject: [PATCH 08/24] mb12xx: add argc check and fix argv index --- src/drivers/distance_sensor/mb12xx/mb12xx.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/drivers/distance_sensor/mb12xx/mb12xx.cpp b/src/drivers/distance_sensor/mb12xx/mb12xx.cpp index b6719be6ab6d..b4903dd4061d 100644 --- a/src/drivers/distance_sensor/mb12xx/mb12xx.cpp +++ b/src/drivers/distance_sensor/mb12xx/mb12xx.cpp @@ -879,25 +879,27 @@ info() int mb12xx_main(int argc, char *argv[]) { - // check for optional arguments int ch; int myoptind = 1; const char *myoptarg = nullptr; uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING; - 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. */ @@ -929,10 +931,11 @@ mb12xx_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")) { mb12xx::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); return PX4_ERROR; } From bd63ed59769475410742cfe7633ef0037b34a064 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 09/24] 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 665e004339df..2960f59b1a42 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; } From da6c2f73dfe345ca2d115ed9b2abbe3ceb1ae2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:26:45 +0200 Subject: [PATCH 10/24] sf1xx: add argc check --- src/drivers/distance_sensor/sf1xx/sf1xx.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/drivers/distance_sensor/sf1xx/sf1xx.cpp b/src/drivers/distance_sensor/sf1xx/sf1xx.cpp index e4156c3c05a8..1c1fd1eeace9 100644 --- a/src/drivers/distance_sensor/sf1xx/sf1xx.cpp +++ b/src/drivers/distance_sensor/sf1xx/sf1xx.cpp @@ -816,25 +816,27 @@ info() int sf1xx_main(int argc, char *argv[]) { - // check for optional arguments int ch; int myoptind = 1; const char *myoptarg = nullptr; uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING; - 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. */ @@ -870,6 +872,7 @@ sf1xx_main(int argc, char *argv[]) sf1xx::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); - return PX4_ERROR; + return -1; } From 5b6009aabeaf9a253c65ab4aa09d0c9c5a89c72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:27:00 +0200 Subject: [PATCH 11/24] srf02: add argc check --- src/drivers/distance_sensor/srf02/srf02.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/drivers/distance_sensor/srf02/srf02.cpp b/src/drivers/distance_sensor/srf02/srf02.cpp index 90f7792aa812..e73149971e58 100644 --- a/src/drivers/distance_sensor/srf02/srf02.cpp +++ b/src/drivers/distance_sensor/srf02/srf02.cpp @@ -882,25 +882,27 @@ info() int srf02_main(int argc, char *argv[]) { - // check for optional arguments int ch; int myoptind = 1; const char *myoptarg = nullptr; uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING; - 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. */ @@ -936,6 +938,7 @@ srf02_main(int argc, char *argv[]) srf02::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); return PX4_ERROR; } From 01fde139daca489e551ea0c9a71693fdf7a2f9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:27:33 +0200 Subject: [PATCH 12/24] teraranger: add argc check and fix argv index --- src/drivers/distance_sensor/teraranger/teraranger.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/drivers/distance_sensor/teraranger/teraranger.cpp b/src/drivers/distance_sensor/teraranger/teraranger.cpp index 5b23479a80ac..1552fa801f81 100644 --- a/src/drivers/distance_sensor/teraranger/teraranger.cpp +++ b/src/drivers/distance_sensor/teraranger/teraranger.cpp @@ -950,14 +950,18 @@ teraranger_main(int argc, char *argv[]) switch (ch) { case 'R': rotation = (uint8_t)atoi(myoptarg); - PX4_INFO("Setting sensor orientation to %d", (int)rotation); break; default: PX4_WARN("Unknown option!"); + return -1; } } + if (myoptind >= argc) { + goto out_error; + } + /* * Start/load the driver. */ @@ -989,10 +993,11 @@ teraranger_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")) { teraranger::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); - return PX4_ERROR; + return -1; } From 84421afa7e350fcb70907b63e0812b7b83860a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:28:30 +0200 Subject: [PATCH 13/24] tfmini: add argc check and fix argv index --- src/drivers/distance_sensor/tfmini/tfmini.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/drivers/distance_sensor/tfmini/tfmini.cpp b/src/drivers/distance_sensor/tfmini/tfmini.cpp index b0fe162fc5a7..383f0a8de57b 100644 --- a/src/drivers/distance_sensor/tfmini/tfmini.cpp +++ b/src/drivers/distance_sensor/tfmini/tfmini.cpp @@ -922,31 +922,32 @@ usage() int tfmini_main(int argc, char *argv[]) { - // check for optional arguments int ch; uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING; const char *device_path = ""; int myoptind = 1; const char *myoptarg = nullptr; - while ((ch = px4_getopt(argc, argv, "R:d:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'R': rotation = (uint8_t)atoi(myoptarg); - PX4_INFO("Setting distance sensor orientation to %d", (int)rotation); break; case 'd': device_path = myoptarg; - PX4_INFO("Using device path '%s'", device_path); break; default: PX4_WARN("Unknown option!"); + return -1; } } + if (myoptind >= argc) { + goto out_error; + } + /* * Start/load the driver. */ @@ -957,7 +958,7 @@ tfmini_main(int argc, char *argv[]) } else { PX4_WARN("Please specify device path!"); tfmini::usage(); - return PX4_ERROR; + return -1; } } @@ -985,10 +986,11 @@ tfmini_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")) { tfmini::info(); } +out_error: PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'"); - return PX4_ERROR; + return -1; } From 82ba0be8904c39956161d12daf0df3844f171c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:28:52 +0200 Subject: [PATCH 14/24] bma180: add argc check --- src/drivers/imu/bma180/bma180.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/drivers/imu/bma180/bma180.cpp b/src/drivers/imu/bma180/bma180.cpp index 58431f46cded..6fd2bdb4aadf 100644 --- a/src/drivers/imu/bma180/bma180.cpp +++ b/src/drivers/imu/bma180/bma180.cpp @@ -890,9 +890,12 @@ info() int bma180_main(int argc, char *argv[]) { + if (argc < 2) { + goto out_error; + } + /* * Start/load the driver. - */ if (!strcmp(argv[1], "start")) { bma180::start(); @@ -919,5 +922,6 @@ bma180_main(int argc, char *argv[]) bma180::info(); } +out_error: errx(1, "unrecognised command, try 'start', 'test', 'reset' or 'info'"); } From 35c72ea091ce9423bd9fec7c1a4bd9d95f49f8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:29:14 +0200 Subject: [PATCH 15/24] bmi160: add argc check and use px4_getopt --- src/drivers/imu/bmi160/bmi160_main.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/drivers/imu/bmi160/bmi160_main.cpp b/src/drivers/imu/bmi160/bmi160_main.cpp index a8c4f1062b09..04ba7fdd454e 100644 --- a/src/drivers/imu/bmi160/bmi160_main.cpp +++ b/src/drivers/imu/bmi160/bmi160_main.cpp @@ -2,6 +2,7 @@ #include "bmi160.hpp" #include +#include /** driver 'main' command */ extern "C" { __EXPORT int bmi160_main(int argc, char *argv[]); } @@ -271,32 +272,37 @@ usage() int bmi160_main(int argc, char *argv[]) { - bool external_bus = false; + int myoptind = 1; int ch; + const char *myoptarg = nullptr; + bool external_bus = false; enum Rotation rotation = ROTATION_NONE; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XR:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XR:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': external_bus = true; break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; default: bmi160::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + bmi160::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. - */ if (!strcmp(verb, "start")) { bmi160::start(external_bus, rotation); @@ -339,5 +345,5 @@ bmi160_main(int argc, char *argv[]) } bmi160::usage(); - exit(1); + return -1; } From c2f7573944736dec7fb237be2afc01ca309140df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:30:21 +0200 Subject: [PATCH 16/24] mpu6000: add argc check and use px4_getopt --- src/drivers/imu/mpu6000/mpu6000.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/drivers/imu/mpu6000/mpu6000.cpp b/src/drivers/imu/mpu6000/mpu6000.cpp index e408c6a0374c..a655de13f7bd 100644 --- a/src/drivers/imu/mpu6000/mpu6000.cpp +++ b/src/drivers/imu/mpu6000/mpu6000.cpp @@ -71,7 +71,7 @@ #include #include #include -#include +#include #include #include @@ -2590,14 +2590,16 @@ usage() int mpu6000_main(int argc, char *argv[]) { + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + enum MPU6000_BUS busid = MPU6000_BUS_ALL; int device_type = MPU_DEVICE_TYPE_MPU6000; - int ch; enum Rotation rotation = ROTATION_NONE; int accel_range = MPU6000_ACCEL_DEFAULT_RANGE_G; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "T:XISsZzR:a:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "T:XISsZzR:a:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': busid = MPU6000_BUS_I2C_EXTERNAL; @@ -2624,28 +2626,32 @@ mpu6000_main(int argc, char *argv[]) break; case 'T': - device_type = atoi(optarg); + device_type = atoi(myoptarg); break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; case 'a': - accel_range = atoi(optarg); + accel_range = atoi(myoptarg); break; default: mpu6000::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + mpu6000::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. - */ if (!strcmp(verb, "start")) { mpu6000::start(busid, rotation, accel_range, device_type); @@ -2692,5 +2698,5 @@ mpu6000_main(int argc, char *argv[]) } mpu6000::usage(); - exit(1); + return -1; } From e3e8e980541b0620a04d91fcbc9bea4b99210440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:30:47 +0200 Subject: [PATCH 17/24] mpu9250: add argc check and use px4_getopt --- src/drivers/imu/mpu9250/main.cpp | 26 +++++++++++++++----------- src/drivers/imu/mpu9250/mpu9250.cpp | 1 - 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/drivers/imu/mpu9250/main.cpp b/src/drivers/imu/mpu9250/main.cpp index 73c047d42563..72151f694dfb 100644 --- a/src/drivers/imu/mpu9250/main.cpp +++ b/src/drivers/imu/mpu9250/main.cpp @@ -51,7 +51,7 @@ #include #include #include -#include +#include #include #include @@ -479,13 +479,14 @@ usage() int mpu9250_main(int argc, char *argv[]) { - enum MPU9250_BUS busid = MPU9250_BUS_ALL; + int myoptind = 1; int ch; - bool external = false; + const char *myoptarg = nullptr; + + enum MPU9250_BUS busid = MPU9250_BUS_ALL; enum Rotation rotation = ROTATION_NONE; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XISstR:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XISstR:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': busid = MPU9250_BUS_I2C_EXTERNAL; @@ -508,22 +509,25 @@ mpu9250_main(int argc, char *argv[]) break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; default: mpu9250::usage(); - exit(0); + return 0; } } - external = (busid == MPU9250_BUS_I2C_EXTERNAL || busid == MPU9250_BUS_SPI_EXTERNAL); + if (myoptind >= argc) { + mpu9250::usage(); + return -1; + } - const char *verb = argv[optind]; + bool external = busid == MPU9250_BUS_I2C_EXTERNAL || busid == MPU9250_BUS_SPI_EXTERNAL; + const char *verb = argv[myoptind]; /* * Start/load the driver. - */ if (!strcmp(verb, "start")) { mpu9250::start(busid, rotation, external); @@ -566,5 +570,5 @@ mpu9250_main(int argc, char *argv[]) } mpu9250::usage(); - exit(1); + return 0; } diff --git a/src/drivers/imu/mpu9250/mpu9250.cpp b/src/drivers/imu/mpu9250/mpu9250.cpp index c66b3db433a4..0d5b6b715a7d 100644 --- a/src/drivers/imu/mpu9250/mpu9250.cpp +++ b/src/drivers/imu/mpu9250/mpu9250.cpp @@ -57,7 +57,6 @@ #include #include #include -#include #include #include From a75b0ff3550a94b74726c064e7801232a43893a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:31:02 +0200 Subject: [PATCH 18/24] bmm150: add argc check and use px4_getopt --- src/drivers/magnetometer/bmm150/bmm150.cpp | 21 ++++++++++++++------- src/drivers/magnetometer/bmm150/bmm150.hpp | 1 - 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/drivers/magnetometer/bmm150/bmm150.cpp b/src/drivers/magnetometer/bmm150/bmm150.cpp index 590b64e5383f..c8f67523b6a2 100644 --- a/src/drivers/magnetometer/bmm150/bmm150.cpp +++ b/src/drivers/magnetometer/bmm150/bmm150.cpp @@ -37,6 +37,7 @@ */ #include "bmm150.hpp" +#include /** driver 'main' command */ extern "C" { __EXPORT int bmm150_main(int argc, char *argv[]); } @@ -1128,28 +1129,34 @@ BMM150::print_registers() int bmm150_main(int argc, char *argv[]) { - bool external_bus = false; + int myoptind = 1; int ch; + const char *myoptarg = nullptr; + bool external_bus = false; enum Rotation rotation = ROTATION_NONE; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XR:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XR:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': external_bus = true; break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; default: bmm150::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + bmm150::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. @@ -1189,5 +1196,5 @@ bmm150_main(int argc, char *argv[]) bmm150::usage(); - exit(1); + return -1; } diff --git a/src/drivers/magnetometer/bmm150/bmm150.hpp b/src/drivers/magnetometer/bmm150/bmm150.hpp index d0f3a02c617c..6683a74d873e 100644 --- a/src/drivers/magnetometer/bmm150/bmm150.hpp +++ b/src/drivers/magnetometer/bmm150/bmm150.hpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include From 896c511df977b8438551997cdef4ff901bf06b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:31:33 +0200 Subject: [PATCH 19/24] lis3mdl: add argc check and use px4_getopt --- src/drivers/magnetometer/lis3mdl/lis3mdl.h | 1 - .../magnetometer/lis3mdl/lis3mdl_main.cpp | 36 +++++++++++-------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/drivers/magnetometer/lis3mdl/lis3mdl.h b/src/drivers/magnetometer/lis3mdl/lis3mdl.h index 643a79fdc86d..a3282243f8ae 100644 --- a/src/drivers/magnetometer/lis3mdl/lis3mdl.h +++ b/src/drivers/magnetometer/lis3mdl/lis3mdl.h @@ -40,7 +40,6 @@ #pragma once #include -#include #include #include diff --git a/src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp b/src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp index d2dad688ae95..624468397fbb 100644 --- a/src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp +++ b/src/drivers/magnetometer/lis3mdl/lis3mdl_main.cpp @@ -38,6 +38,7 @@ */ #include "lis3mdl_main.h" +#include /** * Driver 'main' command. @@ -295,16 +296,18 @@ lis3mdl::usage() int lis3mdl_main(int argc, char *argv[]) { - int index = 0; - bool calibrate = false; + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + bool calibrate = false; enum LIS3MDL_BUS bus_id = LIS3MDL_BUS_ALL; enum Rotation rotation = ROTATION_NONE; - while ((index = getopt(argc, argv, "XISR:CT")) != EOF) { - switch (index) { + while ((ch = px4_getopt(argc, argv, "XISR:CT", &myoptind, &myoptarg)) != EOF) { + switch (ch) { case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; #if (PX4_I2C_BUS_ONBOARD || PX4_SPIDEV_LIS) @@ -331,10 +334,15 @@ lis3mdl_main(int argc, char *argv[]) } } - const char *arg = argv[optind]; + if (myoptind >= argc) { + lis3mdl::usage(); + return -1; + } + + const char *verb = argv[myoptind]; // Start/load the driver - if (!strcmp(arg, "start")) { + if (!strcmp(verb, "start")) { if (lis3mdl::start(bus_id, rotation)) { if (calibrate) { @@ -354,28 +362,28 @@ lis3mdl_main(int argc, char *argv[]) } // Stop the driver - if (!strcmp(arg, "stop")) { + if (!strcmp(verb, "stop")) { return lis3mdl::stop(); } // Test the driver/device - if (!strcmp(arg, "test")) { + if (!strcmp(verb, "test")) { return lis3mdl::test(bus_id); } // Reset the driver - if (!strcmp(arg, "reset")) { + if (!strcmp(verb, "reset")) { return lis3mdl::reset(bus_id); } // Print driver information - if (!strcmp(arg, "info") || - !strcmp(arg, "status")) { + if (!strcmp(verb, "info") || + !strcmp(verb, "status")) { return lis3mdl::info(bus_id); } // Autocalibrate the scaling - if (!strcmp(arg, "calibrate")) { + if (!strcmp(verb, "calibrate")) { if (lis3mdl::calibrate(bus_id) == 0) { PX4_INFO("calibration successful"); return 0; @@ -402,4 +410,4 @@ lis3mdl::lis3mdl_bus_option &lis3mdl::find_bus(LIS3MDL_BUS bus_id) } errx(1, "bus %u not started", (unsigned)bus_id); -} \ No newline at end of file +} From b4c7f3f7725beb719be716cfebb9b6b792ee8ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:31:52 +0200 Subject: [PATCH 20/24] pwm_input: add argc check --- src/drivers/pwm_input/pwm_input.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/drivers/pwm_input/pwm_input.cpp b/src/drivers/pwm_input/pwm_input.cpp index 025aa1f46831..903f5ec78abf 100644 --- a/src/drivers/pwm_input/pwm_input.cpp +++ b/src/drivers/pwm_input/pwm_input.cpp @@ -267,6 +267,7 @@ static void pwmin_start(); static void pwmin_info(void); static void pwmin_test(void); static void pwmin_reset(void); +static void pwmin_usage(void); static PWMIN *g_dev; @@ -647,12 +648,21 @@ static void pwmin_info(void) exit(0); } +static void pwmin_usage() +{ + PX4_ERR("unrecognized command, try 'start', 'info', 'reset' or 'test'"); +} /* * driver entry point */ int pwm_input_main(int argc, char *argv[]) { + if (argc < 2) { + pwmin_usage(); + return -1; + } + const char *verb = argv[1]; /* @@ -683,6 +693,6 @@ int pwm_input_main(int argc, char *argv[]) pwmin_reset(); } - errx(1, "unrecognized command, try 'start', 'info', 'reset' or 'test'"); - return 0; + pwmin_usage(); + return -1; } From 9c8ebdea8afc71fcfeb9201539e3b6d81ce74a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:32:09 +0200 Subject: [PATCH 21/24] iridiumsbd: add argc check --- src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp b/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp index 1abef3a4c748..79493c6cb8ca 100644 --- a/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp +++ b/src/drivers/telemetry/iridiumsbd/IridiumSBD.cpp @@ -1076,6 +1076,10 @@ int IridiumSBD::close_last(struct file *filep) int iridiumsbd_main(int argc, char *argv[]) { + if (argc < 2) { + goto out_error; + } + if (!strcmp(argv[1], "start")) { return IridiumSBD::start(argc, argv); @@ -1091,6 +1095,7 @@ int iridiumsbd_main(int argc, char *argv[]) return OK; } +out_error: PX4_INFO("usage: iridiumsbd {start|stop|status|test} [-d uart_device]"); return PX4_ERROR; From 93c4a2e7601026b0c723e81d27e0533ab30ee50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:32:25 +0200 Subject: [PATCH 22/24] test_ppm: add argc check --- src/drivers/test_ppm/test_ppm.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/drivers/test_ppm/test_ppm.cpp b/src/drivers/test_ppm/test_ppm.cpp index ba1445a36ce3..f9fc7997fecb 100644 --- a/src/drivers/test_ppm/test_ppm.cpp +++ b/src/drivers/test_ppm/test_ppm.cpp @@ -249,23 +249,26 @@ usage() int test_ppm_main(int argc, char *argv[]) { + if (argc < 2) { + test_ppm::usage(); + return -1; + } + const char *verb = argv[1]; unsigned channels = 7; /* * Start/load the driver. - */ - if (!strcmp(verb, "start")) { test_ppm::start(channels); - exit(0); + return 0; } if (!strcmp(verb, "stop")) { test_ppm::stop(); - exit(0); + return 0; } /* @@ -280,11 +283,9 @@ test_ppm_main(int argc, char *argv[]) unsigned value = strtol(argv[3], NULL, 0); test_ppm::set(channel, value); - exit(0); + return 0; } - - test_ppm::usage(); - exit(1); + return -1; } From 9c2060845ad604b9238d8ef05ec9cfd0ae942478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:33:09 +0200 Subject: [PATCH 23/24] position_estimator_inav: add missing return --- .../position_estimator_inav/position_estimator_inav_main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/position_estimator_inav/position_estimator_inav_main.cpp b/src/modules/position_estimator_inav/position_estimator_inav_main.cpp index d5228dcd1063..40394a5bd4eb 100644 --- a/src/modules/position_estimator_inav/position_estimator_inav_main.cpp +++ b/src/modules/position_estimator_inav/position_estimator_inav_main.cpp @@ -134,6 +134,7 @@ int position_estimator_inav_main(int argc, char *argv[]) { if (argc < 2) { usage("missing command"); + return -1; } if (!strcmp(argv[1], "start")) { From 3b7039220d8a5ea94e950561eae26a125d29e474 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 24/24] 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 d446a1fadccc..41e8543c12cb 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++;