Skip to content

Commit

Permalink
Avoid recursive enumeration of Class and ClassLoader instances (#364)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr.Potapov <[email protected]>
  • Loading branch information
avpotapov00 and Aleksandr.Potapov authored Aug 20, 2024
1 parent ba0b605 commit 6163a8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal fun enumerateObjects(obj: Any): Map<Any, Int> {
* @param objectNumberMap result enumeration map
*/
private fun enumerateObjects(obj: Any, processedObjects: MutableSet<Any>, objectNumberMap: MutableMap<Any, Int>) {
if (obj is Class<*> || obj is ClassLoader) return
if (!processedObjects.add(obj)) return
val objectNumber = getObjectNumber(obj.javaClass, obj)
objectNumberMap[obj] = objectNumber
Expand Down
33 changes: 33 additions & 0 deletions src/jvm/test/org/jetbrains/kotlinx/lincheck/ObjectTraverserTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Lincheck
*
* Copyright (C) 2019 - 2024 JetBrains s.r.o.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.jetbrains.kotlinx.lincheck

import org.junit.Assert
import org.junit.Test

/**
* Checks invariants and restrictions on [enumerateObjects] method.
*/
class ObjectTraverserTest {

@Test
fun `should not traverse class and classLoader recursively while enumerating objects`() {
val myObject = @Suppress("unused") object : Any() {
var clazz: Class<*>? = this::class.java
var classLoader: ClassLoader? = this::class.java.classLoader
var integer: Int = 10
}
val objectEnumeration = enumerateObjects(myObject)

Assert.assertTrue(objectEnumeration.keys.none { it is Class<*> || it is ClassLoader })
}

}

0 comments on commit 6163a8f

Please sign in to comment.