Main loop can exit before the worker runs: under CPU load binwalk prints "Analyzed 0 files" and exits 0
On a busy machine, binwalk sometimes analyses nothing, prints only the stats line, and exits 0:
Analyzed 0 files for 111 file signatures (251 magic patterns) in 195.0 milliseconds
Cause. The main loop in src/main.rs runs while !target_files.is_empty() || workers.active_count() > 0. threadpool::ThreadPool::active_count() counts only jobs a worker thread has already dequeued: execute() increments queued_count, and active_count is incremented inside the worker after recv() (threadpool 1.8.1 lib.rs). So once the initial file has been handed to spawn_worker, one of two things can end the loop with the result lost:
- The worker thread isn't scheduled within the 1 ms sleep. active_count is still 0, the queue is empty, and the loop exits with the job still queued.
- The worker finishes between the last worker_rx.try_recv() and the loop test. Its result sits in the channel unread.
Either way file_count stays 0, nothing is displayed, and main returns ExitCode::SUCCESS, so a caller can't tell it apart from a file with no signatures.
Reproduce (any file with a signature; here a JPEG with an ELF appended): for k in 1 2 3 4; do (taskset -c 0,1 sh -c 'end=$(($(date +%s)+90)); while [ $(date +%s) -lt $end ]; do :; done' &); done seq 400 | xargs -P 8 -I{} sh -c 'taskset -c 0,1 nice -n 19 binwalk photo.jpg' | grep -c "Analyzed 0 files"
At commit 704286902ee66922a028551048cc4a1009f46e6a (3.1.1) this gave 47 of 400 runs; on an idle machine, 0 of 1,000. The loop is unchanged at current master.
Suggested fix: include workers.queued_count() > 0 in the loop condition, drain worker_rx after the loop, and exit non-zero if the target file was never analysed.
Source: ReFirmLabs/binwalk