Skip to content

Commit

Permalink
replace atoi, atol, atof (GPSBabel#912)
Browse files Browse the repository at this point in the history
* eliminate usage of atoi

which has possible undefined behavior.

* eliminate a couple more atoi usages.

* eliminate atol usage.

* eliminate use of atof.
  • Loading branch information
tsteven4 authored Aug 15, 2022
1 parent e43fc4b commit 1df589d
Show file tree
Hide file tree
Showing 32 changed files with 166 additions and 159 deletions.
6 changes: 3 additions & 3 deletions csv_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <cassert> // for assert
#include <cmath> // for fabs
#include <cstdlib> // for atof, strtod
#include <cstdlib> // for strtod
#include <cstring> // for strlen, strchr, strncmp, strcmp, memmove, strcpy, strcspn, strncpy

#include <QByteArray> // for QByteArray
Expand Down Expand Up @@ -322,9 +322,9 @@ ddmmdir_to_degrees(const char* ddmmdir)
// if not N or E, prepend a '-' to ddmm2degrees input
// see XT_LAT_NMEA which handles ddmm directly
if (strchr(ddmmdir, 'W') || strchr(ddmmdir, 'S')) {
return ddmm2degrees(- atof(ddmmdir));
return ddmm2degrees(- strtod(ddmmdir, nullptr));
}
return ddmm2degrees(atof(ddmmdir));
return ddmm2degrees(strtod(ddmmdir, nullptr));

}

Expand Down
1 change: 1 addition & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ int gb_ptr2int(const void* p);
void list_codecs();
void list_timezones();
QString grapheme_truncate(const QString& input, unsigned int count);
int xstrtoi(const char* str, char** str_end, int base);

/*
* From parse.c
Expand Down
12 changes: 6 additions & 6 deletions discard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <QDebug> // for QDebug
#include <QRegularExpression> // for QRegularExpression, QRegularExpression::CaseInsensitiveOption, QRegularExpressionMatch

#include <cstdlib> // for atoi, atof
#include <cstdlib> // for strtod

#include "defs.h" // for Waypoint, fatal, route_del_wpt, route_disp_all, track_del_wpt, track_disp_all, waypt_del, waypt_disp_all, route_head, rtedata, trkdata, wptdata, fix_none, fix_unknown
#include "src/core/logging.h" // for FatalMsg
Expand Down Expand Up @@ -149,29 +149,29 @@ QRegularExpression DiscardFilter::generateRegExp(const QString& glob_pattern)
void DiscardFilter::init()
{
if (hdopopt) {
hdopf = atof(hdopopt);
hdopf = strtod(hdopopt, nullptr);
} else {
hdopf = -1.0;
}

if (vdopopt) {
vdopf = atof(vdopopt);
vdopf = strtod(vdopopt, nullptr);
} else {
vdopf = -1.0;
}

if (satopt) {
satpf = atoi(satopt);
satpf = xstrtoi(satopt, nullptr, 10);
} else {
satpf = -1;
}

if (eleminopt) {
eleminpf = atoi(eleminopt);
eleminpf = xstrtoi(eleminopt, nullptr, 10);
}

if (elemaxopt) {
elemaxpf = atoi(elemaxopt);
elemaxpf = xstrtoi(elemaxopt, nullptr, 10);
}

if (nameopt) {
Expand Down
4 changes: 2 additions & 2 deletions exif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include <cmath> // for fabs, modf, copysign, round, fmax
#include <cstdint> // for uint32_t, int32_t, uint16_t, int16_t, uint8_t, INT32_MAX
#include <cstdio> // for printf, SEEK_SET, snprintf, SEEK_CUR
#include <cstdlib> // for labs, atoi
#include <cstdlib> // for labs
#include <cstring> // for memcmp, strlen
#include <type_traits> // for add_const<>::type

Expand Down Expand Up @@ -1510,7 +1510,7 @@ ExifFormat::write()
route_disp_all(nullptr, nullptr, exif_find_wpt_by_time_lambda);
waypt_disp_all(exif_find_wpt_by_time_lambda);

qint64 frame = atoi(opt_frame);
qint64 frame = xstrtoi(opt_frame, nullptr, 10);

if (exif_wpt_ref == nullptr) {
warning(MYNAME ": No point with a valid timestamp found.\n");
Expand Down
8 changes: 4 additions & 4 deletions garmin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <cmath> // for atan2, floor, sqrt
#include <csetjmp> // for setjmp
#include <cstdio> // for fprintf, fflush, snprintf, sprintf
#include <cstdlib> // for atoi, strtol
#include <cstdlib> // for strtol
#include <cstring> // for memcpy, strlen, strncpy, strchr
#include <ctime> // for time_t

Expand Down Expand Up @@ -302,13 +302,13 @@ rw_init(const QString& fname)
* If the user provided a short_length, override the calculated value.
*/
if (snlen) {
setshort_length(mkshort_handle, atoi(snlen));
setshort_length(mkshort_handle, xstrtoi(snlen, nullptr, 10));
} else {
setshort_length(mkshort_handle, receiver_short_length);
}

