Skip to content

Commit 778d796

Browse files
committed
Fixed crash in getCodeOfSubregion
Fixes #1775
1 parent 5887086 commit 778d796

File tree

1 file changed

+7
-1
lines changed
  • cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers

1 file changed

+7
-1
lines changed

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/helpers/RegionUtils.kt

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package de.fraunhofer.aisec.cpg.helpers
2727

2828
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
2929
import de.fraunhofer.aisec.cpg.sarif.Region
30+
import kotlin.math.min
3031
import org.apache.commons.lang3.StringUtils
3132

3233
/**
@@ -62,12 +63,17 @@ fun getCodeOfSubregion(code: String, nodeRegion: Region, subRegion: Region): Str
6263
(StringUtils.ordinalIndexOf(code, nlType, subRegion.startLine - nodeRegion.startLine) +
6364
subRegion.startColumn)
6465
}
65-
val end =
66+
var end =
6667
if (subRegion.endLine == nodeRegion.startLine) {
6768
subRegion.endColumn - nodeRegion.startColumn
6869
} else {
6970
(StringUtils.ordinalIndexOf(code, nlType, subRegion.endLine - nodeRegion.startLine) +
7071
subRegion.endColumn)
7172
}
73+
74+
// Unfortunately, we sometimes have issues with (non)-Unicode characters in code, where the
75+
// python AST thinks that multiple characters are needed and reports a position that is actually
76+
// beyond our "end"
77+
end = min(end, code.length)
7278
return code.substring(start, end)
7379
}

0 commit comments

Comments
 (0)