From 8a638859fd83613374199014d3421b088b978f7f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 20 Nov 2017 08:24:56 +0100 Subject: [PATCH] fix memory access in rc_setoption --- src/economy.test.c | 4 +++- src/kernel/race.c | 17 +++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index 6303b2ae8..47194ef86 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -217,7 +217,7 @@ static unit *setup_trade_unit(CuTest *tc, region *r, const struct race *rc) { } static void test_trade_insect(CuTest *tc) { - /* Insekten können in Wüsten und Sümpfen auch ohne Burgen handeln. */ + /* Insekten k�nnen in W�sten und S�mpfen auch ohne Burgen handeln. */ unit *u; region *r; const item_type *it_luxury; @@ -239,6 +239,8 @@ static void test_trade_insect(CuTest *tc) { LOC(u->faction->locale, resourcename(it_luxury->rtype, 0)))); set_item(u, it_silver, 10); + CuAssertPtrEquals(tc, r, u->region); + CuAssertPtrEquals(tc, (void *)it_luxury, (void *)r_luxury(u->region)); produce(u->region); CuAssertIntEquals(tc, 1, get_item(u, it_luxury)); CuAssertIntEquals(tc, 5, get_item(u, it_silver)); diff --git a/src/kernel/race.c b/src/kernel/race.c index a73c0e699..ceb2029c4 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -106,17 +106,18 @@ static void rc_setoption(race *rc, int k, const char *value) { rc->options->key[1] = RCO_NONE; v = rc->options->value; } else { - for (i=0;!v && i < MAXOPTIONS && rc->options->key[i]!=RCO_NONE;++i) { + for (i=0;!v && i < MAXOPTIONS;++i) { if (rc->options->key[i]==key) { v = rc->options->value+i; + break; } - } - if (!v) { - assert(ioptions->value+i; - rc->options->key[i] = key; - if (i+1options->key[i+1]=RCO_NONE; + if (rc->options->key[i]==RCO_NONE) { + v = rc->options->value+i; + rc->options->key[i] = key; + if (i+1 < MAXOPTIONS) { + rc->options->key[i+1]=RCO_NONE; + } + break; } } }