From 6bcd72e24177e7158357f8255c1579c5e02d627f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Thu, 10 May 2018 10:28:49 +0200 Subject: [PATCH] buy set trade: Fix taking lots off the market To find out whether a lot is in use, some places check for zero trd_owner, others for negative trd_unitid. The former is reliable, the latter is not: set() fails to change trd_unitid when it takes a lot off the market. The next trade-related command then runs check_trade(), which logs "Something weird" and cleans up the mess. Broken in commit e16e38dfabc (v4.2.18). Replace the unreliable checks by reliable ones. Clean up set() not to implictly rely on unused lots having negative trd_unitid. The trd_unitid = -1 are unnecessary now, so drop them. Signed-off-by: Markus Armbruster --- src/lib/commands/buy.c | 6 ++++-- src/lib/commands/set.c | 1 + src/lib/commands/trad.c | 24 ++++++++---------------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/lib/commands/buy.c b/src/lib/commands/buy.c index f3934b3df..4839dbe10 100644 --- a/src/lib/commands/buy.c +++ b/src/lib/commands/buy.c @@ -30,7 +30,7 @@ * Dave Pare, 1986 * Pat Loney, 1992 * Steve McClure, 1996-2000 - * Markus Armbruster, 2004-2013 + * Markus Armbruster, 2004-2018 */ #include @@ -112,8 +112,10 @@ buy(void) this part up.*/ tally = 0.0; for (n = 0; gettrade(n, &tmpt); n++) { + if (!tmpt.trd_owner) + continue; if (tmpt.trd_maxbidder == player->cnum && - tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) { + tmpt.trd_owner != player->cnum) { tally += tmpt.trd_price * tradetax; } } diff --git a/src/lib/commands/set.c b/src/lib/commands/set.c index 22547049f..779dd7ba0 100644 --- a/src/lib/commands/set.c +++ b/src/lib/commands/set.c @@ -105,6 +105,7 @@ set(void) if (!trade.trd_owner) { if (freeslot < 0) freeslot = ni_trade.cur; + continue; } if (trade.trd_unitid == ni.cur && trade.trd_type == type) { foundslot = ni_trade.cur; diff --git a/src/lib/commands/trad.c b/src/lib/commands/trad.c index b2ee58b67..597cc3c85 100644 --- a/src/lib/commands/trad.c +++ b/src/lib/commands/trad.c @@ -30,7 +30,7 @@ * Dave Pare, 1986 * Pat Loney, 1992 * Steve McClure, 1996-2000 - * Markus Armbruster, 2004-2013 + * Markus Armbruster, 2004-2018 */ #include @@ -123,18 +123,13 @@ trad(void) pr("Bad lot number\n"); return RET_OK; } - if (!gettrade(lotno, &trade)) { + if (!gettrade(lotno, &trade) || !trade.trd_owner) { pr("No such lot number\n"); return RET_OK; } - if (trade.trd_unitid < 0) { - pr("Invalid lot number.\n"); - return RET_OK; - } if (!trade_getitem(&trade, &tg)) { pr("Can't find trade #%d!\n", trade.trd_unitid); trade.trd_owner = 0; - trade.trd_unitid = -1; if (!puttrade(lotno, &trade)) { logerror("trad: can't write trade"); pr("Couldn't save after getitem failed; get help!\n"); @@ -164,8 +159,10 @@ trad(void) } tally = 0.0; for (i = 0; gettrade(i, &tmpt); i++) { + if (!tmpt.trd_owner) + continue; if (tmpt.trd_maxbidder == player->cnum && - tmpt.trd_unitid >= 0 && tmpt.trd_owner != player->cnum) { + tmpt.trd_owner != player->cnum) { tally += tmpt.trd_price * tradetax; } } @@ -282,20 +279,18 @@ check_trade(void) natid seller; for (n = 0; gettrade(n, &trade); n++) { - if (trade.trd_unitid < 0) + if (!trade.trd_owner) continue; if (!trade_getitem(&trade, &tg)) continue; if (tg.gen.own == 0) { trade.trd_owner = 0; - trade.trd_unitid = -1; puttrade(n, &trade); continue; } if (tg.gen.own != trade.trd_owner) { logerror("Something weird, tg.gen.own != trade.trd_owner!\n"); trade.trd_owner = 0; - trade.trd_unitid = -1; puttrade(n, &trade); continue; } @@ -310,7 +305,6 @@ check_trade(void) saveid = trade.trd_unitid; seller = trade.trd_owner; trade.trd_owner = 0; - trade.trd_unitid = -1; if (!puttrade(n, &trade)) { logerror("Couldn't save trade after purchase; get help!\n"); continue; @@ -408,7 +402,7 @@ ontradingblock(int type, void *ptr) int n; for (n = 0; gettrade(n, &trade); n++) { - if (trade.trd_unitid < 0) + if (!trade.trd_owner) continue; if (!trade_getitem(&trade, &tg)) continue; @@ -428,7 +422,7 @@ trdswitchown(int type, struct empobj *obj, int newown) int n; for (n = 0; gettrade(n, &trade); n++) { - if (trade.trd_unitid < 0) + if (!trade.trd_owner) continue; if (!trade_getitem(&trade, &tg)) continue; @@ -439,8 +433,6 @@ trdswitchown(int type, struct empobj *obj, int newown) if (trade.trd_owner == trade.trd_maxbidder) trade.trd_maxbidder = newown; trade.trd_owner = newown; - if (newown == 0) - trade.trd_unitid = -1; puttrade(n, &trade); return; } -- 2.43.0