fix termination bug in itoa functions.

This commit is contained in:
Enno Rehling 2018-02-25 19:17:20 +01:00
parent 8c554edfee
commit aacd0885df
5 changed files with 26 additions and 13 deletions

View file

@ -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 {

View file

@ -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);

View file

@ -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];

View file

@ -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);

View file

@ -5,6 +5,7 @@
#include <CuTest.h>
#include "base36.h"
#include <stdlib.h>
#include <string.h>
#include <limits.h>
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)