Skip to content

Commit 5541d7b

Browse files
committed
Limit the maximum length of an array initializer + use guards for both array allocation and initialization
1 parent 449400b commit 5541d7b

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgVariableConstructor.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.utbot.framework.codegen.model.constructor.context.CgContext
66
import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
77
import org.utbot.framework.codegen.model.constructor.util.CgComponents
88
import org.utbot.framework.codegen.model.constructor.util.CgStatementConstructor
9+
import org.utbot.framework.codegen.model.constructor.util.MAX_ARRAY_INITIALIZER_SIZE
910
import org.utbot.framework.codegen.model.constructor.util.arrayInitializer
1011
import org.utbot.framework.codegen.model.constructor.util.get
1112
import org.utbot.framework.codegen.model.constructor.util.isDefaultValueOf
@@ -221,7 +222,11 @@ internal class CgVariableConstructor(val context: CgContext) :
221222
arrayModel.stores.getOrDefault(it, arrayModel.constModel)
222223
}
223224

224-
val canInitWithValues = elementModels.all { it is UtPrimitiveModel } || elementModels.all { it is UtNullModel }
225+
val allPrimitives = elementModels.all { it is UtPrimitiveModel }
226+
val allNulls = elementModels.all { it is UtNullModel }
227+
// we can use array initializer if all elements are primitives or all of them are null,
228+
// and the size of an array is not greater than the fixed maximum size
229+
val canInitWithValues = (allPrimitives || allNulls) && elementModels.size <= MAX_ARRAY_INITIALIZER_SIZE
225230

226231
val initializer = if (canInitWithValues) {
227232
val elements = elementModels.map { model ->

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/util/CgStatementConstructor.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.utbot.framework.plugin.api.util.isSubtypeOf
5757
import org.utbot.framework.plugin.api.util.objectArrayClassId
5858
import org.utbot.framework.plugin.api.util.objectClassId
5959
import fj.data.Either
60+
import org.utbot.framework.codegen.model.tree.CgArrayInitializer
6061
import java.lang.reflect.Constructor
6162
import java.lang.reflect.Method
6263
import kotlin.reflect.KFunction
@@ -174,6 +175,7 @@ internal class CgStatementConstructorImpl(context: CgContext) :
174175
val (type, expr) = when (baseExpr) {
175176
is CgEnumConstantAccess -> guardEnumConstantAccess(baseExpr)
176177
is CgAllocateArray -> guardArrayAllocation(baseExpr)
178+
is CgArrayInitializer -> guardArrayInitializer(baseExpr)
177179
is CgExecutableCall -> guardExecutableCall(baseType, baseExpr)
178180
else -> guardExpression(baseType, baseExpr)
179181
}
@@ -419,13 +421,21 @@ internal class CgStatementConstructorImpl(context: CgContext) :
419421
}
420422

421423
private fun guardArrayAllocation(allocation: CgAllocateArray): ExpressionWithType {
424+
return guardArrayCreation(allocation.type, allocation.size, allocation)
425+
}
426+
427+
private fun guardArrayInitializer(initializer: CgArrayInitializer): ExpressionWithType {
428+
return guardArrayCreation(initializer.type, initializer.size, initializer)
429+
}
430+
431+
private fun guardArrayCreation(arrayType: ClassId, arraySize: Int, initialization: CgExpression): ExpressionWithType {
422432
// TODO: check if this is the right way to check array type accessibility
423-
return if (allocation.type.isAccessibleFrom(testClassPackageName)) {
424-
ExpressionWithType(allocation.type, allocation)
433+
return if (arrayType.isAccessibleFrom(testClassPackageName)) {
434+
ExpressionWithType(arrayType, initialization)
425435
} else {
426436
ExpressionWithType(
427437
objectArrayClassId,
428-
testClassThisInstance[createArray](allocation.elementType.name, allocation.size)
438+
testClassThisInstance[createArray](arrayType.elementClassId!!.name, arraySize)
429439
)
430440
}
431441
}

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/util/ConstructorUtils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ private fun FieldPath.toStringList(): List<String> =
156156
internal fun infiniteInts(): Sequence<Int> =
157157
generateSequence(1) { it + 1 }
158158

159+
internal const val MAX_ARRAY_INITIALIZER_SIZE = 10
160+
159161
/**
160162
* Checks if we have already imported a class with such simple name.
161163
* If so, we cannot import [type] (because it will be used with simple name and will be clashed with already imported)

0 commit comments

Comments
 (0)