Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f08a598

Browse files
committed
Add tests for FPs: type switches, type assertions
1 parent da3fa22 commit f08a598

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

go/ql/test/query-tests/Security/CWE-681/IncorrectIntegerConversion.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,55 @@ func dealWithArchSizeCorrectly(s string) uint {
498498
}
499499
return 0
500500
}
501+
502+
func typeSwitch1(s string) {
503+
i64, _ := strconv.ParseInt(s, 10, 64)
504+
var input any = i64
505+
switch v := input.(type) {
506+
case int16, string:
507+
if _, ok := input.(string); ok {
508+
return
509+
}
510+
_ = int16(v.(int16)) // $ SPURIOUS: hasValueFlow="type assertion"
511+
_ = int8(v.(int16)) // $ hasValueFlow="type assertion"
512+
case int32:
513+
_ = int32(v) // $ SPURIOUS: hasValueFlow="v"
514+
_ = int8(v) // $ hasValueFlow="v"
515+
case int64:
516+
_ = int8(v) // $ hasValueFlow="v"
517+
default:
518+
_ = int8(v.(int64)) // $ hasValueFlow="type assertion"
519+
}
520+
}
521+
522+
func typeSwitch2(s string) {
523+
i64, _ := strconv.ParseInt(s, 10, 64)
524+
var input any = i64
525+
switch input.(type) {
526+
case int16, string:
527+
if _, ok := input.(string); ok {
528+
return
529+
}
530+
_ = int16(input.(int16)) // $ SPURIOUS: hasValueFlow="type assertion"
531+
_ = int8(input.(int16)) // $ hasValueFlow="type assertion"
532+
case int32:
533+
_ = int32(input.(int32)) // $ SPURIOUS: hasValueFlow="type assertion"
534+
_ = int8(input.(int32)) // $ hasValueFlow="type assertion"
535+
case int64:
536+
_ = int8(input.(int64)) // $ hasValueFlow="type assertion"
537+
default:
538+
_ = int8(input.(int64)) // $ hasValueFlow="type assertion"
539+
}
540+
}
541+
542+
func checkedTypeAssertion(s string) {
543+
i64, _ := strconv.ParseInt(s, 10, 64)
544+
var input any = i64
545+
if v, ok := input.(int16); ok {
546+
// Need to account for the fact that within this case clause, v is an int16
547+
_ = int16(v) // $ SPURIOUS: hasValueFlow="v"
548+
_ = int8(v) // $ hasValueFlow="v"
549+
} else if v, ok := input.(int32); ok {
550+
_ = int16(v) // $ hasValueFlow="v"
551+
}
552+
}

0 commit comments

Comments
 (0)