-
Notifications
You must be signed in to change notification settings - Fork 251
fix(amazonq): switch to ulong to avoid overflow when input is larger than 2gb #5558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c034541
1c45ec8
93ca253
b819034
4d0eea3
8c5813c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type" : "bugfix", | ||
"description" : "Fix integer overflow when local context index input is larger than 2GB" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ | |
|
||
data class FileCollectionResult( | ||
val files: List<String>, | ||
val fileSize: Int, | ||
val fileSize: Int, // in MB | ||
Check warning on line 73 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
) | ||
|
||
// TODO: move to LspMessage.kt | ||
|
@@ -241,59 +241,7 @@ | |
} | ||
} | ||
|
||
private fun willExceedPayloadLimit(currentTotalFileSize: Long, currentFileSize: Long): Boolean { | ||
val maxSize = CodeWhispererSettings.getInstance().getProjectContextIndexMaxSize() | ||
return currentTotalFileSize.let { totalSize -> totalSize > (maxSize * 1024 * 1024 - currentFileSize) } | ||
} | ||
|
||
private fun isBuildOrBin(fileName: String): Boolean { | ||
val regex = Regex("""bin|build|node_modules|venv|\.venv|env|\.idea|\.conda""", RegexOption.IGNORE_CASE) | ||
return regex.find(fileName) != null | ||
} | ||
|
||
fun collectFiles(): FileCollectionResult { | ||
val collectedFiles = mutableListOf<String>() | ||
var currentTotalFileSize = 0L | ||
val allFiles = mutableListOf<VirtualFile>() | ||
|
||
val projectBaseDirectories = project.getBaseDirectories() | ||
val changeListManager = ChangeListManager.getInstance(project) | ||
|
||
projectBaseDirectories.forEach { | ||
VfsUtilCore.visitChildrenRecursively( | ||
it, | ||
object : VirtualFileVisitor<Unit>(NO_FOLLOW_SYMLINKS) { | ||
// TODO: refactor this along with /dev & codescan file traversing logic | ||
override fun visitFile(file: VirtualFile): Boolean { | ||
if ((file.isDirectory && isBuildOrBin(file.name)) || | ||
!isWorkspaceSourceContent(file, projectBaseDirectories, changeListManager, additionalGlobalIgnoreRulesForStrictSources) || | ||
(file.isFile && file.length > 10 * 1024 * 1024) | ||
) { | ||
return false | ||
} | ||
if (file.isFile) { | ||
allFiles.add(file) | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
) | ||
} | ||
|
||
for (file in allFiles) { | ||
if (willExceedPayloadLimit(currentTotalFileSize, file.length)) { | ||
break | ||
} | ||
collectedFiles.add(file.path) | ||
currentTotalFileSize += file.length | ||
} | ||
|
||
return FileCollectionResult( | ||
files = collectedFiles.toList(), | ||
fileSize = (currentTotalFileSize / 1024 / 1024).toInt() | ||
) | ||
} | ||
fun collectFiles(): FileCollectionResult = collectFiles(project.getBaseDirectories(), ChangeListManager.getInstance(project)) | ||
Check warning on line 244 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
|
||
private fun queryResultToRelevantDocuments(queryResult: List<Chunk>): List<RelevantDocument> { | ||
val documents: MutableList<RelevantDocument> = mutableListOf() | ||
|
@@ -353,5 +301,57 @@ | |
|
||
companion object { | ||
private val logger = getLogger<ProjectContextProvider>() | ||
private val regex = Regex("""bin|build|node_modules|venv|\.venv|env|\.idea|\.conda""", RegexOption.IGNORE_CASE) | ||
private val mega = (1024 * 1024).toULong() | ||
private val tenMb = 10 * mega.toInt() | ||
Check warning on line 306 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
|
||
private fun willExceedPayloadLimit(maxSize: ULong, currentTotalFileSize: ULong, currentFileSize: Long) = | ||
currentTotalFileSize.let { totalSize -> totalSize > (maxSize - currentFileSize.toUInt()) } | ||
|
||
private fun isBuildOrBin(fileName: String): Boolean = | ||
regex.find(fileName) != null | ||
|
||
fun collectFiles(projectBaseDirectories: Set<VirtualFile>, changeListManager: ChangeListManager): FileCollectionResult { | ||
val maxSize = CodeWhispererSettings.getInstance() | ||
.getProjectContextIndexMaxSize().toULong() * mega | ||
val collectedFiles = mutableListOf<String>() | ||
var currentTotalFileSize = 0UL | ||
val allFiles = mutableListOf<VirtualFile>() | ||
Check warning on line 319 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
|
||
projectBaseDirectories.forEach { | ||
VfsUtilCore.visitChildrenRecursively( | ||
it, | ||
object : VirtualFileVisitor<Unit>(NO_FOLLOW_SYMLINKS) { | ||
Check warning on line 324 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
// TODO: refactor this along with /dev & codescan file traversing logic | ||
override fun visitFile(file: VirtualFile): Boolean { | ||
if ((file.isDirectory && isBuildOrBin(file.name)) || | ||
!isWorkspaceSourceContent(file, projectBaseDirectories, changeListManager, additionalGlobalIgnoreRulesForStrictSources) || | ||
(file.isFile && file.length > tenMb) | ||
) { | ||
return false | ||
Check warning on line 331 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
} | ||
if (file.isFile) { | ||
allFiles.add(file) | ||
return false | ||
Check warning on line 335 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. files have no children to visit so it could go either way |
||
} | ||
return true | ||
Check warning on line 337 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
} | ||
} | ||
) | ||
} | ||
Check warning on line 341 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
|
||
for (file in allFiles) { | ||
if (willExceedPayloadLimit(maxSize, currentTotalFileSize, file.length)) { | ||
break | ||
Check warning on line 345 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
} | ||
collectedFiles.add(file.path) | ||
currentTotalFileSize += file.length.toUInt() | ||
Check warning on line 348 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
} | ||
|
||
return FileCollectionResult( | ||
files = collectedFiles.toList(), | ||
fileSize = (currentTotalFileSize / 1024u / 1024u).toInt() | ||
Check warning on line 353 in plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/project/ProjectContextProvider.kt
|
||
) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker, but do we want to move this list to a common utils file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
everyone seems to have a different concept of what to ignore at the moment