Skip to content

Commit bd2812c

Browse files
committed
Rust: Only resolve deref methods on references to avoid blowup
1 parent 6b2c125 commit bd2812c

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,15 @@ final class MethodCall extends Call {
11571157
(
11581158
path0.isCons(TRefTypeParameter(), path)
11591159
or
1160+
(
1161+
not path0.isCons(TRefTypeParameter(), _) and
1162+
not (path0.isEmpty() and result = TRefType())
1163+
or
1164+
// Ideally we should find all methods on reference types, but as
1165+
// that currently causes a blowup we limit this to the `deref`
1166+
// method in order to make dereferencing work.
1167+
this.getMethodName() = "deref"
1168+
) and
11601169
path = path0
11611170
)
11621171
|

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,14 +1157,14 @@ mod method_call_type_conversion {
11571157
// https://doc.rust-lang.org/std/string/struct.String.html#deref.
11581158
let u = x9.parse::<u32>(); // $ method=parse type=u:T.u32
11591159

1160-
let my_thing = &MyInt { a: 37 }; // $ SPURIOUS: type=my_thing:&T.&T.MyInt
1160+
let my_thing = &MyInt { a: 37 };
11611161
// implicit borrow of a `&`
1162-
let a = my_thing.method_on_borrow(); // $ method=MyInt::method_on_borrow
1162+
let a = my_thing.method_on_borrow(); // $ MISSING: method=MyInt::method_on_borrow
11631163
println!("{:?}", a);
11641164

11651165
// no implicit borrow
11661166
let my_thing = &MyInt { a: 38 };
1167-
let a = my_thing.method_not_on_borrow(); // $ method=MyInt::method_not_on_borrow
1167+
let a = my_thing.method_not_on_borrow(); // $ MISSING: method=MyInt::method_not_on_borrow
11681168
println!("{:?}", a);
11691169
}
11701170
}

rust/ql/test/library-tests/type-inference/type-inference.expected

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,39 +1675,25 @@ inferType
16751675
| main.rs:1158:17:1158:33 | x9.parse() | | {EXTERNAL LOCATION} | Result |
16761676
| main.rs:1158:17:1158:33 | x9.parse() | T | {EXTERNAL LOCATION} | u32 |
16771677
| main.rs:1160:13:1160:20 | my_thing | | file://:0:0:0:0 | & |
1678-
| main.rs:1160:13:1160:20 | my_thing | &T | file://:0:0:0:0 | & |
16791678
| main.rs:1160:13:1160:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1680-
| main.rs:1160:13:1160:20 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
16811679
| main.rs:1160:24:1160:39 | &... | | file://:0:0:0:0 | & |
1682-
| main.rs:1160:24:1160:39 | &... | &T | file://:0:0:0:0 | & |
16831680
| main.rs:1160:24:1160:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
1684-
| main.rs:1160:24:1160:39 | &... | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1685-
| main.rs:1160:25:1160:39 | MyInt {...} | | file://:0:0:0:0 | & |
16861681
| main.rs:1160:25:1160:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
1687-
| main.rs:1160:25:1160:39 | MyInt {...} | &T | main.rs:1082:5:1085:5 | MyInt |
16881682
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i32 |
16891683
| main.rs:1160:36:1160:37 | 37 | | {EXTERNAL LOCATION} | i64 |
1690-
| main.rs:1162:13:1162:13 | a | | {EXTERNAL LOCATION} | i64 |
16911684
| main.rs:1162:17:1162:24 | my_thing | | file://:0:0:0:0 | & |
1692-
| main.rs:1162:17:1162:24 | my_thing | &T | file://:0:0:0:0 | & |
16931685
| main.rs:1162:17:1162:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1694-
| main.rs:1162:17:1162:24 | my_thing | &T.&T | main.rs:1082:5:1085:5 | MyInt |
1695-
| main.rs:1162:17:1162:43 | my_thing.method_on_borrow() | | {EXTERNAL LOCATION} | i64 |
16961686
| main.rs:1163:18:1163:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1697-
| main.rs:1163:26:1163:26 | a | | {EXTERNAL LOCATION} | i64 |
16981687
| main.rs:1166:13:1166:20 | my_thing | | file://:0:0:0:0 | & |
16991688
| main.rs:1166:13:1166:20 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
17001689
| main.rs:1166:24:1166:39 | &... | | file://:0:0:0:0 | & |
17011690
| main.rs:1166:24:1166:39 | &... | &T | main.rs:1082:5:1085:5 | MyInt |
17021691
| main.rs:1166:25:1166:39 | MyInt {...} | | main.rs:1082:5:1085:5 | MyInt |
17031692
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i32 |
17041693
| main.rs:1166:36:1166:37 | 38 | | {EXTERNAL LOCATION} | i64 |
1705-
| main.rs:1167:13:1167:13 | a | | {EXTERNAL LOCATION} | i64 |
17061694
| main.rs:1167:17:1167:24 | my_thing | | file://:0:0:0:0 | & |
17071695
| main.rs:1167:17:1167:24 | my_thing | &T | main.rs:1082:5:1085:5 | MyInt |
1708-
| main.rs:1167:17:1167:47 | my_thing.method_not_on_borrow() | | {EXTERNAL LOCATION} | i64 |
17091696
| main.rs:1168:18:1168:23 | "{:?}\\n" | | {EXTERNAL LOCATION} | str |
1710-
| main.rs:1168:26:1168:26 | a | | {EXTERNAL LOCATION} | i64 |
17111697
| main.rs:1175:16:1175:20 | SelfParam | | file://:0:0:0:0 | & |
17121698
| main.rs:1175:16:1175:20 | SelfParam | &T | main.rs:1173:5:1181:5 | Self [trait MyTrait] |
17131699
| main.rs:1178:16:1178:20 | SelfParam | | file://:0:0:0:0 | & |

