Skip to content

Commit f837504

Browse files
smowtonowen-mc
authored andcommitted
Interface unaliasing: don't confuse the empty interface with the type 'comparable'
1 parent 94a6373 commit f837504

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,16 @@ class TypeSetLiteralType extends @typesetliteraltype, CompositeType {
915915
override string toString() { result = "type set literal type" }
916916
}
917917

918+
private predicate isInterfaceComponentWithQualifiedName(
919+
InterfaceType intf, int idx, string qualifiedName, Type tp
920+
) {
921+
exists(string name | component_types(intf, idx, name, tp) |
922+
interface_private_method_ids(intf, idx, qualifiedName)
923+
or
924+
not interface_private_method_ids(intf, idx, _) and qualifiedName = name
925+
)
926+
}
927+
918928
private newtype TOptInterfaceComponent =
919929
MkNoIComponent() or
920930
MkSomeIComponent(string name, Type tp) { component_types(any(InterfaceType i), _, name, tp) }
@@ -947,10 +957,12 @@ pragma[nomagic]
947957
predicate unpackInterfaceType(
948958
InterfaceType intf, TOptInterfaceComponent c0, TOptInterfaceComponent c1,
949959
TOptInterfaceComponent c2, TOptInterfaceComponent c3, TOptInterfaceComponent c4,
950-
TOptInterfaceComponent e1, TOptInterfaceComponent e2, int nComponents, int nEmbeds
960+
TOptInterfaceComponent e1, TOptInterfaceComponent e2, int nComponents, int nEmbeds,
961+
boolean isComparable
951962
) {
952-
nComponents = count(int i | component_types(intf, i, _, _) and i >= 0) and
953-
nEmbeds = count(int i | component_types(intf, i, _, _) and i < 0) and
963+
nComponents = count(int i | isInterfaceComponentWithQualifiedName(intf, i, _, _) and i >= 0) and
964+
nEmbeds = count(int i | isInterfaceComponentWithQualifiedName(intf, i, _, _) and i < 0) and
965+
(if intf.isOrEmbedsComparable() then isComparable = true else isComparable = false) and
954966
(
955967
if nComponents >= 1
956968
then c0 = any(InterfaceComponent ic | ic.isComponentOf(intf, 0))
@@ -992,14 +1004,15 @@ pragma[nomagic]
9921004
predicate unpackAndUnaliasInterfaceType(
9931005
InterfaceType intf, TOptInterfaceComponent c0, TOptInterfaceComponent c1,
9941006
TOptInterfaceComponent c2, TOptInterfaceComponent c3, TOptInterfaceComponent c4,
995-
TOptInterfaceComponent e1, TOptInterfaceComponent e2, int nComponents, int nEmbeds
1007+
TOptInterfaceComponent e1, TOptInterfaceComponent e2, int nComponents, int nEmbeds,
1008+
boolean isComparable
9961009
) {
9971010
exists(
9981011
OptInterfaceComponent c0a, OptInterfaceComponent c1a, OptInterfaceComponent c2a,
9991012
OptInterfaceComponent c3a, OptInterfaceComponent c4a, OptInterfaceComponent e1a,
10001013
OptInterfaceComponent e2a
10011014
|
1002-
unpackInterfaceType(intf, c0a, c1a, c2a, c3a, c4a, e1a, e2a, nComponents, nEmbeds) and
1015+
unpackInterfaceType(intf, c0a, c1a, c2a, c3a, c4a, e1a, e2a, nComponents, nEmbeds, isComparable) and
10031016
c0 = c0a.getWithDeepUnaliasedType() and
10041017
c1 = c1a.getWithDeepUnaliasedType() and
10051018
c2 = c2a.getWithDeepUnaliasedType() and
@@ -1107,10 +1120,11 @@ class InterfaceType extends @interfacetype, CompositeType {
11071120
exists(
11081121
OptInterfaceComponent c0, OptInterfaceComponent c1, OptInterfaceComponent c2,
11091122
OptInterfaceComponent c3, OptInterfaceComponent c4, OptInterfaceComponent e1,
1110-
OptInterfaceComponent e2, int nComponents, int nEmbeds
1123+
OptInterfaceComponent e2, int nComponents, int nEmbeds, boolean isComparable
11111124
|
1112-
unpackAndUnaliasInterfaceType(this, c0, c1, c2, c3, c4, e1, e2, nComponents, nEmbeds) and
1113-
unpackInterfaceType(result, c0, c1, c2, c3, c4, e1, e2, nComponents, nEmbeds)
1125+
unpackAndUnaliasInterfaceType(this, c0, c1, c2, c3, c4, e1, e2, nComponents, nEmbeds,
1126+
isComparable) and
1127+
unpackInterfaceType(result, c0, c1, c2, c3, c4, e1, e2, nComponents, nEmbeds, isComparable)
11141128
)
11151129
}
11161130

0 commit comments

Comments
 (0)