From 96fdb9bd1607d034d5ec5c824ad9e6a58a53efd9 Mon Sep 17 00:00:00 2001 From: Neil Johari Date: Mon, 15 Sep 2025 23:58:45 -0700 Subject: [PATCH] Add debug logging and simple repro --- programs/dibio.c | 20 +++++++++++++++++++ test_allocation_bug.sh | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100755 test_allocation_bug.sh diff --git a/programs/dibio.c b/programs/dibio.c index 63c455a22..f5f33f1bd 100644 --- a/programs/dibio.c +++ b/programs/dibio.c @@ -279,10 +279,16 @@ static fileStats DiB_fileStats(const char** fileNamesTable, int nbFiles, size_t for (n=0; n alloc_test/good_$i.txt +done + +echo "Valid files created (about 6 bytes each = 30 bytes total)" +echo "" + +# We need enough bad files to make totalSizeToLoad negative +# 30 bytes positive, so we need at least 31 bad files +echo "Adding 1000 non-existent files to make totalSizeToLoad very negative..." +echo "Expected: totalSizeToLoad = 30 + (1000 * -1) = -970 bytes" +echo "" + +# Build command +CMD="./zstd --train alloc_test/good_*.txt" +for i in {1..1000}; do + CMD="$CMD alloc_test/BAD_$i" +done +CMD="$CMD -o alloc_test/dict.zst --maxdict=65536 2>&1" + +echo "Running command..." +echo "=================" + +# Run and capture ALL debug output related to our issue +eval $CMD | grep -E "\[DEBUG FINAL\]|\[DEBUG\] Memory calc|\[BUG\]|About to malloc|Error|not enough memory" + +echo "" +echo "Output should show something like the following:" +echo "1. [DEBUG FINAL] fileStats: totalSizeToLoad=-970 (NEGATIVE!)" +echo "2. [BUG] totalSizeToLoad is NEGATIVE!" +echo "3. [DEBUG] Memory calc: showing huge loadedSize value" +echo "4. Error about memory allocation" -- 2.47.3