if (snwhiteopt) {
setshort_whitespace_ok(mkshort_handle, atoi(snwhiteopt));
setshort_whitespace_ok(mkshort_handle, xstrtoi(snwhiteopt, nullptr, 10));
}

/*
Expand Down Expand Up @@ -1012,7 +1012,7 @@ waypoint_prepare()
tx_waylist[i]->time_populated = 1;
}
if (category) {
tx_waylist[i]->category = 1 << (atoi(category) - 1);
tx_waylist[i]->category = 1 << (xstrtoi(category, nullptr, 10) - 1);
}
if (categorybits) {
tx_waylist[i]->category = categorybits;
Expand Down
8 changes: 4 additions & 4 deletions garmin_fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include <cassert> // for assert
#include <cstdio> // for snprintf, sscanf
#include <cstdlib> // for atof
#include <cstdlib> // for strtod
#include <cstring> // for strncpy

#include <QByteArray> // for QByteArray
Expand Down Expand Up @@ -216,17 +216,17 @@ garmin_fs_xml_convert(const int base_tag, int tag, const QString& qstr, Waypoint
switch (tag) {
case 1:
if (*cdatastr) {
WAYPT_SET(waypt, proximity, atof(cdatastr));
WAYPT_SET(waypt, proximity, strtod(cdatastr, nullptr));
}
break;
case 2:
if (*cdatastr) {
WAYPT_SET(waypt, temperature, atof(cdatastr));
WAYPT_SET(waypt, temperature, strtod(cdatastr, nullptr));
}
break;
case 3:
if (*cdatastr) {
WAYPT_SET(waypt, depth, atof(cdatastr));
WAYPT_SET(waypt, depth, strtod(cdatastr, nullptr));
}
break;
case 4:
Expand Down
3 changes: 1 addition & 2 deletions garmin_gpi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <cctype> // for tolower
#include <cstdint> // for uint32_t, int32_t
#include <cstdio> // for SEEK_CUR, SEEK_SET
#include <cstdlib> // for atoi
#include <cstring> // for strlen, strncmp
#include <ctime> // for time, gmtime, time_t, tm
#include <memory> // for unique_ptr
Expand Down Expand Up @@ -1337,7 +1336,7 @@ GarminGPIFormat::wr_deinit()
gbfclose(fout);

if ((opt_sleep) && !gpsbabel_testmode()) { /* don't sleep during 'testo' */
int sleep = atoi(opt_sleep);
int sleep = xstrtoi(opt_sleep, nullptr, 10);
if (sleep < 1) {
sleep = 1;
}
Expand Down
8 changes: 4 additions & 4 deletions garmin_txt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <cmath> // for fabs, floor
#include <cstdio> // for NULL, snprintf, sscanf
#include <cstdint>
#include <cstdlib> // for atoi, abs
#include <cstdlib> // for abs
#include <cstring> // for memset, strstr, strcat, strchr, strlen, strcmp, strcpy, strncpy
#include <ctime> // for gmtime, localtime, strftime

Expand Down Expand Up @@ -751,7 +751,7 @@ garmin_txt_wr_init(const QString& fname)
gtxt_flags.celsius = (toupper(*get_option_val(opt_temp, "c")) == 'C');
init_date_and_time_format();
if (opt_precision) {
precision = atoi(opt_precision);
precision = xstrtoi(opt_precision, nullptr, 10);
if (precision < 0) {
fatal(MYNAME ": Invalid precision (%s)!", opt_precision);
}
Expand Down Expand Up @@ -789,7 +789,7 @@ garmin_txt_wr_init(const QString& fname)
if (case_ignore_strcmp(opt_utc, "utc") == 0) {
utc_offs = 0;
} else {
utc_offs = atoi(opt_utc);
utc_offs = xstrtoi(opt_utc, nullptr, 10);
}
utc_offs *= (60 * 60);
gtxt_flags.utc = 1;
Expand Down Expand Up @@ -1303,7 +1303,7 @@ parse_track_waypoint(const QStringList& lineparts)
}
break;
case 9:
WAYPT_SET(wpt, course, atoi(CSTR(str)));
WAYPT_SET(wpt, course, xstrtoi(CSTR(str), nullptr, 10));
break;
}
}
Expand Down
5 changes: 2 additions & 3 deletions garmin_xt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/

