aboutsummaryrefslogtreecommitdiff
path: root/internal/player
diff options
context:
space:
mode:
Diffstat (limited to 'internal/player')
-rw-r--r--internal/player/hardening_removeitem_test.go23
-rw-r--r--internal/player/player.go6
2 files changed, 29 insertions, 0 deletions
diff --git a/internal/player/hardening_removeitem_test.go b/internal/player/hardening_removeitem_test.go
new file mode 100644
index 0000000..5cb1e4c
--- /dev/null
+++ b/internal/player/hardening_removeitem_test.go
@@ -0,0 +1,23 @@
+package player
+
+import "testing"
+
+// TestRemoveItemRejectsNonPositive: a non-positive quantity must never mutate
+// a stack; a negative value used to increase it.
+func TestRemoveItemRejectsNonPositive(t *testing.T) {
+ p := New("alice")
+ p.SetInvSlot(0, &InventorySlot{ItemID: "coin", Quantity: 5})
+
+ if p.RemoveItem("coin", -3) {
+ t.Fatal("negative quantity accepted")
+ }
+ if slot := p.InvSlot(0); slot == nil || slot.Quantity != 5 {
+ t.Fatalf("stack mutated by a negative removal: %+v", slot)
+ }
+ if p.RemoveItem("coin", 0) {
+ t.Fatal("zero quantity accepted")
+ }
+ if slot := p.InvSlot(0); slot == nil || slot.Quantity != 5 {
+ t.Fatalf("stack mutated by a zero removal: %+v", slot)
+ }
+}
diff --git a/internal/player/player.go b/internal/player/player.go
index f8d9a68..efa906b 100644
--- a/internal/player/player.go
+++ b/internal/player/player.go
@@ -510,6 +510,12 @@ func (p *Player) CountItem(itemID string) int {
}
func (p *Player) RemoveItem(itemID string, qty int) bool {
+ // A non-positive quantity must never mutate a stack: with qty < 0 the
+ // subtraction below would increase it. Callers pass positive quantities;
+ // reject anything else instead of creating items from a data typo.
+ if qty <= 0 {
+ return false
+ }
remaining := qty
for i, slot := range p.Inventory {
if slot == nil || slot.ItemID != itemID {