Skip to content

Commit e8ad8a1

Browse files
committed
compile_native_go_fuzzer_v2: fix handling of multiple file matches
The script uses recursive grep to find fuzzer functions, which can match function names in multiple files. This causes fuzzer_filename to contain multiple files, breaking the convertLibFuzzerTestcaseToStdLibGo command during coverage builds. Fix by searching directly for the function signature with testing.F parameter, which only exists in the actual fuzzer test file. This avoids false matches in files that only reference the function name in comments or helper functions. The script now also explicitly fails if multiple matching files are found. Required for #14183 Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com>
1 parent 537c800 commit e8ad8a1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

infra/base-images/base-builder/compile_native_go_fuzzer_v2

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,25 @@ tags="-tags gofuzz"
2323
# Get absolute path.
2424
abs_file_dir=$(go list $tags -f {{.Dir}} $path)
2525

26-
# TODO(adamkorcz): Get rid of "-r" flag here.
27-
export fuzzer_filename=$(grep -r -l --include='*.go' -s "$function" "${abs_file_dir}")
26+
# Find the file containing the fuzzer function definition.
27+
# Search for the actual function signature with testing.F to avoid false matches
28+
# in files that only reference the function name (e.g., in comments or helper functions).
29+
export fuzzer_filename=$(grep -r -l --include='*.go' "func $function.*testing\.F" "${abs_file_dir}")
2830

2931
# Import build_native_go_fuzzer
3032
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
3133
source "$SCRIPT_DIR/go_utils.sh"
3234

33-
# Test if file contains a line with "func $function" and "testing.F".
34-
if [ $(grep -r "func $function" $fuzzer_filename | grep "testing.F" | wc -l) -eq 1 ]
35+
# Verify we found exactly one file with the fuzzer function.
36+
file_count=$(echo "$fuzzer_filename" | wc -w)
37+
if [ "$file_count" -eq 1 ] && [ -n "$fuzzer_filename" ] && [ -f "$fuzzer_filename" ]
3538
then
3639
build_native_go_fuzzer $fuzzer $function $abs_file_dir $path
40+
elif [ "$file_count" -gt 1 ]
41+
then
42+
echo "Error: Found multiple files with func ${function}(f *testing.F):"
43+
echo "$fuzzer_filename"
44+
exit 1
3745
else
3846
echo "Could not find the function: func ${function}(f *testing.F)"
3947
fi

0 commit comments

Comments
 (0)