Skip to content

Commit 7a9ddc2

Browse files
committed
Ruby: address some more feedback on array flow summaries
1 parent ed00f2b commit 7a9ddc2

File tree

3 files changed

+6563
-6440
lines changed

3 files changed

+6563
-6440
lines changed

ruby/ql/lib/codeql/ruby/frameworks/Array.qll

Lines changed: 121 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ module Array {
843843
"ArrayElement of Receiver", "ArrayElement of ArrayElement of Receiver",
844844
"ArrayElement of ArrayElement of ArrayElement of Receiver"
845845
] and
846-
output = "ArrayElement[?] of Receiver"
846+
output = ["ArrayElement[?] of Receiver", "ArrayElement[?] of ReturnValue"]
847847
) and
848848
preservesValue = true
849849
}
@@ -1142,6 +1142,25 @@ module Array {
11421142
}
11431143
}
11441144

1145+
private class RejectBangSummary extends SimpleSummarizedCallable {
1146+
RejectBangSummary() { this = "reject!" }
1147+
1148+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1149+
input = "ArrayElement of Receiver" and
1150+
output =
1151+
[
1152+
"ArrayElement[?] of ReturnValue", "ArrayElement[?] of Receiver",
1153+
"Parameter[0] of BlockArgument"
1154+
] and
1155+
preservesValue = true
1156+
}
1157+
1158+
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
1159+
pos.isSelf() and
1160+
content instanceof DataFlow::Content::ArrayElementContent
1161+
}
1162+
}
1163+
11451164
private class ReplaceSummary extends SimpleSummarizedCallable {
11461165
ReplaceSummary() { this = "replace" }
11471166

@@ -1507,14 +1526,6 @@ module Array {
15071526
)
15081527
)
15091528
}
1510-
1511-
predicate debugDeleteMe(MethodCall c, string input, string output, int s, int e, int ln) {
1512-
c = mc and
1513-
s = start and
1514-
e = end and
1515-
propagatesFlowExt(input, output, _) and
1516-
ln = mc.getLocation().getStartLine()
1517-
}
15181529
}
15191530

