From 26cd875616735ae3f19e4f1c498c6961ee116f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20M=C3=A9ndez=20Rey?= Date: Fri, 13 Nov 2026 21:28:51 +0100 Subject: [PATCH] fix: build the out-of-range FileMode test cases at run time (#5512) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Test_safeFileModeConvert declared two cases as int(math.MaxInt32) + 1 and int(math.MinInt32) - 1. Those are constants that do not fit in an int where int is 32 bits wide, so the package did not compile on 386. On such a platform no int value lies outside the int32 range, so the two cases cannot be built there. They now come from int64 variables, appended only where int is wider than int32, which keeps both cases on 64-bit. Signed-off-by: Juan Manuel Méndez Rey --- syft/format/syftjson/to_syft_model_test.go | 41 ++++++++++++++-------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/syft/format/syftjson/to_syft_model_test.go b/syft/format/syftjson/to_syft_model_test.go index ccdbaec1d..aa2f5faa5 100644 --- a/syft/format/syftjson/to_syft_model_test.go +++ b/syft/format/syftjson/to_syft_model_test.go @@ -554,12 +554,14 @@ func Test_deduplicateErrors(t *testing.T) { } func Test_safeFileModeConvert(t *testing.T) { - tests := []struct { + type testCase struct { name string val int want fs.FileMode wantErr bool - }{ + } + + tests := []testCase{ { // fs.go ModePerm 511 = FileMode = 0777 // Unix permission bits :192 name: "valid perm", @@ -573,18 +575,29 @@ func Test_safeFileModeConvert(t *testing.T) { want: os.FileMode(0o1000000777), // 134218239 wantErr: false, }, - { - name: "outside int32 high", - val: int(math.MaxInt32) + 1, - want: 0, - wantErr: true, - }, - { - name: "outside int32 low", - val: int(math.MinInt32) - 1, - want: 0, - wantErr: true, - }, + } + + // Where int is 32 bits wide, no int value lies outside the int32 range, so + // these two cases cannot be built at all. The bounds come from int64 + // variables rather than constants, because a constant that does not fit in + // an int is a compile error on those platforms, not a skipped test. + if math.MaxInt > math.MaxInt32 { + maxInt32 := int64(math.MaxInt32) + minInt32 := int64(math.MinInt32) + tests = append(tests, + testCase{ + name: "outside int32 high", + val: int(maxInt32 + 1), + want: 0, + wantErr: true, + }, + testCase{ + name: "outside int32 low", + val: int(minInt32 - 1), + want: 0, + wantErr: true, + }, + ) } for _, tt := range tests {