parsing tokens when buffer is too small - skip the entire token. now with tests.

This commit is contained in:
Enno Rehling 2014-12-24 15:55:55 +01:00
parent 1ba7248e48
commit 67881dfff4
3 changed files with 22 additions and 8 deletions

View file

@ -885,7 +885,7 @@ static int read_newunitid(const faction * f, const region * r)
int read_unitid(const faction * f, const region * r) int read_unitid(const faction * f, const region * r)
{ {
char token[8]; char token[16];
const char *s = gettoken(token, sizeof(token)); const char *s = gettoken(token, sizeof(token));
/* Da s nun nur einen string enthaelt, suchen wir ihn direkt in der /* Da s nun nur einen string enthaelt, suchen wir ihn direkt in der

View file

@ -120,7 +120,7 @@ void skip_token(void)
} }
} }
char *parse_token(const char **str, char *lbuf, size_t len) char *parse_token(const char **str, char *lbuf, size_t buflen)
{ {
char *cursor = lbuf; char *cursor = lbuf;
char quotechar = 0; char quotechar = 0;
@ -132,12 +132,12 @@ char *parse_token(const char **str, char *lbuf, size_t len)
} }
eatwhitespace_c(&ctoken); eatwhitespace_c(&ctoken);
if (!*ctoken) { if (!*ctoken) {
if (len > 0) { if (buflen > 0) {
*cursor = 0; *cursor = 0;
} }
return 0; return 0;
} }
while (*ctoken && cursor-len < lbuf-1) { while (*ctoken) {
ucs4_t ucs; ucs4_t ucs;
size_t len; size_t len;
bool copy = false; bool copy = false;
@ -188,13 +188,14 @@ char *parse_token(const char **str, char *lbuf, size_t len)
copy = true; copy = true;
} }
if (copy) { if (copy) {
memcpy(cursor, ctoken, len); if (cursor - buflen < lbuf - 1) {
cursor += len; memcpy(cursor, ctoken, len);
cursor += len;
}
ctoken += len; ctoken += len;
} }
} }
assert(cursor - len < lbuf - 1); // TODO: handle too-small buffers
*cursor = '\0'; *cursor = '\0';
*str = ctoken; *str = ctoken;
return lbuf; return lbuf;
@ -232,7 +233,7 @@ unsigned int getuint(void)
int getid(void) int getid(void)
{ {
char token[16]; char token[8];
const char *str = gettoken(token, sizeof(token)); const char *str = gettoken(token, sizeof(token));
int i = str ? atoi36(str) : 0; int i = str ? atoi36(str) : 0;
if (i < 0) { if (i < 0) {

View file

@ -15,6 +15,18 @@ static void test_gettoken(CuTest *tc) {
CuAssertStrEquals(tc, "", token); CuAssertStrEquals(tc, "", token);
} }
static void test_gettoken_short(CuTest *tc) {
char token[3];
init_tokens_str("HELP ONE TWO THREE");
CuAssertStrEquals(tc, "HE", gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "HE", token);
CuAssertStrEquals(tc, "ON", gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "TW", gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "TH", gettoken(token, sizeof(token)));
CuAssertPtrEquals(tc, NULL, (void *)gettoken(token, sizeof(token)));
CuAssertStrEquals(tc, "", token);
}
static void test_skip_token(CuTest *tc) { static void test_skip_token(CuTest *tc) {
char token[128]; char token[128];
init_tokens_str("HELP ONE TWO THREE"); init_tokens_str("HELP ONE TWO THREE");
@ -53,6 +65,7 @@ CuSuite *get_parser_suite(void)
SUITE_ADD_TEST(suite, test_atoip); SUITE_ADD_TEST(suite, test_atoip);
SUITE_ADD_TEST(suite, test_skip_token); SUITE_ADD_TEST(suite, test_skip_token);
SUITE_ADD_TEST(suite, test_gettoken); SUITE_ADD_TEST(suite, test_gettoken);
SUITE_ADD_TEST(suite, test_gettoken_short);
SUITE_ADD_TEST(suite, test_getintegers); SUITE_ADD_TEST(suite, test_getintegers);
SUITE_ADD_TEST(suite, test_getstrtoken); SUITE_ADD_TEST(suite, test_getstrtoken);
return suite; return suite;