Skip to content

Commit a273917

Browse files
committed
Merge branch 'master' into init-dynamic-alloc-newexpr
2 parents 8fdc4b0 + 4825774 commit a273917

File tree

120 files changed

+1490
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+1490
-314
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
.vs/*
1515
!.vs/VSWorkspaceSettings.json
1616

17+
# Byte-compiled python files
18+
*.pyc
19+
1720
# It's useful (though not required) to be able to unpack codeql in the ql checkout itself
1821
/codeql/
1922
.vscode/settings.json

change-notes/1.24/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following changes in version 1.24 affect C/C++ analysis in all applications.
2020
| Memory may not be freed (`cpp/memory-may-not-be-freed`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
2121
| Mismatching new/free or malloc/delete (`cpp/new-free-mismatch`) | Fewer false positive results | Fixed false positive results in template code. |
2222
| Missing return statement (`cpp/missing-return`) | Fewer false positive results | Functions containing `asm` statements are no longer highlighted by this query. |
23+
| Missing return statement (`cpp/missing-return`) | More accurate locations | Locations reported by this query are now more accurate in some cases. |
2324
| No space for zero terminator (`cpp/no-space-for-terminator`) | More correct results | String arguments to formatting functions are now (usually) expected to be null terminated strings. |
2425
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | | This query is no longer run on LGTM. |
2526
| No space for zero terminator (`cpp/no-space-for-terminator`) | Fewer false positive results | This query has been modified to be more conservative when identifying which pointers point to null-terminated strings. This approach produces fewer, more accurate results. |

config/sync-files.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env python3
2+
3+
# Due to various technical limitations, we sometimes have files that need to be
4+
# kept identical in the repository. This script loads a database of such
5+
# files and can perform two functions: check whether they are still identical,
6+
# and overwrite the others with a master copy if needed.
7+
8+
import hashlib
9+
import shutil
10+
import os
11+
import sys
12+
import json
13+
import re
14+
path = os.path
15+
16+
file_groups = {}
17+
18+
def add_prefix(prefix, relative):
19+
result = path.join(prefix, relative)
20+
if path.commonprefix((path.realpath(result), path.realpath(prefix))) != \
21+
path.realpath(prefix):
22+
raise Exception("Path {} is not below {}".format(
23+
result, prefix))
24+
return result
25+
26+
def load_if_exists(prefix, json_file_relative):
27+
json_file_name = path.join(prefix, json_file_relative)
28+
if path.isfile(json_file_name):
29+
print("Loading file groups from", json_file_name)
30+
with open(json_file_name, 'r', encoding='utf-8') as fp:
31+
raw_groups = json.load(fp)
32+
prefixed_groups = {
33+
name: [
34+
add_prefix(prefix, relative)
35+
for relative in relatives
36+
]
37+
for name, relatives in raw_groups.items()
38+
}
39+
file_groups.update(prefixed_groups)
40+
41+
# Generates a list of C# test files that should be in sync
42+
def csharp_test_files():
43+
test_file_re = re.compile('.*(Bad|Good)[0-9]*\\.cs$')
44+
csharp_doc_files = {
45+
file:os.path.join(root, file)
46+
for root, dirs, files in os.walk("csharp/ql/src")
47+
for file in files
48+
if test_file_re.match(file)
49+
}
50+
return {
51+
"C# test '" + file + "'" : [os.path.join(root, file), csharp_doc_files[file]]
52+
for root, dirs, files in os.walk("csharp/ql/test")
53+
for file in files
54+
if file in csharp_doc_files
55+
}
56+
57+
def file_checksum(filename):
58+
with open(filename, 'rb') as file_handle:
59+
return hashlib.sha1(file_handle.read()).hexdigest()
60+
61+
def check_group(group_name, files, master_file_picker, emit_error):
62+
checksums = {file_checksum(f) for f in files}
63+
64+
if len(checksums) == 1:
65+
return
66+
67+
master_file = master_file_picker(files)
68+
if master_file is None:
69+
emit_error(__file__, 0,
70+
"Files from group '"+ group_name +"' not in sync.")
71+
emit_error(__file__, 0,
72+
"Run this script with a file-name argument among the "
73+
"following to overwrite the remaining files with the contents "
74+
"of that file or run with the --latest switch to update each "
75+
"group of files from the most recently modified file in the group.")
76+
for filename in files:
77+
emit_error(__file__, 0, " " + filename)
78+
else:
79+
print(" Syncing others from", master_file)
80+
for filename in files:
81+
if filename == master_file:
82+
continue
83+
print(" " + filename)
84+
os.replace(filename, filename + '~')
85+
shutil.copy(master_file, filename)
86+
print(" Backups written with '~' appended to file names")
87+
88+
def chdir_repo_root():
89+
root_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
90+
os.chdir(root_path)
91+
92+
def choose_master_file(master_file, files):
93+
if master_file in files:
94+
return master_file
95+
else:
96+
return None
97+
98+
def choose_latest_file(files):
99+
latest_time = None
100+
latest_file = None
101+
for filename in files:
102+
file_time = os.path.getmtime(filename)
103+
if (latest_time is None) or (latest_time < file_time):
104+
latest_time = file_time
105+
latest_file = filename
106+
return latest_file
107+
108+
local_error_count = 0
109+
def emit_local_error(path, line, error):
110+
print('ERROR: ' + path + ':' + line + " - " + error)
111+
global local_error_count
112+
local_error_count += 1
113+
114+
# This function is invoked directly by a CI script, which passes a different error-handling
115+
# callback.
116+
def sync_identical_files(emit_error):
117+
if len(sys.argv) == 1:
118+
master_file_picker = lambda files: None
119+
elif len(sys.argv) == 2:
120+
if sys.argv[1] == "--latest":
121+
master_file_picker = choose_latest_file
122+
elif os.path.isfile(sys.argv[1]):
123+
master_file_picker = lambda files: choose_master_file(sys.argv[1], files)
124+
else:
125+
raise Exception("File not found")
126+
else:
127+
raise Exception("Bad command line or file not found")
128+
chdir_repo_root()
129+
load_if_exists('.', 'config/identical-files.json')
130+
file_groups.update(csharp_test_files())
131+
for group_name, files in file_groups.items():
132+
check_group(group_name, files, master_file_picker, emit_error)
133+
134+
def main():
135+
sync_identical_files(emit_local_error)
136+
if local_error_count > 0:
137+
exit(1)
138+
139+
if __name__ == "__main__":
140+
main()

cpp/ql/src/jsf/4.13 Functions/AV Rule 114.ql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ predicate functionsMissingReturnStmt(Function f, ControlFlowNode blame) {
3030
) and
3131
exists(ReturnStmt s |
3232
f.getAPredecessor() = s and
33-
blame = s.getAPredecessor()
33+
(
34+
blame = s.getAPredecessor() and
35+
count(blame.getASuccessor()) = 1
36+
or
37+
blame = s and
38+
exists(ControlFlowNode pred | pred = s.getAPredecessor() | count(pred.getASuccessor()) != 1)
39+
)
3440
)
3541
}
3642

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,8 @@ private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
20892089

20902090
SummaryCtxSome() { this = TSummaryCtxSome(p, ap) }
20912091

2092+
int getParameterPos() { p.isParameterOf(_, result) }
2093+
20922094
override string toString() { result = p + ": " + ap }
20932095

20942096
predicate hasLocationInfo(
@@ -2482,13 +2484,15 @@ pragma[nomagic]
24822484
private predicate paramFlowsThrough(
24832485
ReturnKindExt kind, CallContextCall cc, SummaryCtxSome sc, AccessPath ap, Configuration config
24842486
) {
2485-
exists(PathNodeMid mid, ReturnNodeExt ret |
2487+
exists(PathNodeMid mid, ReturnNodeExt ret, int pos |
24862488
mid.getNode() = ret and
24872489
kind = ret.getKind() and
24882490
cc = mid.getCallContext() and
24892491
sc = mid.getSummaryCtx() and
24902492
config = mid.getConfiguration() and
2491-
ap = mid.getAp()
2493+
ap = mid.getAp() and
2494+
pos = sc.getParameterPos() and
2495+
not kind.(ParamUpdateReturnKind).getPosition() = pos
24922496
)
24932497
}
24942498

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl2.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,8 @@ private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
20892089

20902090
SummaryCtxSome() { this = TSummaryCtxSome(p, ap) }
20912091

2092+
int getParameterPos() { p.isParameterOf(_, result) }
2093+
20922094
override string toString() { result = p + ": " + ap }
20932095

20942096
predicate hasLocationInfo(
@@ -2482,13 +2484,15 @@ pragma[nomagic]
24822484
private predicate paramFlowsThrough(
24832485
ReturnKindExt kind, CallContextCall cc, SummaryCtxSome sc, AccessPath ap, Configuration config
24842486
) {
2485-
exists(PathNodeMid mid, ReturnNodeExt ret |
2487+
exists(PathNodeMid mid, ReturnNodeExt ret, int pos |
24862488
mid.getNode() = ret and
24872489
kind = ret.getKind() and
24882490
cc = mid.getCallContext() and
24892491
sc = mid.getSummaryCtx() and
24902492
config = mid.getConfiguration() and
2491-
ap = mid.getAp()
2493+
ap = mid.getAp() and
2494+
pos = sc.getParameterPos() and
2495+
not kind.(ParamUpdateReturnKind).getPosition() = pos
24922496
)
24932497
}
24942498

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl3.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,8 @@ private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
20892089

20902090
SummaryCtxSome() { this = TSummaryCtxSome(p, ap) }
20912091

2092+
int getParameterPos() { p.isParameterOf(_, result) }
2093+
20922094
override string toString() { result = p + ": " + ap }
20932095

20942096
predicate hasLocationInfo(
@@ -2482,13 +2484,15 @@ pragma[nomagic]
24822484
private predicate paramFlowsThrough(
24832485
ReturnKindExt kind, CallContextCall cc, SummaryCtxSome sc, AccessPath ap, Configuration config
24842486
) {
2485-
exists(PathNodeMid mid, ReturnNodeExt ret |
2487+
exists(PathNodeMid mid, ReturnNodeExt ret, int pos |
24862488
mid.getNode() = ret and
24872489
kind = ret.getKind() and
24882490
cc = mid.getCallContext() and
24892491
sc = mid.getSummaryCtx() and
24902492
config = mid.getConfiguration() and
2491-
ap = mid.getAp()
2493+
ap = mid.getAp() and
2494+
pos = sc.getParameterPos() and
2495+
not kind.(ParamUpdateReturnKind).getPosition() = pos
24922496
)
24932497
}
24942498

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImpl4.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,8 @@ private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
20892089

20902090
SummaryCtxSome() { this = TSummaryCtxSome(p, ap) }
20912091

2092+
int getParameterPos() { p.isParameterOf(_, result) }
2093+
20922094
override string toString() { result = p + ": " + ap }
20932095

20942096
predicate hasLocationInfo(
@@ -2482,13 +2484,15 @@ pragma[nomagic]
24822484
private predicate paramFlowsThrough(
24832485
ReturnKindExt kind, CallContextCall cc, SummaryCtxSome sc, AccessPath ap, Configuration config
24842486
) {
2485-
exists(PathNodeMid mid, ReturnNodeExt ret |
2487+
exists(PathNodeMid mid, ReturnNodeExt ret, int pos |
24862488
mid.getNode() = ret and
24872489
kind = ret.getKind() and
24882490
cc = mid.getCallContext() and
24892491
sc = mid.getSummaryCtx() and
24902492
config = mid.getConfiguration() and
2491-
ap = mid.getAp()
2493+
ap = mid.getAp() and
2494+
pos = sc.getParameterPos() and
2495+
not kind.(ParamUpdateReturnKind).getPosition() = pos
24922496
)
24932497
}
24942498

cpp/ql/src/semmle/code/cpp/dataflow/internal/DataFlowImplLocal.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,8 @@ private class SummaryCtxSome extends SummaryCtx, TSummaryCtxSome {
20892089

20902090
SummaryCtxSome() { this = TSummaryCtxSome(p, ap) }
20912091

2092+
int getParameterPos() { p.isParameterOf(_, result) }
2093+
20922094
override string toString() { result = p + ": " + ap }
20932095

20942096
predicate hasLocationInfo(
@@ -2482,13 +2484,15 @@ pragma[nomagic]
24822484
private predicate paramFlowsThrough(
24832485
ReturnKindExt kind, CallContextCall cc, SummaryCtxSome sc, AccessPath ap, Configuration config
24842486
) {
2485-
exists(PathNodeMid mid, ReturnNodeExt ret |
2487+
exists(PathNodeMid mid, ReturnNodeExt ret, int pos |
24862488
mid.getNode() = ret and
24872489
kind = ret.getKind() and
24882490
cc = mid.getCallContext() and
24892491
sc = mid.getSummaryCtx() and
24902492
config = mid.getConfiguration() and
2491-
ap = mid.getAp()
2493+
ap = mid.getAp() and
2494+
pos = sc.getParameterPos() and
2495+
not kind.(ParamUpdateReturnKind).getPosition() = pos
24922496
)
24932497
}
24942498

cpp/ql/src/semmle/code/cpp/exprs/Expr.qll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import semmle.code.cpp.Element
22
private import semmle.code.cpp.Enclosing
33
private import semmle.code.cpp.internal.ResolveClass
44
private import semmle.code.cpp.internal.AddressConstantExpression
5+
private import semmle.code.cpp.models.implementations.Allocation
56

67
/**
78
* A C/C++ expression.
@@ -804,8 +805,10 @@ class NewOrNewArrayExpr extends Expr, @any_new_expr {
804805
* call the constructor of `T` but will not allocate memory.
805806
*/
806807
Expr getPlacementPointer() {
807-
isStandardPlacementNewAllocator(this.getAllocator()) and
808-
result = this.getAllocatorCall().getArgument(1)
808+
result =
809+
this
810+
.getAllocatorCall()
811+
.getArgument(this.getAllocator().(OperatorNewAllocationFunction).getPlacementArgument())
809812
}
810813
}
811814

@@ -1194,12 +1197,6 @@ private predicate convparents(Expr child, int idx, Element parent) {
11941197
)
11951198
}
11961199

1197-
private predicate isStandardPlacementNewAllocator(Function operatorNew) {
1198-
operatorNew.getName().matches("operator new%") and
1199-
operatorNew.getNumberOfParameters() = 2 and
1200-
operatorNew.getParameter(1).getType() instanceof VoidPointerType
1201-
}
1202-
12031200
// Pulled out for performance. See QL-796.
12041201
private predicate hasNoConversions(Expr e) { not e.hasConversion() }
12051202

0 commit comments

Comments
 (0)