From aacd0885df3f70805846f9149d28c1cdaf742d6a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 25 Feb 2018 19:17:20 +0100 Subject: [PATCH] fix termination bug in itoa functions. --- src/gmtool.c | 2 +- src/kernel/unit.h | 1 - src/util/base36.c | 12 +++++++++--- src/util/base36.h | 1 + src/util/base36.test.c | 23 +++++++++++++++-------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/gmtool.c b/src/gmtool.c index 6051ed9cc..65ca7fe07 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -1247,7 +1247,7 @@ static void handlekey(state * st, int c) else if (findmode == 'F') { faction *f = select_faction(st); if (f != NULL) { - itoab_r(f->no, 36, locate, sizeof(locate)); + itoa36_r(f->no, locate, sizeof(locate)); findmode = 'f'; } else { diff --git a/src/kernel/unit.h b/src/kernel/unit.h index b70082a4b..c242e0bd2 100644 --- a/src/kernel/unit.h +++ b/src/kernel/unit.h @@ -249,7 +249,6 @@ extern "C" { void remove_empty_units(void); struct unit *findunit(int n); - struct unit *findunitr(const struct region *r, int n); void default_name(const unit *u, char name[], int len); diff --git a/src/util/base36.c b/src/util/base36.c index 3ded21a54..30020de30 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -58,7 +58,7 @@ const char *itoab_r(int i, int base, char *s, size_t len) assert(len > 2); dst = s + len - 2; - *dst = 0; + dst[1] = 0; if (i != 0) { int neg = 0; @@ -92,12 +92,18 @@ const char *itoab_r(int i, int base, char *s, size_t len) assert(i == 0 || !"itoab: static buffer exhausted"); } } - else - *dst = '0'; + else { + dst[0] = '0'; + } return dst; } +const char *itoa36_r(int i, char *result, size_t len) +{ + return itoab_r(i, 36, result, len); +} + const char *itoab(int i, int base) { static char sstr[80]; diff --git a/src/util/base36.h b/src/util/base36.h index 804952ab7..05c81d2c0 100644 --- a/src/util/base36.h +++ b/src/util/base36.h @@ -25,6 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif + const char *itoa36_r(int i, char *result, size_t len); const char *itoab_r(int i, int base, char *result, size_t len); const char *itoab(int i, int base); const char *itoa36(int i); diff --git a/src/util/base36.test.c b/src/util/base36.test.c index 226fd86ab..90d5b42f7 100644 --- a/src/util/base36.test.c +++ b/src/util/base36.test.c @@ -5,6 +5,7 @@ #include #include "base36.h" #include +#include #include static void test_atoi36(CuTest * tc) @@ -19,14 +20,20 @@ static void test_atoi36(CuTest * tc) static void test_itoa36(CuTest * tc) { - CuAssertStrEquals(tc, itoa36(0), "0"); - CuAssertStrEquals(tc, itoa10(INT_MAX), "2147483647"); - CuAssertStrEquals(tc, itoab(INT_MAX, 8), "17777777777"); - CuAssertStrEquals(tc, itoab(INT_MAX, 4), "1333333333333333"); - CuAssertStrEquals(tc, itoab(-1, 5), "-1"); - CuAssertStrEquals(tc, itoa36(-1), "-1"); - CuAssertStrEquals(tc, itoa36(-10), "-a"); - CuAssertStrEquals(tc, itoa36(666), "ii"); + char buf[10]; + CuAssertStrEquals(tc, itoa36(0), "0"); + CuAssertStrEquals(tc, itoa10(INT_MAX), "2147483647"); + CuAssertStrEquals(tc, itoab(INT_MAX, 8), "17777777777"); + CuAssertStrEquals(tc, itoab(INT_MAX, 4), "1333333333333333"); + CuAssertStrEquals(tc, itoab(-1, 5), "-1"); + CuAssertStrEquals(tc, itoa36(-1), "-1"); + CuAssertStrEquals(tc, itoa36(-10), "-a"); + CuAssertStrEquals(tc, itoa36(666), "ii"); + + memset(buf, 255, sizeof(buf)); + CuAssertStrEquals(tc, itoa36_r(-10, buf, sizeof(buf)), "-a"); + memset(buf, 255, sizeof(buf)); + CuAssertStrEquals(tc, itoab_r(666, 36, buf, sizeof(buf)), "ii"); } CuSuite *get_base36_suite(void)