using only px4_getopt() and never the unsafe getopt()

using only <px4_getopt.h> as include, cleanup related includes
added check that all the option are valid (myoptind >= argc). if there are invalid options on some script that might now lead not to run commands
This commit is contained in:
bazooka joe
2019-02-21 09:45:45 +01:00
committed by Beat Küng
parent 3db901b238
commit 922c19aa9c
20 changed files with 131 additions and 50 deletions
+11 -2
View File
@@ -42,6 +42,7 @@
*/
#include "batt_smbus.h"
#include <px4_getopt.h>
#include <stdlib.h>
@@ -100,9 +101,12 @@ BATT_SMBUS::~BATT_SMBUS()
int BATT_SMBUS::task_spawn(int argc, char *argv[])
{
enum BATT_SMBUS_BUS busid = BATT_SMBUS_BUS_ALL;
int ch;
while ((ch = getopt(argc, argv, "XTRIA:")) != EOF) {
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
while ((ch = px4_getopt(argc, argv, "XTRIA:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'X':
busid = BATT_SMBUS_BUS_I2C_EXTERNAL;
@@ -130,6 +134,11 @@ int BATT_SMBUS::task_spawn(int argc, char *argv[])
}
}
if (myoptind >= argc) {
print_usage();
return PX4_ERROR;
}
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
if (!is_running() && (busid == BATT_SMBUS_BUS_ALL || bus_options[i].busid == busid)) {