Skip to content

Commit

Permalink
using only px4_getopt() and never the unsafe getopt()
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BazookaJoe1900 authored and bkueng committed Feb 21, 2019
1 parent 3db901b commit 922c19a
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 48 deletions.
14 changes: 11 additions & 3 deletions src/drivers/barometer/lps25h/lps25h.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
#include <uORB/uORB.h>

#include <float.h>
#include <getopt.h>
#include <px4_getopt.h>

#include "lps25h.h"

Expand Down Expand Up @@ -967,9 +967,12 @@ int
lps25h_main(int argc, char *argv[])
{
enum LPS25H_BUS busid = LPS25H_BUS_ALL;

int myoptind = 1;
int ch;
const char *myoptarg = nullptr;

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)

Expand All @@ -992,7 +995,12 @@ lps25h_main(int argc, char *argv[])
}
}

const char *verb = argv[optind];
if (myoptind >= argc) {
lps25h::usage();
exit(0);
}

const char *verb = argv[myoptind];

/*
* Start/load the driver.
Expand Down
10 changes: 7 additions & 3 deletions src/drivers/barometer/mpl3115a2/mpl3115a2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <px4_getopt.h>

#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
Expand All @@ -70,7 +70,6 @@

#include <perf/perf_counter.h>
#include <systemlib/err.h>
#include <platforms/px4_getopt.h>

#include "mpl3115a2.h"

Expand Down Expand Up @@ -897,8 +896,8 @@ int
mpl3115a2_main(int argc, char *argv[])
{
enum MPL3115A2_BUS busid = MPL3115A2_BUS_ALL;
int ch;
int myoptind = 1;
int ch;
const char *myoptarg = NULL;

/* jump over start/off/etc and look at options first */
Expand All @@ -918,6 +917,11 @@ mpl3115a2_main(int argc, char *argv[])
}
}

if (myoptind >= argc) {
mpl3115a2::usage();
exit(0);
}

const char *verb = argv[myoptind];

/*
Expand Down
11 changes: 10 additions & 1 deletion src/drivers/batt_smbus/batt_smbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*/

#include "batt_smbus.h"
#include <px4_getopt.h>

#include <stdlib.h>

Expand Down Expand Up @@ -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 myoptind = 1;
int ch;
const char *myoptarg = nullptr;

while ((ch = getopt(argc, argv, "XTRIA:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "XTRIA:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'X':
busid = BATT_SMBUS_BUS_I2C_EXTERNAL;
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/distance_sensor/ll40ls/ll40ls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <cstdlib>
#include <string.h>
#include <stdio.h>
#include <platforms/px4_getopt.h>
#include <px4_getopt.h>

#ifndef CONFIG_SCHED_WORKQUEUE
# error This requires CONFIG_SCHED_WORKQUEUE.
Expand Down
15 changes: 12 additions & 3 deletions src/drivers/imu/adis16448/adis16448.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <px4_getopt.h>

#include <perf/perf_counter.h>
#include <systemlib/err.h>
Expand Down Expand Up @@ -1780,10 +1780,13 @@ int
adis16448_main(int argc, char *argv[])
{
enum Rotation rotation = ROTATION_NONE;

int myoptind = 1;
int ch;
const char *myoptarg = nullptr;

/* start options */
while ((ch = getopt(argc, argv, "R:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'R':
rotation = (enum Rotation)atoi(optarg);
Expand All @@ -1795,7 +1798,13 @@ adis16448_main(int argc, char *argv[])
}
}

const char *verb = argv[optind];
if (myoptind >= argc) {
adis16448::usage();
return -1;
}

const char *verb = argv[myoptind];


/*
* Start/load the driver.
Expand Down
16 changes: 13 additions & 3 deletions src/drivers/imu/adis16477/ADIS16477_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include "ADIS16477.hpp"

#include <px4_getopt.h>

#define ADIS16477_DEVICE_PATH_ACCEL "/dev/adis16477_accel"
#define ADIS16477_DEVICE_PATH_GYRO "/dev/adis16477_gyro"

Expand Down Expand Up @@ -215,22 +217,30 @@ int
adis16477_main(int argc, char *argv[])
{
enum Rotation rotation = ROTATION_NONE;

int myoptind = 1;
int ch;
const char *myoptarg = nullptr;

/* start options */
while ((ch = getopt(argc, argv, "R:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'R':
rotation = (enum Rotation)atoi(optarg);
break;

default:
adis16477::usage();
exit(0);
return 0;
}
}

const char *verb = argv[optind];
if (myoptind >= argc) {
adis16477::usage();
return -1;
}

const char *verb = argv[myoptind];

/*
* Start/load the driver.
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/imu/bmi055/bmi055_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "BMI055_gyro.hpp"

#include <px4_config.h>
#include <platforms/px4_getopt.h>
#include <px4_getopt.h>

/** driver 'main' command */
extern "C" { __EXPORT int bmi055_main(int argc, char *argv[]); }
Expand Down
1 change: 0 additions & 1 deletion src/drivers/imu/bmi160/bmi160.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>

#include <perf/perf_counter.h>
#include <systemlib/err.h>
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/imu/fxas21002c/fxas21002c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#include <board_config.h>
#include <mathlib/math/filter/LowPassFilter2p.hpp>
#include <lib/conversion/rotation.h>
#include <platforms/px4_getopt.h>
#include <px4_getopt.h>
#include <systemlib/err.h>

/* SPI protocol address bits */
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/imu/fxos8701cq/fxos8701cq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
#include <board_config.h>
#include <mathlib/math/filter/LowPassFilter2p.hpp>
#include <lib/conversion/rotation.h>
#include <platforms/px4_getopt.h>
#include <px4_getopt.h>
#include <systemlib/err.h>

/* SPI protocol address bits */
Expand Down
16 changes: 12 additions & 4 deletions src/drivers/imu/lsm303d/lsm303d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <px4_getopt.h>

#include <perf/perf_counter.h>
#include <systemlib/err.h>
Expand Down Expand Up @@ -1932,12 +1932,15 @@ int
lsm303d_main(int argc, char *argv[])
{
bool external_bus = false;
int ch;
enum Rotation rotation = ROTATION_NONE;
int accel_range = 8;

int myoptind = 1;
int ch;
const char *myoptarg = nullptr;

/* jump over start/off/etc and look at options first */
while ((ch = getopt(argc, argv, "XR:a:")) != EOF) {
while ((ch = px4_getopt(argc, argv, "XR:a:", &myoptind, &myoptarg)) != EOF) {
switch (ch) {
case 'X':
external_bus = true;
Expand All @@ -1957,7 +1960,12 @@ lsm303d_main(int argc, char *argv[])
}
}

const char *verb = argv[optind];
if (myoptind >= argc) {
lsm303d::usage();
exit(0);
}

const char *verb = argv[myoptind];

/*
* Start/load the driver.
Expand Down
16 changes: 10 additions & 6 deletions src/drivers/lights/oreoled/oreoled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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 */
Expand Down
12 changes: 8 additions & 4 deletions src/drivers/lights/pca8574/pca8574.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <px4_getopt.h>

#include <nuttx/wqueue.h>

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 922c19a

Please sign in to comment.