From acdd23d529b091d81cf385d945f909741a8a7b5c Mon Sep 17 00:00:00 2001 From: arcuri82 Date: Tue, 26 Sep 2023 23:26:11 +0200 Subject: [PATCH] avoid crashing if not handled regex in SQL --- .../core/sql/SqlActionGeneBuilder.kt | 56 ++++++++++++------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/core/src/main/kotlin/org/evomaster/core/sql/SqlActionGeneBuilder.kt b/core/src/main/kotlin/org/evomaster/core/sql/SqlActionGeneBuilder.kt index b5622803b7..820e612bd2 100644 --- a/core/src/main/kotlin/org/evomaster/core/sql/SqlActionGeneBuilder.kt +++ b/core/src/main/kotlin/org/evomaster/core/sql/SqlActionGeneBuilder.kt @@ -2,6 +2,7 @@ package org.evomaster.core.sql import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType import org.evomaster.client.java.instrumentation.shared.RegexSharedUtils +import org.evomaster.core.logging.LoggingUtil import org.evomaster.core.sql.schema.Column import org.evomaster.core.sql.schema.ColumnDataType import org.evomaster.core.sql.schema.ForeignKey @@ -680,33 +681,50 @@ class SqlActionGeneBuilder { /** * TODO Handle a conjunction of Regex patterns */ - log.warn("Handling only a regex pattern for (${column.name}). Using like pattern: ${likePattern}}") + LoggingUtil.uniqueWarn(log, "Handling only a regex pattern for (${column.name}). Using like pattern: ${likePattern}}") } buildLikeRegexGene(columnName, likePattern, databaseType = column.databaseType) } else if (column.javaRegExPattern != null) { - buildJavaRegexGene(column.name, column.javaRegExPattern) - } else { - val columnMinLength = if (isFixedLength) { - column.size - } else { - if (column.minSize !=null && column.minSize>0) { - column.minSize - } else if (column.isNotBlank==true) { - 1 - } else { - 0 - } - } - val columnMaxLength = if (column.maxSize!=null) { - minOf(column.maxSize, column.size) - } else { - column.size + + try { + buildJavaRegexGene(column.name, column.javaRegExPattern) + } catch (e: Exception){ + LoggingUtil.uniqueWarn(log, "Failed to handle regex: ${column.javaRegExPattern}") + buildStringGene(isFixedLength, column) } - StringGene(name = column.name, minLength = columnMinLength, maxLength = columnMaxLength) + /* + TODO in those cases of regex, shouldn't still check for size constraints? + */ + + } else { + buildStringGene(isFixedLength, column) } } } + private fun buildStringGene( + isFixedLength: Boolean, + column: Column, + ): StringGene { + val columnMinLength = if (isFixedLength) { + column.size + } else { + if (column.minSize != null && column.minSize > 0) { + column.minSize + } else if (column.isNotBlank == true) { + 1 + } else { + 0 + } + } + val columnMaxLength = if (column.maxSize != null) { + minOf(column.maxSize, column.size) + } else { + column.size + } + return StringGene(name = column.name, minLength = columnMinLength, maxLength = columnMaxLength) + } + private fun buildJavaRegexGene(name: String, javaRegExPattern: String): RegexGene { val fullMatchRegex = RegexSharedUtils.forceFullMatch(javaRegExPattern) val disjunctionRxGenes = RegexHandler.createGeneForJVM(fullMatchRegex).disjunctions