Skip to content

Commit 02ec702

Browse files
evanliu048rli
andauthored
fix(amazonq): add boundary check to UserModificationTracker to prevent IndexOutOfBoundsException (#5312)
* add boundary check for userModificationTracker * add chaneglog * Update bugfix-24d6621e-32d3-45c5-a996-66232724e425.json This reverts commit d9453ea. --------- Co-authored-by: Richard Li <[email protected]>
1 parent a545699 commit 02ec702

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Amazon Q: Prevent IndexOutOfBoundsException by adding boundary checks for invalid range markers (#5187)"
4+
}

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/telemetry/CodeWhispererUserModificationTracker.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.assertj.core.util.VisibleForTesting
1919
import software.amazon.awssdk.services.codewhispererruntime.model.CodeWhispererRuntimeException
2020
import software.aws.toolkits.core.utils.debug
2121
import software.aws.toolkits.core.utils.getLogger
22+
import software.aws.toolkits.core.utils.warn
2223
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnection
2324
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
2425
import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
@@ -154,14 +155,28 @@ class CodeWhispererUserModificationTracker(private val project: Project) : Dispo
154155
private fun emitTelemetryOnSuggestion(acceptedSuggestion: AcceptedSuggestionEntry) {
155156
val file = acceptedSuggestion.vFile
156157

157-
if (file == null || (!file.isValid)) {
158+
if (file == null || (!file.isValid) || !acceptedSuggestion.range.isValid) {
158159
sendModificationTelemetry(acceptedSuggestion, null)
159160
sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
160161
} else {
161162
// Will remove this later when we truly don't need toolkit user modification telemetry anymore
162163
val document = runReadAction {
163164
FileDocumentManager.getInstance().getDocument(file)
164165
}
166+
val start = acceptedSuggestion.range.startOffset
167+
val end = acceptedSuggestion.range.endOffset
168+
if (document != null) {
169+
if (start < 0 || end < start || end > document.textLength) {
170+
LOG.warn {
171+
"Invalid range for suggestion ${acceptedSuggestion.requestId}: " +
172+
"start=$start, end=$end, docLength=${document.textLength}"
173+
}
174+
sendModificationTelemetry(acceptedSuggestion, null)
175+
sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
176+
return
177+
}
178+
}
179+
165180
val currentString = document?.getText(
166181
TextRange(acceptedSuggestion.range.startOffset, acceptedSuggestion.range.endOffset)
167182
)

0 commit comments

Comments
 (0)