Skip to content

Commit 1cf2db1

Browse files
authored
Merge pull request github#10718 from tamasvajk/kotlin-internal-repr
Kotlin: ignore properties in `java/internal-representation-exposure` check
2 parents 87b971c + cd64faf commit 1cf2db1

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

java/ql/src/Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ predicate exposesByStore(Callable c, Field f, Expr why, string whyText) {
120120

121121
from Callable c, Field f, Expr why, string whyText
122122
where
123-
exposesByReturn(c, f, why, whyText) or
124-
exposesByStore(c, f, why, whyText)
123+
(
124+
exposesByReturn(c, f, why, whyText) or
125+
exposesByStore(c, f, why, whyText)
126+
) and
127+
// Kotlin properties expose internal representation, but it's not accidental, so ignore them
128+
not exists(Property p | p.getBackingField() = f)
125129
select c,
126130
c.getName() + " exposes the internal representation stored in field " + f.getName() +
127131
". The value may be modified $@.", why.getLocation(), whyText

java/ql/test/kotlin/query-tests/ExposeRepresentation/ExposeRepresentation.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ExposesRep {
2+
val strings: Array<String?> = arrayOfNulls(1)
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class User {
2+
fun test1(er: ExposesRep) {
3+
er.strings[0] = "Hello world"
4+
}
5+
}

0 commit comments

Comments
 (0)