Skip to content

Commit ec2c58b

Browse files
committed
Swift: Understand overflow binary bitwise operations.
1 parent 1206b73 commit ec2c58b

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

swift/ql/lib/codeql/swift/elements/expr/BitwiseOperation.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,22 @@ class XorBitwiseExpr extends BinaryExpr {
7373
* A bitwise shift left expression.
7474
* ```
7575
* a << b
76+
* a &<<
7677
* ```
7778
*/
7879
class ShiftLeftBitwiseExpr extends BinaryExpr {
79-
ShiftLeftBitwiseExpr() { this.getStaticTarget().getName() = "<<(_:_:)" }
80+
ShiftLeftBitwiseExpr() { this.getStaticTarget().getName() = ["<<(_:_:)", "&<<(_:_:)"] }
8081
}
8182

8283
/**
8384
* A bitwise shift right expression.
8485
* ```
8586
* a >> b
87+
* a &>>
8688
* ```
8789
*/
8890
class ShiftRightBitwiseExpr extends BinaryExpr {
89-
ShiftRightBitwiseExpr() { this.getStaticTarget().getName() = ">>(_:_:)" }
91+
ShiftRightBitwiseExpr() { this.getStaticTarget().getName() = [">>(_:_:)", "&>>(_:_:)"] }
9092
}
9193

9294
/**

swift/ql/test/library-tests/elements/expr/bitwiseopration/bitwiseoperation.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
| bitwiseoperation.swift:5:7:5:11 | ... .^(_:_:) ... | BinaryBitwiseOperation, XorBitwiseExpr |
55
| bitwiseoperation.swift:6:7:6:12 | ... .<<(_:_:) ... | BinaryBitwiseOperation, ShiftLeftBitwiseExpr |
66
| bitwiseoperation.swift:7:7:7:12 | ... .>>(_:_:) ... | BinaryBitwiseOperation, ShiftRightBitwiseExpr |
7+
| bitwiseoperation.swift:10:7:10:13 | ... .&<<(_:_:) ... | BinaryBitwiseOperation, ShiftLeftBitwiseExpr |
8+
| bitwiseoperation.swift:11:7:11:13 | ... .&>>(_:_:) ... | BinaryBitwiseOperation, ShiftRightBitwiseExpr |

swift/ql/test/library-tests/elements/expr/bitwiseopration/bitwiseoperation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ func bitwise() {
77
_ = 1 >> 0
88

99
// bitwise operations with overflow
10-
_ = 1 &<< 1 // NOT DETECTED
11-
_ = 1 &>> 1 // NOT DETECTED
10+
_ = 1 &<< 1
11+
_ = 1 &>> 1
1212

1313
// pointwise bitwise operations
1414
let a = SIMD4<Int>(1, 2, 3, 4)

0 commit comments

Comments
 (0)