15201531
/**
@@ -1548,6 +1559,100 @@ module Array {
15481559
}
15491560
}
15501561

1562+
private class SortBangSummary extends SimpleSummarizedCallable {
1563+
SortBangSummary() { this = "sort!" }
1564+
1565+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1566+
input = "ArrayElement of Receiver" and
1567+
output =
1568+
[
1569+
"Parameter[0] of BlockArgument", "Parameter[1] of BlockArgument",
1570+
"ArrayElement[?] of Receiver", "ArrayElement[?] of ReturnValue"
1571+
] and
1572+
preservesValue = true
1573+
}
1574+
1575+
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
1576+
pos.isSelf() and
1577+
content instanceof DataFlow::Content::KnownArrayElementContent
1578+
}
1579+
}
1580+
1581+
private class SortByBangSummary extends SimpleSummarizedCallable {
1582+
SortByBangSummary() { this = "sort_by!" }
1583+
1584+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1585+
input = "ArrayElement of Receiver" and
1586+
output =
1587+
[
1588+
"Parameter[0] of BlockArgument", "ArrayElement[?] of Receiver",
1589+
"ArrayElement[?] of ReturnValue"
1590+
] and
1591+
preservesValue = true
1592+
}
1593+
1594+
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
1595+
pos.isSelf() and
1596+
content instanceof DataFlow::Content::KnownArrayElementContent
1597+
}
1598+
}
1599+
1600+
private class TransposeSummary extends SimpleSummarizedCallable {
1601+
TransposeSummary() { this = "transpose" }
1602+
1603+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1604+
preservesValue = true and
1605+
(
1606+
input = "ArrayElement[?] of ArrayElement[?] of Receiver" and
1607+
output = "ArrayElement[?] of ArrayElement[?] of ReturnValue"
1608+
or
1609+
exists(ArrayIndex i, ArrayIndex j |
1610+
input = "ArrayElement[" + i + "] of ArrayElement[" + j + "] of Receiver" and
1611+
output = "ArrayElement[" + j + "] of ArrayElement[" + i + "] of ReturnValue"
1612+
)
1613+
)
1614+
}
1615+
}
1616+
1617+
private class UniqBangSummary extends SimpleSummarizedCallable {
1618+
UniqBangSummary() { this = "uniq!" }
1619+
1620+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1621+
input = "ArrayElement of Receiver" and
1622+
output =
1623+
[
1624+
"ArrayElement[?] of Receiver", "ArrayElement[?] of ReturnValue",
1625+
"Parameter[0] of BlockArgument"
1626+
] and
1627+
preservesValue = true
1628+
}
1629+
1630+
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
1631+
pos.isSelf() and
1632+
content instanceof DataFlow::Content::KnownArrayElementContent
1633+
}
1634+
}
1635+
1636+
private class UnionSummary extends SummarizedCallable {
1637+
MethodCall mc;
1638+
1639+
UnionSummary() { this = "union" and mc.getMethodName() = this }
1640+
1641+
override MethodCall getACall() { result = mc }
1642+
1643+
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
1644+
(
1645+
input = "ArrayElement of Receiver"
1646+
or
1647+
exists(int i | i in [0 .. mc.getNumberOfArguments() - 1] |
1648+
input = "ArrayElement of Argument[" + i + "]"
1649+
)
1650+
) and
1651+
output = "ArrayElement[?] of ReturnValue" and
1652+
preservesValue = true
1653+
}
1654+
}
1655+
15511656
abstract private class ValuesAtSummary extends SummarizedCallable {
15521657
MethodCall mc;
15531658

@@ -1656,7 +1761,7 @@ module Enumerable {
16561761
output = "Parameter[0] of BlockArgument" and
16571762
preservesValue = true
16581763
or
1659-
input = "ArrayElement of ReturnValue of BlockArgument" and
1764+
input = ["ArrayElement of ReturnValue of BlockArgument", "ReturnValue of BlockArgument"] and
16601765
output = "ArrayElement[?] of ReturnValue" and
16611766
preservesValue = true
16621767
}
@@ -1997,6 +2102,8 @@ module Enumerable {
19972102
or
19982103
exists(ArrayIndex i | i > 0 | input = "ArrayElement[" + i + "] of Receiver") and
19992104
output = "Parameter[1] of BlockArgument"
2105+
or
2106+
input = "ReturnValue of BlockArgument" and output = "ReturnValue"
20002107
) and
20012108
preservesValue = true
20022109
}
@@ -2014,6 +2121,8 @@ module Enumerable {
20142121
// Each element in the receiver is passed to the second block parameter.
20152122
exists(ArrayIndex i | input = "ArrayElement[" + i + "] of Receiver") and
20162123
output = "Parameter[1] of BlockArgument"
2124+
or
2125+
input = "ReturnValue of BlockArgument" and output = "ReturnValue"
20172126
) and
20182127
preservesValue = true
20192128
}
@@ -2203,25 +2312,6 @@ module Enumerable {
22032312
}
22042313
}
22052314

2206-
private class RejectBangSummary extends SimpleSummarizedCallable {
2207-
RejectBangSummary() { this = "reject!" }
2208-
2209-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2210-
input = "ArrayElement of Receiver" and
2211-
output =
2212-
[
2213-
"ArrayElement[?] of ReturnValue", "ArrayElement[?] of Receiver",
2214-
"Parameter[0] of BlockArgument"
2215-
] and
2216-
preservesValue = true
2217-
}
2218-
2219-
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
2220-
pos.isSelf() and
2221-
content instanceof DataFlow::Content::ArrayElementContent
2222-
}
2223-
}
2224-
22252315
private class SelectSummary extends SimpleSummarizedCallable {
22262316
// `find_all` and `filter` are aliases of `select`.
22272317
SelectSummary() { this = ["select", "find_all", "filter"] }
@@ -2267,25 +2357,6 @@ module Enumerable {
22672357
}
22682358
}
22692359

2270-
private class SortBangSummary extends SimpleSummarizedCallable {
2271-
SortBangSummary() { this = "sort!" }
2272-
2273-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2274-
input = "ArrayElement of Receiver" and
2275-
output =
2276-
[
2277-
"Parameter[0] of BlockArgument", "Parameter[1] of BlockArgument",
2278-
"ArrayElement[?] of Receiver", "ArrayElement[?] of ReturnValue"
2279-
] and
2280-
preservesValue = true
2281-
}
2282-
2283-
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
2284-
pos.isSelf() and
2285-
content instanceof DataFlow::Content::KnownArrayElementContent
2286-
}
2287-
}
2288-
22892360
private class SortBySummary extends SimpleSummarizedCallable {
22902361
SortBySummary() { this = "sort_by" }
22912362

@@ -2296,25 +2367,6 @@ module Enumerable {
22962367
}
22972368
}
22982369

2299-
private class SortByBangSummary extends SimpleSummarizedCallable {
2300-
SortByBangSummary() { this = "sort_by!" }
2301-
2302-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2303-
input = "ArrayElement of Receiver" and
2304-
output =
2305-
[
2306-
"Parameter[0] of BlockArgument", "ArrayElement[?] of Receiver",
2307-
"ArrayElement[?] of ReturnValue"
2308-
] and
2309-
preservesValue = true
2310-
}
2311-
2312-
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
2313-
pos.isSelf() and
2314-
content instanceof DataFlow::Content::KnownArrayElementContent
2315-
}
2316-
}
2317-
23182370
private class SumSummary extends SimpleSummarizedCallable {
23192371
SumSummary() { this = "sum" }
23202372

@@ -2377,7 +2429,7 @@ module Enumerable {
23772429

23782430
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
23792431
input = "ArrayElement of Receiver" and
2380-
output = ["Parameter[0] of BlockArgument"] and
2432+
output = "Parameter[0] of BlockArgument" and
23812433
preservesValue = true
23822434
or
23832435
// We can't know the size of the return value, but we know that indices
@@ -2401,43 +2453,6 @@ module Enumerable {
24012453
}
24022454
}
24032455

2404-
private class TransposeSummary extends SimpleSummarizedCallable {
2405-
TransposeSummary() { this = "transpose" }
2406-
2407-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2408-
preservesValue = true and
2409-
(
2410-
input = "ArrayElement[?] of ArrayElement[?] of Receiver" and
2411-
output = "ArrayElement[?] of ArrayElement[?] of ReturnValue"
2412-
or
2413-
exists(ArrayIndex i, ArrayIndex j |
2414-
input = "ArrayElement[" + i + "] of ArrayElement[" + j + "] of Receiver" and
2415-
output = "ArrayElement[" + j + "] of ArrayElement[" + i + "] of ReturnValue"
2416-
)
2417-
)
2418-
}
2419-
}
2420-
2421-
private class UnionSummary extends SummarizedCallable {
2422-
MethodCall mc;
2423-
2424-
UnionSummary() { this = "union" and mc.getMethodName() = this }
2425-
2426-
override MethodCall getACall() { result = mc }
2427-
2428-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2429-
(
2430-
input = "ArrayElement of Receiver"
2431-
or
2432-
exists(int i | i in [0 .. mc.getNumberOfArguments() - 1] |
2433-
input = "ArrayElement of Argument[" + i + "]"
2434-
)
2435-
) and
2436-
output = "ArrayElement[?] of ReturnValue" and
2437-
preservesValue = true
2438-
}
2439-
}
2440-
24412456
private class UniqSummary extends SimpleSummarizedCallable {
24422457
UniqSummary() { this = "uniq" }
24432458

@@ -2448,25 +2463,6 @@ module Enumerable {
24482463
}
24492464
}
24502465

2451-
private class UniqBangSummary extends SimpleSummarizedCallable {
2452-
UniqBangSummary() { this = "uniq!" }
2453-
2454-
override predicate propagatesFlowExt(string input, string output, boolean preservesValue) {
2455-
input = "ArrayElement of Receiver" and
2456-
output =
2457-
[
2458-
"ArrayElement[?] of Receiver", "ArrayElement[?] of ReturnValue",
2459-
"Parameter[0] of BlockArgument"
2460-
] and
2461-
preservesValue = true
2462-
}
2463-
2464-
override predicate clearsContent(ParameterPosition pos, DataFlow::Content content) {
2465-
pos.isSelf() and
2466-
content instanceof DataFlow::Content::KnownArrayElementContent
2467-
}
2468-
}
2469-
24702466
abstract private class ZipSummary extends SummarizedCallable {
24712467
MethodCall mc;
24722468

@@ -2504,7 +2500,7 @@ module Enumerable {
25042500
output = "ArrayElement[0] of ArrayElement[" + i + "] of ReturnValue"
25052501
)
25062502
or
2507-
// receiver[?] -> return_value[0][?]
2503+
// receiver[?] -> return_value[?][0]
25082504
input = "ArrayElement[?] of Receiver" and
25092505
output = "ArrayElement[0] of ArrayElement[?] of ReturnValue"
25102506
or

0 commit comments

Comments
 (0)