Skip to content

Commit

Permalink
Merge pull request #175 from dmeybohm/fix-substring-issue
Browse files Browse the repository at this point in the history
Handle some of bounds string access that was reported in #174
  • Loading branch information
dmeybohm authored Nov 8, 2024
2 parents 4fe43a0 + 2ee4513 commit 7f3b26f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

## [Unreleased]

## [0.9.2] - 2024-11-07

### Fixed
- Fix exception thrown during indexing

## [0.9.1] - 2024-06-19

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pluginGroup = com.daveme.chocolateCakePHP
pluginName = chocolate-cakephp
pluginRepositoryUrl = https://github.com/dmeybohm/chocolate-cakephp
# SemVer format -> https://semver.org
pluginVersion = 0.9.1
pluginVersion = 0.9.2

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/daveme/chocolateCakePHP/Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import java.util.*
fun String.startsWithUppercaseCharacter(): Boolean =
this.isNotEmpty() && Character.isUpperCase(this[0])

fun String.substringOrNull(startIndex: Int, endIndex: Int = length): String? {
return if (startIndex in 0..length && endIndex in startIndex..length) {
substring(startIndex, endIndex)
} else {
null
}
}

fun String.removeFromEnd(end: String, ignoreCase: Boolean = false): String =
if (end == "" || !this.endsWith(end, ignoreCase))
this
Expand Down Expand Up @@ -84,3 +92,4 @@ fun String.underscoreToCamelCase(): String {

fun String.mneumonicEscape(): String =
this.replace("_", "__")

Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class ControllerFieldTypeProvider : PhpTypeProvider4 {
override fun complete(expression: String, project: Project): PhpType? {
val indexOfSign = expression.indexOf(getKey())
val indexOfDelimiter = expression.indexOf(getKey(), indexOfSign + 1)
val firstFieldName = expression.substring(indexOfSign + 1, indexOfDelimiter)
val targetFieldName = expression.substring(indexOfDelimiter + 1)
val firstFieldName = expression.substringOrNull(indexOfSign + 1, indexOfDelimiter)
?: return null
val targetFieldName = expression.substringOrNull(indexOfDelimiter + 1)
?: return null

val index = PhpIndex.getInstance(project)
val settings = Settings.getInstance(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.daveme.chocolateCakePHP.model
import com.daveme.chocolateCakePHP.Settings
import com.daveme.chocolateCakePHP.cake.getPossibleTableClasses
import com.daveme.chocolateCakePHP.startsWithUppercaseCharacter
import com.daveme.chocolateCakePHP.substringOrNull
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.jetbrains.php.PhpIndex
Expand All @@ -24,6 +25,7 @@ class AssociatedTableTypeProvider : PhpTypeProvider4 {
override fun getType(psiElement: PsiElement): PhpType? {
val fieldReference = psiElement as? FieldReference ?: return null
val fieldName = fieldReference.name ?: return null
if (fieldName.isEmpty()) return null

//
// Handles:
Expand Down Expand Up @@ -53,7 +55,7 @@ class AssociatedTableTypeProvider : PhpTypeProvider4 {
}

override fun complete(expression: String, project: Project): PhpType? {
val possibleTableName = expression.substring(3)
val possibleTableName = expression.substringOrNull(3) ?: return null
val settings = Settings.getInstance(project)
if (!settings.cake3Enabled) {
return null
Expand Down
54 changes: 54 additions & 0 deletions src/test/kotlin/com/daveme/chocolateCakePHP/test/StringsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.daveme.chocolateCakePHP.test

import com.daveme.chocolateCakePHP.substringOrNull
import junit.framework.TestCase

class StringsTest : TestCase() {

private val text = "Kotlin is fun!"

// Test case 1: Valid indices within bounds
fun testValidIndicesWithinBounds() {
assertEquals("Kotlin", text.substringOrNull(0, 6))
}

// Test case 2: Only startIndex provided, endIndex defaults to the end of the string
fun testOnlyStartIndexProvided() {
assertEquals("is fun!", text.substringOrNull(7))
}

// Test case 3: Full string when startIndex is 0 and endIndex is the length of the string
fun testFullString() {
assertEquals("Kotlin is fun!", text.substringOrNull(0, text.length))
}

// Test case 4: startIndex is out of bounds
fun testStartIndexOutOfBounds() {
assertNull(text.substringOrNull(20, 25))
}

// Test case 5: endIndex is out of bounds
fun testEndIndexOutOfBounds() {
assertNull(text.substringOrNull(0, 20))
}

// Test case 6: startIndex > endIndex, invalid range
fun testStartIndexGreaterThanEndIndex() {
assertNull(text.substringOrNull(5, 3))
}

// Test case 7: Empty substring when startIndex equals endIndex
fun testEmptySubstring() {
assertEquals("", text.substringOrNull(5, 5))
}

// Test case 8: startIndex equals length, expecting empty string
fun testStartIndexAtLength() {
assertEquals("", text.substringOrNull(text.length))
}

// Test case 9: Both indices are zero, expecting empty string
fun testBothIndicesZero() {
assertEquals("", text.substringOrNull(0, 0))
}
}

0 comments on commit 7f3b26f

Please sign in to comment.