#include "defs.h"
#include <cstdlib>

#define MYNAME "Garmin_XT"
#define GARMIN_XT_ELE 31500/65536
Expand Down Expand Up @@ -94,7 +93,7 @@ format_garmin_xt_rd_st_attrs(char* p_trk_name, uint8_t* p_track_color)

// get the option for the processing the track name
if (opt_trk_header) {
method = atoi(opt_trk_header);
method = xstrtoi(opt_trk_header, nullptr, 10);
// if method is out of range set to default
if ((method < 0) || (method > 1)) {
method = 0;
Expand Down Expand Up @@ -331,7 +330,7 @@ format_garmin_xt_proc_atrk()

// get the option for the processing the track name
if (opt_trk_header) {
method = atoi(opt_trk_header);
method = xstrtoi(opt_trk_header, nullptr, 10);
}

if (! track) {
Expand Down
8 changes: 4 additions & 4 deletions gdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

#include <cmath> // for fabs
#include <cstdio> // for printf, snprintf, sscanf, SEEK_SET
#include <cstdlib> // for atoi, strtol
#include <cstdlib> // for strtol
#include <cstring> // for memset, strstr, strchr, strcmp, strlen, strncpy
#include <ctime> // for strftime, tm
#include <iterator> // for next
Expand Down Expand Up @@ -1720,8 +1720,8 @@ GdbFormat::wr_init(const QString& fname)
fout = gbfopen_le(fname, "wb", MYNAME);
ftmp = gbfopen_le(nullptr, "wb", MYNAME);

gdb_category = (gdb_opt_category) ? atoi(gdb_opt_category) : 0;
gdb_ver = (gdb_opt_ver && *gdb_opt_ver) ? atoi(gdb_opt_ver) : 0;
gdb_category = (gdb_opt_category) ? xstrtoi(gdb_opt_category, nullptr, 10) : 0;
gdb_ver = (gdb_opt_ver && *gdb_opt_ver) ? xstrtoi(gdb_opt_ver, nullptr, 10) : 0;

if (gdb_category) {
if ((gdb_category < 1) || (gdb_category > 16)) {
Expand Down Expand Up @@ -1763,7 +1763,7 @@ void
GdbFormat::write()
{
if (gdb_opt_ver) {
gdb_ver = atoi(gdb_opt_ver);
gdb_ver = xstrtoi(gdb_opt_ver, nullptr, 10);
}
write_header();

Expand Down
4 changes: 2 additions & 2 deletions gpssim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ gpssim_wr_init(const QString& fname)
{
fnamestr = fname;
trk_count = 0;
splitfiles = splitfiles_opt ? atoi(splitfiles_opt) : 0;
splitfiles = splitfiles_opt ? xstrtoi(splitfiles_opt, nullptr, 10) : 0;

/* If writing to stdout, never split files */
if (0 == strcmp("-",splitfiles_opt)) {
Expand Down Expand Up @@ -168,7 +168,7 @@ gpssim_write()
fout = gbfopen(ofname, "wb", MYNAME);
}
if (wayptspd && wayptspd[0]) {
gpssim_write_spd(atof(wayptspd));
gpssim_write_spd(strtod(wayptspd, nullptr));
}
waypt_disp_all(gpssim_write_pt);
if (splitfiles) {
Expand Down
6 changes: 3 additions & 3 deletions gpx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <cmath> // for lround
#include <cstdio> // for sscanf
#include <cstdlib> // for atoi, strtod
#include <cstdlib> // for strtod
#include <cstring> // for strchr, strncpy

#include <QDate> // for QDate
Expand Down Expand Up @@ -98,7 +98,7 @@ GpxFormat::gpx_reset_short_handle()
setshort_whitespace_ok(mkshort_handle, 0);
}

setshort_length(mkshort_handle, atoi(snlen));
setshort_length(mkshort_handle, xstrtoi(snlen, nullptr, 10));
}

void
Expand Down Expand Up @@ -1635,7 +1635,7 @@ void
GpxFormat::write()
{

elevation_precision = atoi(opt_elevation_precision);
elevation_precision = xstrtoi(opt_elevation_precision, nullptr, 10);

gpx_reset_short_handle();
auto gpx_waypt_pr_lambda = [this](const Waypoint* waypointp)->void {
Expand Down
3 changes: 1 addition & 2 deletions gtrnctr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include <cstdarg> // for va_end, va_list, va_start
#include <cstdio> // for snprintf
#include <cstdlib> // for atoi
#include <iterator> // for size
#include <optional> // for optional
#include <type_traits> // for add_const<>::type
Expand Down Expand Up @@ -75,7 +74,7 @@ GtrnctrFormat::wr_init(const QString& fname)
}
}
}
gtc_course_flag = atoi(opt_course);
gtc_course_flag = xstrtoi(opt_course, nullptr, 10);
}

void
Expand Down
2 changes: 1 addition & 1 deletion interpolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <climits> // for INT_MAX
#include <cmath> // for abs, ceil, isfinite, round
#include <cstdlib> // for abs, atoi, strtod
#include <cstdlib> // for abs, strtod
#include <optional> // for optional

#include <QString> // for QString
Expand Down
2 changes: 1 addition & 1 deletion jeeps/gpslibusb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ gusb_init(const char* portname, gpsdevh** dh)
if (0 == strcmp(portname+4, "list")) {
req_unit_number = -1;
} else {
req_unit_number = atoi(portname + 4);
req_unit_number = xstrtoi(portname + 4, nullptr, 10);
}
}
return garmin_usb_scan(lud, req_unit_number);
Expand Down
2 changes: 1 addition & 1 deletion jeeps/gpsusbwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ gusb_init(const char* pname, gpsdevh** dh)
if (0 == strcmp(pname+4, "list")) {
req_unit_number = -1;
} else {
req_unit_number = atoi(pname+4);
req_unit_number = xstrtoi(pname+4, nullptr, 10);
}
}

Expand Down
12 changes: 6 additions & 6 deletions kml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <cctype> // for tolower, toupper
#include <cmath> // for fabs
#include <cstdio> // for sscanf, printf
#include <cstdlib> // for atoi, atol, atof
#include <cstdlib> // for strtod
#include <cstring> // for strcmp
#include <optional> // for optional
#include <tuple> // for tuple, make_tuple, tie
Expand Down Expand Up @@ -69,7 +69,7 @@
void KmlFormat::kml_init_color_sequencer(unsigned int steps_per_rev)
{
if (rotate_colors) {
float color_step = atof(opt_rotate_colors);
float color_step = strtod(opt_rotate_colors, nullptr);
if (color_step > 0.0f) {
// step around circle by given number of degrees for each track(route)
kml_color_sequencer.step = ((float)kml_color_limit) * 6.0f * color_step / 360.0f;
Expand Down Expand Up @@ -393,7 +393,7 @@ void KmlFormat::wr_position_init(const QString& fname)
posnfilename = fname;
posnfilenametmp = QStringLiteral("%1-").arg(fname);
realtime_positioning = 1;
max_position_points = atoi(opt_max_position_points);
max_position_points = xstrtoi(opt_max_position_points, nullptr, 10);
}

void KmlFormat::wr_deinit()
Expand Down Expand Up @@ -821,7 +821,7 @@ void KmlFormat::kml_output_point(const Waypoint* waypointp, kml_point_type pt_ty

if (export_points) {
writer->writeStartElement(QStringLiteral("Placemark"));
if (atoi(opt_labels)) {
if (xstrtoi(opt_labels, nullptr, 10)) {
writer->writeOptionalTextElement(QStringLiteral("name"), waypointp->shortname);
}
writer->writeEmptyElement(QStringLiteral("snippet"));
Expand Down Expand Up @@ -1733,8 +1733,8 @@ void KmlFormat::write()
rotate_colors = (!! opt_rotate_colors);
trackdata = (!! strcmp("0", opt_trackdata));
trackdirection = (!! strcmp("0", opt_trackdirection));
line_width = atol(opt_line_width);
precision = atol(opt_precision);
line_width = xstrtoi(opt_line_width, nullptr, 10);
precision = xstrtoi(opt_precision, nullptr, 10);

writer->writeStartDocument();

Expand Down
Loading

0 comments on commit 1df589d

Please sign in to comment.