Skip to contentSkip to conversation

randstruct: stop seed generation when a command fails

PATCH New Hamdy Abou El Anein 1 participant 1 message Nov 3 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 randstruct: stop seed generation when a command fails

From: Hamdy Abou El Anein <[email protected]>

The pipeline ending in tr hides an od failure, allowing an empty seed and its hash to be written successfully. Similarly, cut masks a failed sha256sum, and a failed seed-file write does not stop header generation.

Enable errexit and capture od and sha256sum output before formatting it. Compute the hash before writing either output, then stop if a write fails. The seed format and hash calculation are unchanged.

Tested normal generation against Python hashlib, injected od and sha256sum failures, and an invalid seed output path. Failed generation now returns nonzero and does not create the hash header.

AI assistance: ChatGPT/Codex identified the issue and prepared the change, commit message and executable regression tests.

Fixes 313dd1b62921 ("gcc-plugins: Add the randstruct plugin")
---
 scripts/gen-randstruct-seed.sh | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
scripts/gen-randstruct-seed.sh +7 -2
index 61017b36c46..d3c4c1b59bc 100755
@@ -1,7 +1,12 @@
 #!/bin/sh
 # SPDX-License-Identifier: GPL-2.0
 
-SEED=$(od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n')
+set -e
+
+SEED=$(od -A n -t x8 -N 32 /dev/urandom)
+SEED=$(printf '%s' "$SEED" | tr -d ' \n')
+HASH=$(printf '%s' "$SEED" | sha256sum)
+HASH=${HASH%% *}
+
 echo "$SEED" > "$1"
-HASH=$(echo -n "$SEED" | sha256sum | cut -d" " -f1)
 echo "#define RANDSTRUCT_HASHED_SEED \"$HASH\"" > "$2"