forked from github/server
parsing tokens when buffer is too small - skip the entire token. now with tests.
This commit is contained in:
parent
1ba7248e48
commit
67881dfff4
|
@ -885,7 +885,7 @@ static int read_newunitid(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));
|
||||
|
||||
/* Da s nun nur einen string enthaelt, suchen wir ihn direkt in der
|
||||
|
|
|
@ -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 quotechar = 0;
|
||||
|
@ -132,12 +132,12 @@ char *parse_token(const char **str, char *lbuf, size_t len)
|
|||
}
|
||||
eatwhitespace_c(&ctoken);
|
||||
if (!*ctoken) {
|
||||
if (len > 0) {
|
||||
if (buflen > 0) {
|
||||
*cursor = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
while (*ctoken && cursor-len < lbuf-1) {
|
||||
while (*ctoken) {
|
||||
ucs4_t ucs;
|
||||
size_t len;
|
||||
bool copy = false;
|
||||
|
@ -188,13 +188,14 @@ char *parse_token(const char **str, char *lbuf, size_t len)
|
|||
copy = true;
|
||||
}
|
||||
if (copy) {
|
||||
memcpy(cursor, ctoken, len);
|
||||
cursor += len;
|
||||
if (cursor - buflen < lbuf - 1) {
|
||||
memcpy(cursor, ctoken, len);
|
||||
cursor += len;
|
||||
}
|
||||
ctoken += len;
|
||||
}
|
||||
}
|
||||
|
||||
assert(cursor - len < lbuf - 1); // TODO: handle too-small buffers
|
||||
*cursor = '\0';
|
||||
*str = ctoken;
|
||||
return lbuf;
|
||||
|
@ -232,7 +233,7 @@ unsigned int getuint(void)
|
|||
|
||||
int getid(void)
|
||||
{
|
||||
char token[16];
|
||||
char token[8];
|
||||
const char *str = gettoken(token, sizeof(token));
|
||||
int i = str ? atoi36(str) : 0;
|
||||
if (i < 0) {
|
||||
|
|
|
@ -15,6 +15,18 @@ static void test_gettoken(CuTest *tc) {
|
|||
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) {
|
||||
char token[128];
|
||||
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_skip_token);
|
||||
SUITE_ADD_TEST(suite, test_gettoken);
|
||||
SUITE_ADD_TEST(suite, test_gettoken_short);
|
||||
SUITE_ADD_TEST(suite, test_getintegers);
|
||||
SUITE_ADD_TEST(suite, test_getstrtoken);
|
||||
return suite;
|
||||
|
|
Loading…
Reference in New Issue