@@ -4,7 +4,34 @@ import (
4
4
"testing"
5
5
)
6
6
7
+ // isTruncationDetectionEnabled checks if truncation detection is enabled by attempting
8
+ // a truncation that should panic if detection is enabled
9
+ func isTruncationDetectionEnabled () bool {
10
+ panicked := false
11
+ func () {
12
+ defer func () {
13
+ if recover () != nil {
14
+ panicked = true
15
+ }
16
+ }()
17
+
18
+ // Try a simple truncation that should trigger detection if enabled
19
+ var test uint16 = 256
20
+ _ = uint8 (test ) // This should panic if truncation detection is on
21
+ }()
22
+
23
+ return panicked
24
+ }
25
+
26
+ // skipIfTruncationDisabled skips the test if truncation detection is disabled
27
+ func skipIfTruncationDisabled (t * testing.T ) {
28
+ if ! isTruncationDetectionEnabled () {
29
+ t .Skip ("Skipping truncation test - truncation detection is disabled" )
30
+ }
31
+ }
32
+
7
33
func TestInt64ToInt32Overflow (t * testing.T ) {
34
+ skipIfTruncationDisabled (t )
8
35
defer func () {
9
36
if r := recover (); r == nil {
10
37
t .Fatal ("Expected panic for int64 to int32 overflow" )
@@ -15,6 +42,7 @@ func TestInt64ToInt32Overflow(t *testing.T) {
15
42
}
16
43
17
44
func TestInt64ToInt32Underflow (t * testing.T ) {
45
+ skipIfTruncationDisabled (t )
18
46
defer func () {
19
47
if r := recover (); r == nil {
20
48
t .Fatal ("Expected panic for int64 to int32 underflow" )
@@ -25,6 +53,7 @@ func TestInt64ToInt32Underflow(t *testing.T) {
25
53
}
26
54
27
55
func TestInt32ToInt16Overflow (t * testing.T ) {
56
+ skipIfTruncationDisabled (t )
28
57
defer func () {
29
58
if r := recover (); r == nil {
30
59
t .Fatal ("Expected panic for int32 to int16 overflow" )
@@ -35,6 +64,7 @@ func TestInt32ToInt16Overflow(t *testing.T) {
35
64
}
36
65
37
66
func TestInt32ToInt16Underflow (t * testing.T ) {
67
+ skipIfTruncationDisabled (t )
38
68
defer func () {
39
69
if r := recover (); r == nil {
40
70
t .Fatal ("Expected panic for int32 to int16 underflow" )
@@ -45,6 +75,7 @@ func TestInt32ToInt16Underflow(t *testing.T) {
45
75
}
46
76
47
77
func TestInt16ToInt8Overflow (t * testing.T ) {
78
+ skipIfTruncationDisabled (t )
48
79
defer func () {
49
80
if r := recover (); r == nil {
50
81
t .Fatal ("Expected panic for int16 to int8 overflow" )
@@ -55,6 +86,7 @@ func TestInt16ToInt8Overflow(t *testing.T) {
55
86
}
56
87
57
88
func TestInt16ToInt8Underflow (t * testing.T ) {
89
+ skipIfTruncationDisabled (t )
58
90
defer func () {
59
91
if r := recover (); r == nil {
60
92
t .Fatal ("Expected panic for int16 to int8 underflow" )
@@ -65,6 +97,7 @@ func TestInt16ToInt8Underflow(t *testing.T) {
65
97
}
66
98
67
99
func TestUint64ToUint32Overflow (t * testing.T ) {
100
+ skipIfTruncationDisabled (t )
68
101
defer func () {
69
102
if r := recover (); r == nil {
70
103
t .Fatal ("Expected panic for uint64 to uint32 overflow" )
@@ -75,6 +108,7 @@ func TestUint64ToUint32Overflow(t *testing.T) {
75
108
}
76
109
77
110
func TestUint32ToUint16Overflow (t * testing.T ) {
111
+ skipIfTruncationDisabled (t )
78
112
defer func () {
79
113
if r := recover (); r == nil {
80
114
t .Fatal ("Expected panic for uint32 to uint16 overflow" )
@@ -85,6 +119,7 @@ func TestUint32ToUint16Overflow(t *testing.T) {
85
119
}
86
120
87
121
func TestUint16ToUint8Overflow (t * testing.T ) {
122
+ skipIfTruncationDisabled (t )
88
123
defer func () {
89
124
if r := recover (); r == nil {
90
125
t .Fatal ("Expected panic for uint16 to uint8 overflow" )
@@ -95,6 +130,7 @@ func TestUint16ToUint8Overflow(t *testing.T) {
95
130
}
96
131
97
132
func TestIntToInt32OnLargeValues (t * testing.T ) {
133
+ skipIfTruncationDisabled (t )
98
134
defer func () {
99
135
if r := recover (); r == nil {
100
136
t .Fatal ("Expected panic for int to int32 on large values" )
@@ -105,6 +141,7 @@ func TestIntToInt32OnLargeValues(t *testing.T) {
105
141
}
106
142
107
143
func TestIntToInt16OnLargeValues (t * testing.T ) {
144
+ skipIfTruncationDisabled (t )
108
145
defer func () {
109
146
if r := recover (); r == nil {
110
147
t .Fatal ("Expected panic for int to int16 on large values" )
@@ -115,6 +152,7 @@ func TestIntToInt16OnLargeValues(t *testing.T) {
115
152
}
116
153
117
154
func TestIntToInt8OnLargeValues (t * testing.T ) {
155
+ skipIfTruncationDisabled (t )
118
156
defer func () {
119
157
if r := recover (); r == nil {
120
158
t .Fatal ("Expected panic for int to int8 on large values" )
@@ -125,6 +163,7 @@ func TestIntToInt8OnLargeValues(t *testing.T) {
125
163
}
126
164
127
165
func TestUintToUint32OnLargeValues (t * testing.T ) {
166
+ skipIfTruncationDisabled (t )
128
167
defer func () {
129
168
if r := recover (); r == nil {
130
169
t .Fatal ("Expected panic for uint to uint32 on large values" )
@@ -135,6 +174,7 @@ func TestUintToUint32OnLargeValues(t *testing.T) {
135
174
}
136
175
137
176
func TestUintToUint16OnLargeValues (t * testing.T ) {
177
+ skipIfTruncationDisabled (t )
138
178
defer func () {
139
179
if r := recover (); r == nil {
140
180
t .Fatal ("Expected panic for uint to uint16 on large values" )
@@ -145,6 +185,7 @@ func TestUintToUint16OnLargeValues(t *testing.T) {
145
185
}
146
186
147
187
func TestUintToUint8OnLargeValues (t * testing.T ) {
188
+ skipIfTruncationDisabled (t )
148
189
defer func () {
149
190
if r := recover (); r == nil {
150
191
t .Fatal ("Expected panic for uint to uint8 on large values" )
@@ -155,6 +196,7 @@ func TestUintToUint8OnLargeValues(t *testing.T) {
155
196
}
156
197
157
198
func TestSignedToUnsignedNegative (t * testing.T ) {
199
+ skipIfTruncationDisabled (t )
158
200
defer func () {
159
201
if r := recover (); r == nil {
160
202
t .Fatal ("Expected panic for signed to unsigned with negative values" )
@@ -165,6 +207,7 @@ func TestSignedToUnsignedNegative(t *testing.T) {
165
207
}
166
208
167
209
func TestUnsignedToSigned (t * testing.T ) {
210
+ skipIfTruncationDisabled (t )
168
211
defer func () {
169
212
if r := recover (); r == nil {
170
213
t .Fatal ("Expected panic for unsigned to signed with large values" )
@@ -175,6 +218,7 @@ func TestUnsignedToSigned(t *testing.T) {
175
218
}
176
219
177
220
func TestInt16ToUint16Negative (t * testing.T ) {
221
+ skipIfTruncationDisabled (t )
178
222
defer func () {
179
223
if r := recover (); r == nil {
180
224
t .Fatal ("Expected panic for int16 to uint16 with negative values" )
@@ -185,6 +229,7 @@ func TestInt16ToUint16Negative(t *testing.T) {
185
229
}
186
230
187
231
func TestInt8ToUint8Negative (t * testing.T ) {
232
+ skipIfTruncationDisabled (t )
188
233
defer func () {
189
234
if r := recover (); r == nil {
190
235
t .Fatal ("Expected panic for int8 to uint8 with negative values" )
@@ -195,6 +240,7 @@ func TestInt8ToUint8Negative(t *testing.T) {
195
240
}
196
241
197
242
func TestComplexTruncationChain (t * testing.T ) {
243
+ skipIfTruncationDisabled (t )
198
244
defer func () {
199
245
if r := recover (); r == nil {
200
246
t .Fatal ("Expected panic for complex truncation chain" )
@@ -207,6 +253,7 @@ func TestComplexTruncationChain(t *testing.T) {
207
253
}
208
254
209
255
func TestRuntimeComputedTruncation (t * testing.T ) {
256
+ skipIfTruncationDisabled (t )
210
257
defer func () {
211
258
if r := recover (); r == nil {
212
259
t .Fatal ("Expected panic for runtime computed truncation" )
@@ -220,6 +267,7 @@ func TestRuntimeComputedTruncation(t *testing.T) {
220
267
}
221
268
222
269
func TestBufferSizeVulnerability (t * testing.T ) {
270
+ skipIfTruncationDisabled (t )
223
271
defer func () {
224
272
if r := recover (); r == nil {
225
273
t .Fatal ("Expected panic for buffer size vulnerability" )
@@ -231,6 +279,7 @@ func TestBufferSizeVulnerability(t *testing.T) {
231
279
}
232
280
233
281
func TestArrayIndexTruncation (t * testing.T ) {
282
+ skipIfTruncationDisabled (t )
234
283
defer func () {
235
284
if r := recover (); r == nil {
236
285
t .Fatal ("Expected panic for array index truncation" )
@@ -242,6 +291,7 @@ func TestArrayIndexTruncation(t *testing.T) {
242
291
}
243
292
244
293
func TestMemoryOffsetTruncation (t * testing.T ) {
294
+ skipIfTruncationDisabled (t )
245
295
defer func () {
246
296
if r := recover (); r == nil {
247
297
t .Fatal ("Expected panic for memory offset truncation" )
@@ -253,6 +303,7 @@ func TestMemoryOffsetTruncation(t *testing.T) {
253
303
}
254
304
255
305
func TestSecurityBoundaryTruncation (t * testing.T ) {
306
+ skipIfTruncationDisabled (t )
256
307
defer func () {
257
308
if r := recover (); r == nil {
258
309
t .Fatal ("Expected panic for security boundary truncation" )
@@ -265,6 +316,7 @@ func TestSecurityBoundaryTruncation(t *testing.T) {
265
316
266
317
// Platform-dependent truncation edge cases
267
318
func TestPlatformDependentIntTruncation (t * testing.T ) {
319
+ skipIfTruncationDisabled (t )
268
320
defer func () {
269
321
if r := recover (); r == nil {
270
322
t .Fatal ("Expected panic for platform-dependent int truncation" )
@@ -275,6 +327,7 @@ func TestPlatformDependentIntTruncation(t *testing.T) {
275
327
}
276
328
277
329
func TestBoundaryTruncationInt32MaxPlusOne (t * testing.T ) {
330
+ skipIfTruncationDisabled (t )
278
331
defer func () {
279
332
if r := recover (); r == nil {
280
333
t .Fatal ("Expected panic for int32 max+1 boundary truncation" )
@@ -285,6 +338,7 @@ func TestBoundaryTruncationInt32MaxPlusOne(t *testing.T) {
285
338
}
286
339
287
340
func TestBoundaryTruncationInt32MinMinusOne (t * testing.T ) {
341
+ skipIfTruncationDisabled (t )
288
342
defer func () {
289
343
if r := recover (); r == nil {
290
344
t .Fatal ("Expected panic for int32 min-1 boundary truncation" )
@@ -295,6 +349,7 @@ func TestBoundaryTruncationInt32MinMinusOne(t *testing.T) {
295
349
}
296
350
297
351
func TestBoundaryTruncationInt16MaxPlusOne (t * testing.T ) {
352
+ skipIfTruncationDisabled (t )
298
353
defer func () {
299
354
if r := recover (); r == nil {
300
355
t .Fatal ("Expected panic for int16 max+1 boundary truncation" )
@@ -305,6 +360,7 @@ func TestBoundaryTruncationInt16MaxPlusOne(t *testing.T) {
305
360
}
306
361
307
362
func TestBoundaryTruncationInt8MaxPlusOne (t * testing.T ) {
363
+ skipIfTruncationDisabled (t )
308
364
defer func () {
309
365
if r := recover (); r == nil {
310
366
t .Fatal ("Expected panic for int8 max+1 boundary truncation" )
@@ -315,6 +371,7 @@ func TestBoundaryTruncationInt8MaxPlusOne(t *testing.T) {
315
371
}
316
372
317
373
func TestBoundaryTruncationUint32MaxPlusOne (t * testing.T ) {
374
+ skipIfTruncationDisabled (t )
318
375
defer func () {
319
376
if r := recover (); r == nil {
320
377
t .Fatal ("Expected panic for uint32 max+1 boundary truncation" )
@@ -325,6 +382,7 @@ func TestBoundaryTruncationUint32MaxPlusOne(t *testing.T) {
325
382
}
326
383
327
384
func TestBoundaryTruncationUint16MaxPlusOne (t * testing.T ) {
385
+ skipIfTruncationDisabled (t )
328
386
defer func () {
329
387
if r := recover (); r == nil {
330
388
t .Fatal ("Expected panic for uint16 max+1 boundary truncation" )
@@ -335,6 +393,7 @@ func TestBoundaryTruncationUint16MaxPlusOne(t *testing.T) {
335
393
}
336
394
337
395
func TestBoundaryTruncationUint8MaxPlusOne (t * testing.T ) {
396
+ skipIfTruncationDisabled (t )
338
397
defer func () {
339
398
if r := recover (); r == nil {
340
399
t .Fatal ("Expected panic for uint8 max+1 boundary truncation" )
@@ -346,6 +405,7 @@ func TestBoundaryTruncationUint8MaxPlusOne(t *testing.T) {
346
405
347
406
// Additional edge case truncation tests
348
407
func TestBitOperationTruncation (t * testing.T ) {
408
+ skipIfTruncationDisabled (t )
349
409
defer func () {
350
410
if r := recover (); r == nil {
351
411
t .Fatal ("Expected panic for bit operation truncation" )
@@ -357,6 +417,7 @@ func TestBitOperationTruncation(t *testing.T) {
357
417
}
358
418
359
419
func TestChainedTruncationWithBitOps (t * testing.T ) {
420
+ skipIfTruncationDisabled (t )
360
421
defer func () {
361
422
if r := recover (); r == nil {
362
423
t .Fatal ("Expected panic for chained truncation with bit ops" )
0 commit comments