From 67881dfff4ddf8ee9d31c241e2c860e33d0a1039 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 24 Dec 2014 15:55:55 +0100 Subject: [PATCH 1/4] parsing tokens when buffer is too small - skip the entire token. now with tests. --- src/kernel/config.c | 2 +- src/util/parser.c | 15 ++++++++------- src/util/parser.test.c | 13 +++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 3f511ffd9..db55b8976 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -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 diff --git a/src/util/parser.c b/src/util/parser.c index 51e99a9b6..62a2a37fe 100644 --- a/src/util/parser.c +++ b/src/util/parser.c @@ -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) { diff --git a/src/util/parser.test.c b/src/util/parser.test.c index 0c6e0059c..95ef70a17 100644 --- a/src/util/parser.test.c +++ b/src/util/parser.test.c @@ -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; From ea035ac154bf02c080f434d261d5c209d010e3e8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 24 Dec 2014 16:21:01 +0100 Subject: [PATCH 2/4] convert float values in at_object to integer where it makes sense. this feature is only used for the embassy_muschel turn, and I am tempted to kill it entirely. --- src/attributes/object.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/attributes/object.c b/src/attributes/object.c index c0d0e0800..ae06d6454 100644 --- a/src/attributes/object.c +++ b/src/attributes/object.c @@ -112,7 +112,13 @@ static int object_read(attrib * a, void *owner, struct storage *store) break; case TREAL: READ_FLT(store, &flt); - data->data.real = flt; + if ((int)flt == flt) { + data->type = TINTEGER; + data->data.i = (int)flt; + } + else { + data->data.real = flt; + } break; case TSTRING: READ_STR(store, name, sizeof(name)); From 3e4973db253afbe7936f4626ff6b217f8c348296 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 24 Dec 2014 16:33:21 +0100 Subject: [PATCH 3/4] kill the unused EnhancedQuit feature, reducing the potential use of at_object by one more situation. --- src/laws.c | 59 ++---------------------------------------------------- 1 file changed, 2 insertions(+), 57 deletions(-) diff --git a/src/laws.c b/src/laws.c index b8ea9fa2e..115b5792a 100755 --- a/src/laws.c +++ b/src/laws.c @@ -63,7 +63,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* attributes includes */ -#include #include #include #include @@ -825,8 +824,8 @@ static void transfer_faction(faction * f, faction * f2) for (u = f->units; u;) { un = u->nextF; - if (!unit_has_cursed_item(u) - && !has_skill(u, SK_MAGIC) && !has_skill(u, SK_ALCHEMY)) { + if (!unit_has_cursed_item(u) && + !has_skill(u, SK_MAGIC) && !has_skill(u, SK_ALCHEMY)) { u_setfaction(u, f2); } u = un; @@ -934,16 +933,6 @@ int leave_cmd(unit * u, struct order *ord) return 0; } -static bool EnhancedQuit(void) -{ - static int value = -1; - if (value < 0) { - const char *str = get_param(global.parameters, "alliance.transferquit"); - value = (str != 0 && strcmp(str, "true") == 0); - } - return value; -} - int quit_cmd(unit * u, struct order *ord) { char token[128]; @@ -955,31 +944,6 @@ int quit_cmd(unit * u, struct order *ord) assert(kwd == K_QUIT); passwd = gettoken(token, sizeof(token)); if (checkpasswd(f, (const char *)passwd)) { - if (EnhancedQuit()) { - int f2_id = getid(); - if (f2_id > 0) { - faction *f2 = findfaction(f2_id); - - if (f2 == NULL) { - cmistake(u, ord, 66, MSG_EVENT); - return 0; - } - else if (!u->faction->alliance - || u->faction->alliance != f2->alliance) { - cmistake(u, ord, 315, MSG_EVENT); - return 0; - } - else if (!alliedfaction(NULL, f, f2, HELP_MONEY)) { - cmistake(u, ord, 316, MSG_EVENT); - return 0; - } - else { - variant var; - var.i = f2_id; - a_add(&f->attribs, object_create("quit", TINTEGER, var)); - } - } - } fset(f, FFL_QUIT); } else { @@ -1299,25 +1263,6 @@ void quit(void) while (*fptr) { faction *f = *fptr; if (f->flags & FFL_QUIT) { - if (EnhancedQuit()) { - /* this doesn't work well (use object_name()) */ - attrib *a = a_find(f->attribs, &at_object); - if (a) { - variant var; - object_type type; - var.i = 0; - object_get(a, &type, &var); - assert(var.i && type == TINTEGER); - if (var.i) { - int f2_id = var.i; - faction *f2 = findfaction(f2_id); - - assert(f2_id > 0); - assert(f2 != NULL); - transfer_faction(f, f2); - } - } - } destroyfaction(f); } else { From 4f962ae9999b81d18b02a46d2a64851e7a95f777 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 25 Dec 2014 00:45:52 +0100 Subject: [PATCH 4/4] remove dead code --- src/laws.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/laws.c b/src/laws.c index 115b5792a..0c01f0471 100755 --- a/src/laws.c +++ b/src/laws.c @@ -818,20 +818,6 @@ static void inactivefaction(faction * f) } } -static void transfer_faction(faction * f, faction * f2) -{ - unit *u, *un; - - for (u = f->units; u;) { - un = u->nextF; - if (!unit_has_cursed_item(u) && - !has_skill(u, SK_MAGIC) && !has_skill(u, SK_ALCHEMY)) { - u_setfaction(u, f2); - } - u = un; - } -} - /* test if the unit can slip through a siege undetected. * returns 0 if siege is successful, or 1 if the building is either * not besieged or the unit can slip through the siege due to better stealth.