From 1469e88a795b77f85ed773e233a7f8b9f7b2872c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Wed, 19 Oct 2022 23:18:30 +0200 Subject: [PATCH] Replace `atof` usages with `strtod` (`str_tofloat` wrapper) As `atof` does not provide any way to detect errors and can cause undefined behavior on errors, i.e. when the result cannot be represented as a `float`. References: - https://wiki.sei.cmu.edu/confluence/display/c/ERR07-C.+Prefer+functions+that+support+error+checking+over+equivalent+functions+that+don%27t - https://pubs.opengroup.org/onlinepubs/9699919799/functions/atof.html --- src/base/system.cpp | 2 +- src/tools/map_replace_area.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/base/system.cpp b/src/base/system.cpp index a58868f7b..1a0a04e22 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -3402,7 +3402,7 @@ int str_isallnum(const char *str) int str_toint(const char *str) { return str_toint_base(str, 10); } int str_toint_base(const char *str, int base) { return strtol(str, NULL, base); } unsigned long str_toulong_base(const char *str, int base) { return strtoul(str, NULL, base); } -float str_tofloat(const char *str) { return atof(str); } +float str_tofloat(const char *str) { return strtod(str, NULL); } int str_utf8_comp_nocase(const char *a, const char *b) { diff --git a/src/tools/map_replace_area.cpp b/src/tools/map_replace_area.cpp index 3a6157a06..44d509928 100644 --- a/src/tools/map_replace_area.cpp +++ b/src/tools/map_replace_area.cpp @@ -80,10 +80,10 @@ int main(int argc, const char *argv[]) for(int i = 0; i < 2; i++) { - aaaGameAreas[i][0][0] = atof(argv[2 + i * 3]) * 32; //x - aaaGameAreas[i][1][0] = atof(argv[3 + i * 3]) * 32; //y - aaaGameAreas[i][0][1] = aaaGameAreas[i][0][0] + atof(argv[7]) * 32; //x + width - aaaGameAreas[i][1][1] = aaaGameAreas[i][1][0] + atof(argv[8]) * 32; //y + height + aaaGameAreas[i][0][0] = str_tofloat(argv[2 + i * 3]) * 32; //x + aaaGameAreas[i][1][0] = str_tofloat(argv[3 + i * 3]) * 32; //y + aaaGameAreas[i][0][1] = aaaGameAreas[i][0][0] + str_tofloat(argv[7]) * 32; //x + width + aaaGameAreas[i][1][1] = aaaGameAreas[i][1][0] + str_tofloat(argv[8]) * 32; //y + height } cmdline_free(argc, argv);