Skip to content

Commit 6163a8f

Browse files
avpotapov00Aleksandr.Potapov
andauthored
Avoid recursive enumeration of Class and ClassLoader instances (#364)
Co-authored-by: Aleksandr.Potapov <[email protected]>
1 parent ba0b605 commit 6163a8f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/jvm/main/org/jetbrains/kotlinx/lincheck/ObjectTraverser.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ internal fun enumerateObjects(obj: Any): Map<Any, Int> {
5252
* @param objectNumberMap result enumeration map
5353
*/
5454
private fun enumerateObjects(obj: Any, processedObjects: MutableSet<Any>, objectNumberMap: MutableMap<Any, Int>) {
55+
if (obj is Class<*> || obj is ClassLoader) return
5556
if (!processedObjects.add(obj)) return
5657
val objectNumber = getObjectNumber(obj.javaClass, obj)
5758
objectNumberMap[obj] = objectNumber
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Lincheck
3+
*
4+
* Copyright (C) 2019 - 2024 JetBrains s.r.o.
5+
*
6+
* This Source Code Form is subject to the terms of the
7+
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
8+
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
package org.jetbrains.kotlinx.lincheck
12+
13+
import org.junit.Assert
14+
import org.junit.Test
15+
16+
/**
17+
* Checks invariants and restrictions on [enumerateObjects] method.
18+
*/
19+
class ObjectTraverserTest {
20+
21+
@Test
22+
fun `should not traverse class and classLoader recursively while enumerating objects`() {
23+
val myObject = @Suppress("unused") object : Any() {
24+
var clazz: Class<*>? = this::class.java
25+
var classLoader: ClassLoader? = this::class.java.classLoader
26+
var integer: Int = 10
27+
}
28+
val objectEnumeration = enumerateObjects(myObject)
29+
30+
Assert.assertTrue(objectEnumeration.keys.none { it is Class<*> || it is ClassLoader })
31+
}
32+
33+
}

0 commit comments

Comments
 (0)