Skip to content

Commit

Permalink
fix(amazonq): add boundary check to UserModificationTracker to preven…
Browse files Browse the repository at this point in the history
…t 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]>
  • Loading branch information
evanliu048 and rli authored Feb 3, 2025
1 parent a545699 commit 02ec702
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Amazon Q: Prevent IndexOutOfBoundsException by adding boundary checks for invalid range markers (#5187)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.assertj.core.util.VisibleForTesting
import software.amazon.awssdk.services.codewhispererruntime.model.CodeWhispererRuntimeException
import software.aws.toolkits.core.utils.debug
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnection
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
import software.aws.toolkits.jetbrains.services.codewhisperer.customization.CodeWhispererModelConfigurator
Expand Down Expand Up @@ -154,14 +155,28 @@ class CodeWhispererUserModificationTracker(private val project: Project) : Dispo
private fun emitTelemetryOnSuggestion(acceptedSuggestion: AcceptedSuggestionEntry) {
val file = acceptedSuggestion.vFile

if (file == null || (!file.isValid)) {
if (file == null || (!file.isValid) || !acceptedSuggestion.range.isValid) {
sendModificationTelemetry(acceptedSuggestion, null)
sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
} else {
// Will remove this later when we truly don't need toolkit user modification telemetry anymore
val document = runReadAction {
FileDocumentManager.getInstance().getDocument(file)
}
val start = acceptedSuggestion.range.startOffset
val end = acceptedSuggestion.range.endOffset
if (document != null) {
if (start < 0 || end < start || end > document.textLength) {
LOG.warn {
"Invalid range for suggestion ${acceptedSuggestion.requestId}: " +
"start=$start, end=$end, docLength=${document.textLength}"
}
sendModificationTelemetry(acceptedSuggestion, null)
sendUserModificationTelemetryToServiceAPI(acceptedSuggestion)
return
}
}

val currentString = document?.getText(
TextRange(acceptedSuggestion.range.startOffset, acceptedSuggestion.range.endOffset)
)
Expand Down

0 comments on commit 02ec702

Please sign in to comment.