Fix Stop Index Check in strSubstring Function for Accurate Substring Extraction! ( Update functions.go )#2794
Open
ImLeonLi wants to merge 2 commits intoinfluxdata:masterfrom
Open
Fix Stop Index Check in strSubstring Function for Accurate Substring Extraction! ( Update functions.go )#2794ImLeonLi wants to merge 2 commits intoinfluxdata:masterfrom
ImLeonLi wants to merge 2 commits intoinfluxdata:masterfrom
Conversation
**Issue Description:**
The current `strSubstring` function has a potential issue with the stop index check. The condition `if stop >= len(str)` may result in undesired behavior, especially when attempting to include the last character in the substring. Since the slicing operation `v = str[start : stop+1]` is left-closed and right-open, the character at the `stop` index is excluded.
**Proposed Solution:**
To address this, it is suggested to modify the condition to `if stop > len(str) || stop < 0` for a more accurate check on invalid stop indices. Additionally, the error message should be updated to reflect this change:
```go
if stop > len(str) || stop < 0 {
return nil, fmt.Errorf("invalid stop index for string in strSubstring: %d", stop)
}
v = str[start : stop+1]
```
This modification ensures correct substring extraction, even when attempting to include the character at the stop index.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue Description:
The current
strSubstringfunction has a potential issue with the stop index check. The conditionif stop >= len(str)may result in undesired behavior, especially when attempting to include the last character in the substring. Since the slicing operationv = str[start : stop+1]is left-closed and right-open, the character at thestopindex is excluded.Proposed Solution:
To address this, it is suggested to modify the condition to
if stop > len(str) || stop < 0for a more accurate check on invalid stop indices. Additionally, the error message should be updated to reflect this change:This modification ensures correct substring extraction, even when attempting to include the character at the stop index.
Required checklist
/etcfolder andNewDemoConfigmethods) (influxdb and plutonium)Description
1-3 sentences describing the PR (or link to well written issue)
Context
Why was this added? What value does it add? What are risks/best practices?
Affected areas (if applicable):
List of user-visible changes. As a user, what would I need to see in docs?
Examples:
CLI commands, subcommands, and flags
API changes
Configuration (sample config blocks)
Severity (optional)
i.e., ("recommend to upgrade immediately", "upgrade at your leasure", etc.)
Note for reviewers:
Check the semantic commit type: