Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 2e60d40

Browse files
committed
Address review comments 6
1 parent 69212b9 commit 2e60d40

File tree

5 files changed

+53
-36
lines changed

5 files changed

+53
-36
lines changed

ql/src/Security/CWE-681/IncorrectIntegerConversion.ql

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ float getMaxIntValue(int bitSize, boolean isSigned) {
3333
* architecture-specific.
3434
*/
3535
int getIntTypeBitSize(File file) {
36-
if file.hasConstrainedIntBitSize(32)
37-
then result = 32
38-
else
39-
if file.hasConstrainedIntBitSize(64)
40-
then result = 64
41-
else result = 0
36+
file.constrainsIntBitSize(result)
37+
or
38+
not file.constrainsIntBitSize(_) and
39+
result = 0
4240
}
4341

4442
/**

ql/src/go.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import Customizations
6+
import semmle.go.Architectures
67
import semmle.go.AST
78
import semmle.go.Comments
89
import semmle.go.Concepts

ql/src/semmle/go/Architectures.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** Provides classes for working with architectures. */
2+
3+
import go
4+
5+
/**
6+
* An architecture that is valid in a build constraint.
7+
*
8+
* Information obtained from
9+
* https://github.com/golang/go/blob/98cbf45cfc6a5a50cc6ac2367f9572cb198b57c7/src/go/types/gccgosizes.go
10+
* where the first field of the struct is 4 for 32-bit architectures
11+
* and 8 for 64-bit architectures.
12+
*/
13+
class Architecture extends string {
14+
int bitSize;
15+
16+
Architecture() {
17+
this in ["386", "amd64p32", "arm", "armbe", "mips", "mipsle", "mips64p32", "mips64p32le", "ppc",
18+
"s390", "sparc"] and
19+
bitSize = 32
20+
or
21+
this in ["amd64", "arm64", "arm64be", "ppc64", "ppc64le", "mips64", "mips64le", "s390x",
22+
"sparc64"] and
23+
bitSize = 64
24+
}
25+
26+
int getBitSize() { result = bitSize }
27+
}

ql/src/semmle/go/Comments.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,10 @@ class BuildConstraintComment extends LineComment {
211211
}
212212

213213
override string getAPrimaryQlClass() { result = "BuildConstraintComment" }
214+
215+
/** Gets the body of this build constraint. */
216+
string getConstraintBody() { result = getText().splitAt("+build ", 1) }
217+
218+
/** Gets a disjunct of this build constraint. */
219+
string getADisjunct() { result = getConstraintBody().splitAt(" ") }
214220
}

ql/src/semmle/go/Files.qll

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -203,55 +203,40 @@ class File extends Container, @file, Documentable, ExprParent, GoModExprParent,
203203
pragma[noinline]
204204
predicate hasBuildConstraints() { exists(BuildConstraintComment bc | this = bc.getFile()) }
205205

206-
/**
207-
* Gets an architecture that is valid in a build constraint with bit
208-
* size `bitSize`.
209-
*
210-
* Information obtained from
211-
* https://github.com/golang/go/blob/98cbf45cfc6a5a50cc6ac2367f9572cb198b57c7/src/go/types/gccgosizes.go
212-
* where the first field of the struct is 4 for 32-bit architectures
213-
* and 8 for 64-bit architectures.
214-
*/
215-
private string getAnArchitecture(int bitSize) {
216-
bitSize = 32 and
217-
result in ["386", "amd64p32", "arm", "armbe", "mips", "mipsle", "mips64p32", "mips64p32le",
218-
"ppc", "s390", "sparc"]
219-
or
220-
bitSize = 64 and
221-
result in ["amd64", "arm64", "arm64be", "ppc64", "ppc64le", "mips64", "mips64le", "s390x",
222-
"sparc64"]
223-
}
224-
225206
/**
226207
* Holds if this file contains build constraints that ensure that it
227-
* is only built on architectures of bit size `bitSize`.
208+
* is only built on architectures of bit size `bitSize`, which can be
209+
* 32 or 64.
228210
*/
229-
predicate hasConstrainedIntBitSize(int bitSize) {
230-
hasExplicitBuildConstraintsForArchitectures(bitSize) or
231-
hasImplicitBuildConstraintForAnArchitecture(bitSize)
211+
predicate constrainsIntBitSize(int bitSize) {
212+
explicitlyConstrainsIntBitSize(bitSize) or
213+
implicitlyConstrainsIntBitSize(bitSize)
232214
}
233215

234216
/**
235217
* Holds if this file contains explicit build constraints that ensure
236-
* that it is only built on an architecture of bit size `bitSize`.
218+
* that it is only built on an architecture of bit size `bitSize`,
219+
* which can be 32 or 64.
237220
*/
238-
predicate hasExplicitBuildConstraintsForArchitectures(int bitSize) {
221+
predicate explicitlyConstrainsIntBitSize(int bitSize) {
239222
exists(BuildConstraintComment bcc, string bc |
240223
this = bcc.getFile() and bc = bcc.getText().splitAt("+build ", 1)
241224
|
242-
forex(string disjunct | disjunct = bc.splitAt(" ") |
243-
disjunct.splitAt(",").matches(getAnArchitecture(bitSize))
225+
forex(string disjunct | disjunct = bcc.getADisjunct() |
226+
disjunct.splitAt(",").(Architecture).getBitSize() = bitSize
244227
)
245228
)
246229
}
247230

248231
/**
249232
* Holds if this file has a name which acts as an implicit build
250233
* constraint that ensures that it is only built on an
251-
* architecture of bit size `bitSize`.
234+
* architecture of bit size `bitSize`, which can be 32 or 64.
252235
*/
253-
predicate hasImplicitBuildConstraintForAnArchitecture(int bitSize) {
254-
this.getStem().regexpMatch(".*_" + getAnArchitecture(bitSize) + "(_test)?")
236+
predicate implicitlyConstrainsIntBitSize(int bitSize) {
237+
this
238+
.getStem()
239+
.regexpMatch(".*_" + any(Architecture arch | arch.getBitSize() = bitSize) + "(_test)?")
255240
}
256241

257242
override string toString() { result = Container.super.toString() }

0 commit comments

Comments
 (0)