Skip to content

Commit 5052979

Browse files
authored
Add constraints to the start line field in SARIF (#1762)
1 parent d2edb45 commit 5052979

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ data class SarifArtifact(
203203
val uriBaseId: String = "%SRCROOT%"
204204
)
205205

206-
// all fields should be one-based
206+
/**
207+
* All fields should be one-based.
208+
*/
207209
data class SarifRegion(
208210
val startLine: Int,
209211
val endLine: Int? = null,
@@ -214,13 +216,20 @@ data class SarifRegion(
214216
/**
215217
* Makes [startColumn] the first non-whitespace character in [startLine] in the [text].
216218
* If the [text] contains less than [startLine] lines, [startColumn] == null.
219+
* @param startLine should be one-based
217220
*/
218221
fun withStartLine(text: String, startLine: Int): SarifRegion {
219222
val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based
220223
val startColumn = neededLine?.run {
221224
takeWhile { it.toString().isBlank() }.length + 1 // to one-based
222225
}
223-
return SarifRegion(startLine = startLine, startColumn = startColumn)
226+
val safeStartLine = if (startLine < 1) {
227+
logger.warn { "For some reason startLine < 1, so now it is equal to 1" }
228+
1 // we don't want to fail, so just set the line number to 1
229+
} else {
230+
startLine
231+
}
232+
return SarifRegion(startLine = safeStartLine, startColumn = startColumn)
224233
}
225234
}
226235
}

utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.utbot.sarif
22

3+
import mu.KotlinLogging
34
import org.utbot.common.PathUtil.fileExtension
45
import org.utbot.common.PathUtil.toPath
56
import org.utbot.framework.UtSettings
67
import org.utbot.framework.plugin.api.*
78
import java.nio.file.Path
89
import kotlin.io.path.nameWithoutExtension
910

11+
internal val logger = KotlinLogging.logger {}
12+
1013
/**
1114
* Used for the SARIF report creation by given test cases and generated tests code.
1215
* SARIF is a JSON-based format for presenting static analyzer results.
@@ -328,7 +331,7 @@ class SarifReport(
328331
message = Message("$classFqn.$methodName($sourceFileName:$lineNumber)"),
329332
physicalLocation = SarifPhysicalLocation(
330333
SarifArtifact(uri = sourceFilePath),
331-
SarifRegion(startLine = lineNumber)
334+
SarifRegion(startLine = lineNumber) // lineNumber is one-based
332335
)
333336
))
334337
}
@@ -440,15 +443,15 @@ class SarifReport(
440443
val lastCoveredInstruction = coveredInstructions?.lastOrNull()
441444
if (lastCoveredInstruction != null)
442445
return Pair(
443-
lastCoveredInstruction.lineNumber,
446+
lastCoveredInstruction.lineNumber, // .lineNumber is one-based
444447
lastCoveredInstruction.className.replace('/', '.')
445448
)
446449

447450
// if for some reason we can't extract the last line from the coverage
448451
val lastPathElementLineNumber = try {
449452
// path/fullPath might be empty when engine executes in another process -
450453
// soot entities cannot be passed to the main process because kryo cannot deserialize them
451-
(utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber
454+
(utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber // one-based
452455
} catch (t: Throwable) {
453456
null
454457
}

0 commit comments

Comments
 (0)