Skip to content

Commit 3ad2d90

Browse files
committed
Make type switches tranform flow state
1 parent 611f98b commit 3ad2d90

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

go/ql/lib/semmle/go/security/IncorrectIntegerConversionLib.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,26 @@ class TypeAssertionCheck extends DataFlow::ExprNode, FlowStateTransformer {
430430
}
431431
}
432432

433+
/**
434+
* The implicit definition of a variable with integer type for a case clause of
435+
* a type switch statement which declares a variable in its guard, which has
436+
* effectively had a checked type assertion.
437+
*/
438+
class TypeSwitchVarFlowStateTransformer extends DataFlow::SsaNode, FlowStateTransformer {
439+
IntegerType it;
440+
441+
TypeSwitchVarFlowStateTransformer() {
442+
exists(IR::TypeSwitchImplicitVariableInstruction insn, LocalVariable lv | insn.writes(lv, _) |
443+
this.getSourceVariable() = lv and
444+
it = lv.getType()
445+
)
446+
}
447+
448+
override predicate barrierFor(int bitSize, int architectureBitSize) {
449+
integerTypeBound(it, bitSize, architectureBitSize)
450+
}
451+
}
452+
433453
/**
434454
* Holds if `source` is the result of a call to `strconv.Atoi`,
435455
* `strconv.ParseInt`, or `strconv.ParseUint`, `bitSize` is the `bitSize`

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ func typeSwitch1(s string) {
510510
_ = int16(v.(int16))
511511
_ = int8(v.(int16)) // $ hasValueFlow="type assertion"
512512
case int32:
513-
_ = int32(v) // $ SPURIOUS: hasValueFlow="v"
514-
_ = int8(v) // $ hasValueFlow="v"
513+
_ = int32(v)
514+
_ = int8(v) // $ hasValueFlow="v"
515515
case int64:
516516
_ = int8(v) // $ hasValueFlow="v"
517517
default:

0 commit comments

Comments
 (0)