Skip to content

Commit b0376d5

Browse files
authored
Merge pull request github#17792 from owen-mc/go/lookthrough-pointer-type
Go: Add helper predicate `lookThroughPointerType`
2 parents 6e197b5 + 1318504 commit b0376d5

File tree

5 files changed

+16
-25
lines changed

5 files changed

+16
-25
lines changed

go/ql/lib/semmle/go/Decls.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,7 @@ class MethodDecl extends FuncDecl {
212212
*
213213
* is `Rectangle`.
214214
*/
215-
NamedType getReceiverBaseType() {
216-
result = this.getReceiverType() or
217-
result = this.getReceiverType().(PointerType).getBaseType()
218-
}
215+
NamedType getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
219216

220217
/**
221218
* Gets the receiver variable of this method.

go/ql/lib/semmle/go/Scopes.qll

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,7 @@ class Method extends Function {
519519
* Gets the receiver base type of this method, that is, either the base type of the receiver type
520520
* if it is a pointer type, or the receiver type itself if it is not a pointer type.
521521
*/
522-
Type getReceiverBaseType() {
523-
exists(Type recv | recv = this.getReceiverType() |
524-
if recv instanceof PointerType
525-
then result = recv.(PointerType).getBaseType()
526-
else result = recv
527-
)
528-
}
522+
Type getReceiverBaseType() { result = lookThroughPointerType(this.getReceiverType()) }
529523

530524
/** Holds if this method has name `m` and belongs to the method set of type `tp` or `*tp`. */
531525
private predicate isIn(NamedType tp, string m) {

go/ql/lib/semmle/go/Types.qll

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,7 @@ class StructType extends @structtype, CompositeType {
446446
if n = ""
447447
then (
448448
isEmbedded = true and
449-
(
450-
name = tp.(NamedType).getName()
451-
or
452-
name = tp.(PointerType).getBaseType().(NamedType).getName()
453-
)
449+
name = lookThroughPointerType(tp).(NamedType).getName()
454450
) else (
455451
isEmbedded = false and
456452
name = n
@@ -518,9 +514,7 @@ class StructType extends @structtype, CompositeType {
518514
this.hasFieldCand(_, embeddedParent, depth - 1, true) and
519515
result.getName() = name and
520516
(
521-
result.getReceiverBaseType() = embeddedParent.getType()
522-
or
523-
result.getReceiverBaseType() = embeddedParent.getType().(PointerType).getBaseType()
517+
result.getReceiverBaseType() = lookThroughPointerType(embeddedParent.getType())
524518
or
525519
methodhosts(result, embeddedParent.getType())
526520
)
@@ -644,6 +638,16 @@ class PointerType extends @pointertype, CompositeType {
644638
override string toString() { result = "pointer type" }
645639
}
646640

641+
/**
642+
* Gets the base type if `t` is a pointer type, otherwise `t` itself.
643+
*/
644+
Type lookThroughPointerType(Type t) {
645+
not t instanceof PointerType and
646+
result = t
647+
or
648+
result = t.(PointerType).getBaseType()
649+
}
650+
647651
private newtype TTypeSetTerm =
648652
MkTypeSetTerm(TypeSetLiteralType tslit, int index) { component_types(tslit, index, _, _) }
649653

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,7 @@ module IR {
358358

359359
override predicate reads(ValueEntity v) { v = field }
360360

361-
override Type getResultType() {
362-
if field.getType() instanceof PointerType
363-
then result = field.getType().(PointerType).getBaseType()
364-
else result = field.getType()
365-
}
361+
override Type getResultType() { result = lookThroughPointerType(field.getType()) }
366362

367363
override ControlFlow::Root getRoot() { result.isRootOf(e) }
368364

go/ql/src/InconsistentCode/LengthComparisonOffByOne.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ predicate isRegexpMethodCall(DataFlow::MethodCallNode c) {
7373
exists(NamedType regexp, Type recvtp |
7474
regexp.getName() = "Regexp" and recvtp = c.getReceiver().getType()
7575
|
76-
recvtp = regexp or recvtp.(PointerType).getBaseType() = regexp
76+
lookThroughPointerType(recvtp) = regexp
7777
)
7878
}
7979

0 commit comments

Comments
 (0)