From: Stefan Sperling Subject: deltify addblk() fixes To: gameoftrees@openbsd.org Date: Sat, 5 Jun 2021 11:44:30 +0200 Fix deltify addblk() list size confusion: We only have 'old_size' number of blocks to rehash after growing the array. And only extend the array slightly when we outgrow it, instead of doubling the array's size each time (Have you ever seen CVS run out of memory? CVS loved growing arrays like that...) ok? diff 0c9eeee21134b27edd7528f489391f9ded46ffb1 /home/stsp/src/got blob - 0b023fb4330314246e6a110ad5aade625f8b9a0f file + lib/deltify.c --- lib/deltify.c +++ lib/deltify.c @@ -99,7 +99,7 @@ static const struct got_error * addblk(struct got_delta_table *dt, FILE *f, off_t len, off_t offset, uint64_t h) { const struct got_error *err = NULL; - int i, nalloc; + int i; uint8_t buf[GOT_DELTIFY_MAXCHUNK]; size_t r = 0; @@ -140,23 +140,23 @@ addblk(struct got_delta_table *dt, FILE *f, off_t len, dt->blocks[i].offset = offset; dt->blocks[i].hash = h; dt->nblocks++; - if (dt->nalloc < 2 * dt->nblocks) { + if (dt->nalloc < dt->nblocks + 64) { struct got_delta_block *db; - nalloc = dt->nalloc * 2; + size_t old_size = dt->nalloc; db = dt->blocks; - dt->blocks = calloc(nalloc, sizeof(struct got_delta_block)); + dt->blocks = calloc(dt->nblocks + 64, sizeof(struct got_delta_block)); if (dt->blocks == NULL) { err = got_error_from_errno("calloc"); dt->blocks = db; return err; } - dt->nalloc = nalloc; + dt->nalloc = dt->nblocks + 64; /* * Recompute all block positions. Hash-based indices of blocks * in the array depend on the allocated length of the array. */ dt->nblocks = 0; - for (i = 0; i < nalloc; i++) { + for (i = 0; i < old_size; i++) { if (db[i].len == 0) continue; err = addblk(dt, f, db[i].len, db[i].offset,