]> git.pond.sub.org Git - empserver/commitdiff
budget: Oops on use of invalid bp map entries
authorMarkus Armbruster <armbru@pond.sub.org>
Sun, 17 Jul 2016 06:12:56 +0000 (08:12 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Sun, 6 Aug 2017 17:59:58 +0000 (19:59 +0200)
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
src/lib/update/bp.c

index 941e68d95dc0b2172effe14d9fc39d5aebc08ed1..8418031e9fc574cbdd4dfef48f6423d204dc951c 100644 (file)
@@ -57,6 +57,7 @@ enum bp_item_idx {
 struct bp {
     short bp_item[BP_MAX + 1];
     short bp_avail;
+    unsigned char bp_tracked;
 };
 
 /* Map i_type to enum bp_item_idx. */
@@ -81,15 +82,22 @@ bp_set_from_sect(struct bp *bp, struct sctstr *sp)
            bp[sp->sct_uid].bp_item[idx] = sp->sct_item[i];
     }
     bp[sp->sct_uid].bp_avail = sp->sct_avail;
+    bp[sp->sct_uid].bp_tracked = 1;
 }
 
-/* Copy the values tracked in @bp for sector @sp back to @sp. */
+/*
+ * Copy the values tracked in @bp for sector @sp back to @sp.
+ * Values must have been set with bp_set_from_sect().
+ */
 void
 bp_to_sect(struct bp *bp, struct sctstr *sp)
 {
     i_type i;
     enum bp_item_idx idx;
 
+    if (CANT_HAPPEN(!bp[sp->sct_uid].bp_tracked))
+       return;
+
     for (i = I_NONE + 1; i <= I_MAX; i++) {
        idx = bud_key[i];
        if (idx >= 0)
@@ -105,5 +113,11 @@ bp_to_sect(struct bp *bp, struct sctstr *sp)
 struct bp *
 bp_alloc(void)
 {
-    return malloc(WORLD_SZ() * sizeof(struct bp));
+    int n = WORLD_SZ();
+    struct bp *bp = malloc(n * sizeof(*bp));
+    int i;
+
+    for (i = 0; i < n; i++)
+       bp[i].bp_tracked = 0;
+    return bp;
 }