rust/ql/test/query-tests/security/CWE-312/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,3 @@ multipleMethodCallTargets
55
| test_logging.rs:78:22:78:38 | password.as_str() | file://:0:0:0:0 | fn as_str |
66
| test_logging.rs:88:18:88:34 | password.as_str() | file://:0:0:0:0 | fn as_str |
77
| test_logging.rs:88:18:88:34 | password.as_str() | file://:0:0:0:0 | fn as_str |
8-
| test_logging.rs:103:12:103:44 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |
9-
| test_logging.rs:103:12:103:44 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |
10-
| test_logging.rs:107:14:107:46 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |
11-
| test_logging.rs:107:14:107:46 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |
12-
| test_logging.rs:111:12:111:44 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |
13-
| test_logging.rs:111:12:111:44 | ... .write_fmt(...) | file://:0:0:0:0 | fn write_fmt |

rust/ql/test/query-tests/security/CWE-770/CONSISTENCY/PathResolutionConsistency.expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
multiplePathResolutions
2-
| main.rs:218:14:218:17 | libc | file://:0:0:0:0 | Crate([email protected]) |
32
| main.rs:218:14:218:17 | libc | file://:0:0:0:0 | Crate([email protected]) |
3+
| main.rs:218:14:218:17 | libc | file://:0:0:0:0 | Crate([email protected]) |
44
| main.rs:218:14:218:25 | ...::malloc | file://:0:0:0:0 | fn malloc |
55
| main.rs:218:14:218:25 | ...::malloc | file://:0:0:0:0 | fn malloc |
6-
| main.rs:219:13:219:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
76
| main.rs:219:13:219:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
7+
| main.rs:219:13:219:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
88
| main.rs:219:13:219:24 | ...::malloc | file://:0:0:0:0 | fn malloc |
99
| main.rs:219:13:219:24 | ...::malloc | file://:0:0:0:0 | fn malloc |
10-
| main.rs:220:13:220:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
1110
| main.rs:220:13:220:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
11+
| main.rs:220:13:220:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
1212
| main.rs:220:13:220:31 | ...::aligned_alloc | file://:0:0:0:0 | fn aligned_alloc |
1313
| main.rs:220:13:220:31 | ...::aligned_alloc | file://:0:0:0:0 | fn aligned_alloc |
14-
| main.rs:221:13:221:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
1514
| main.rs:221:13:221:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
15+
| main.rs:221:13:221:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
1616
| main.rs:221:13:221:31 | ...::aligned_alloc | file://:0:0:0:0 | fn aligned_alloc |
1717
| main.rs:221:13:221:31 | ...::aligned_alloc | file://:0:0:0:0 | fn aligned_alloc |
18-
| main.rs:222:13:222:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
1918
| main.rs:222:13:222:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
19+
| main.rs:222:13:222:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
2020
| main.rs:222:13:222:24 | ...::calloc | file://:0:0:0:0 | fn calloc |
2121
| main.rs:222:13:222:24 | ...::calloc | file://:0:0:0:0 | fn calloc |
22-
| main.rs:223:13:223:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
2322
| main.rs:223:13:223:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
23+
| main.rs:223:13:223:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
2424
| main.rs:223:13:223:24 | ...::calloc | file://:0:0:0:0 | fn calloc |
2525
| main.rs:223:13:223:24 | ...::calloc | file://:0:0:0:0 | fn calloc |
26-
| main.rs:224:13:224:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
2726
| main.rs:224:13:224:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
27+
| main.rs:224:13:224:16 | libc | file://:0:0:0:0 | Crate([email protected]) |
2828
| main.rs:224:13:224:25 | ...::realloc | file://:0:0:0:0 | fn realloc |
2929
| main.rs:224:13:224:25 | ...::realloc | file://:0:0:0:0 | fn realloc |
3030
| main.rs:229:13:229:37 | ...::with_capacity | file://:0:0:0:0 | fn with_capacity |

0 commit comments

Comments
 (0)