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
+10 -6
View File
@@ -54,6 +54,7 @@
#include <stdio.h>
#include <ctype.h>
#include <sys/stat.h>
#include <px4_getopt.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
@@ -1611,10 +1612,13 @@ oreoled_main(int argc, char *argv[])
int i2cdevice = -1;
int i2c_addr = OREOLED_BASE_I2C_ADDR; /* 7bit */
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
/* jump over start/off/etc and look at options first */
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'a':
i2c_addr = (int)strtol(optarg, NULL, 0);
@@ -1630,12 +1634,12 @@ oreoled_main(int argc, char *argv[])
}
}
if (optind >= argc) {
if (myoptind >= argc) {
oreoled_usage();
exit(1);
}
const char *verb = argv[optind];
const char *verb = argv[myoptind];
int ret;
@@ -2008,18 +2012,18 @@ oreoled_main(int argc, char *argv[])
}
/* check led num */
sendb.led_num = (uint8_t)strtol(argv[optind + 1], NULL, 0);
sendb.led_num = (uint8_t)strtol(argv[myoptind + 1], NULL, 0);
if (sendb.led_num > 3) {
errx(1, "led number must be between 0 ~ 3");
}
/* get bytes */
sendb.num_bytes = argc - (optind + 2);
sendb.num_bytes = argc - (myoptind + 2);
uint8_t byte_count;
for (byte_count = 0; byte_count < sendb.num_bytes; byte_count++) {
sendb.buff[byte_count] = (uint8_t)strtol(argv[byte_count + optind + 2], NULL, 0);
sendb.buff[byte_count] = (uint8_t)strtol(argv[byte_count + myoptind + 2], NULL, 0);
}
/* send bytes */
+8 -4
View File
@@ -54,6 +54,7 @@
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <px4_getopt.h>
#include <nuttx/wqueue.h>
@@ -371,10 +372,13 @@ pca8574_main(int argc, char *argv[])
int i2cdevice = -1;
int pca8574adr = ADDR; // 7bit
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
// jump over start/off/etc and look at options first
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'a':
pca8574adr = strtol(optarg, NULL, 0);
@@ -390,12 +394,12 @@ pca8574_main(int argc, char *argv[])
}
}
if (optind >= argc) {
if (myoptind >= argc) {
pca8574_usage();
exit(1);
exit(0);
}
const char *verb = argv[optind];
const char *verb = argv[myoptind];
int fd;
int ret;
+9 -4
View File
@@ -52,6 +52,7 @@
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <px4_getopt.h>
#include <nuttx/wqueue.h>
#include <drivers/drv_hrt.h>
@@ -293,10 +294,13 @@ rgbled_usage()
int
rgbled_pwm_main(int argc, char *argv[])
{
int myoptind = 1;
int ch;
const char *myoptarg = nullptr;
/* jump over start/off/etc and look at options first */
while ((ch = getopt(argc, argv, "a:b:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "a:b:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'a':
break;
@@ -310,12 +314,13 @@ rgbled_pwm_main(int argc, char *argv[])
}
}
if (optind >= argc) {
if (myoptind >= argc) {
rgbled_usage();
exit(1);
exit(0);
}
const char *verb = argv[optind];
const char *verb = argv[myoptind];
if (!strcmp(verb, "start")) {
if (g_rgbled != nullptr) {