diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt index e71294914d..01ed9ca331 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt @@ -203,7 +203,9 @@ data class SarifArtifact( val uriBaseId: String = "%SRCROOT%" ) -// all fields should be one-based +/** + * All fields should be one-based. + */ data class SarifRegion( val startLine: Int, val endLine: Int? = null, @@ -214,13 +216,20 @@ data class SarifRegion( /** * Makes [startColumn] the first non-whitespace character in [startLine] in the [text]. * If the [text] contains less than [startLine] lines, [startColumn] == null. + * @param startLine should be one-based */ fun withStartLine(text: String, startLine: Int): SarifRegion { val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based val startColumn = neededLine?.run { takeWhile { it.toString().isBlank() }.length + 1 // to one-based } - return SarifRegion(startLine = startLine, startColumn = startColumn) + val safeStartLine = if (startLine < 1) { + logger.warn { "For some reason startLine < 1, so now it is equal to 1" } + 1 // we don't want to fail, so just set the line number to 1 + } else { + startLine + } + return SarifRegion(startLine = safeStartLine, startColumn = startColumn) } } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt index 94de6244cc..1df35ccd1e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt @@ -1,5 +1,6 @@ package org.utbot.sarif +import mu.KotlinLogging import org.utbot.common.PathUtil.fileExtension import org.utbot.common.PathUtil.toPath import org.utbot.framework.UtSettings @@ -7,6 +8,8 @@ import org.utbot.framework.plugin.api.* import java.nio.file.Path import kotlin.io.path.nameWithoutExtension +internal val logger = KotlinLogging.logger {} + /** * Used for the SARIF report creation by given test cases and generated tests code. * SARIF is a JSON-based format for presenting static analyzer results. @@ -328,7 +331,7 @@ class SarifReport( message = Message("$classFqn.$methodName($sourceFileName:$lineNumber)"), physicalLocation = SarifPhysicalLocation( SarifArtifact(uri = sourceFilePath), - SarifRegion(startLine = lineNumber) + SarifRegion(startLine = lineNumber) // lineNumber is one-based ) )) } @@ -440,7 +443,7 @@ class SarifReport( val lastCoveredInstruction = coveredInstructions?.lastOrNull() if (lastCoveredInstruction != null) return Pair( - lastCoveredInstruction.lineNumber, + lastCoveredInstruction.lineNumber, // .lineNumber is one-based lastCoveredInstruction.className.replace('/', '.') ) @@ -448,7 +451,7 @@ class SarifReport( val lastPathElementLineNumber = try { // path/fullPath might be empty when engine executes in another process - // soot entities cannot be passed to the main process because kryo cannot deserialize them - (utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber + (utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber // one-based } catch (t: Throwable) { null }