Skip to content
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

Fix Stop Index Check in strSubstring Function for Accurate Substring Extraction! ( Update functions.go ) #2794

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lichenliang666
Copy link

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:

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.

Required checklist

  • Sample config files updated (both /etc folder and NewDemoConfig methods) (influxdb and plutonium)
  • openapi swagger.yml updated (if modified API) - link openapi PR
  • Signed CLA (if not already signed)

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:

  • Feat: a feature with user-visible changes
  • Fix: a bug fix that we might tell a user “upgrade to get this fix for your issue”
  • Chore: version bumps, internal doc (e.g. README) changes, code comment updates, code formatting fixes… must not be user facing (except dependency version changes)
  • Build: build script changes, CI config changes, build tool updates
  • Refactor: non-user-visible refactoring
  • Check the PR title: we should be able to put this as a one-liner in the release notes

**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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant