Skip to content

StringUtils.chopNewline(''): prevent StringIndexOutOfBoundsException - #380

Merged
elharo merged 1 commit into
masterfrom
fix/chopNewline-empty-string
Jul 1, 2026
Merged

StringUtils.chopNewline(''): prevent StringIndexOutOfBoundsException#380
elharo merged 1 commit into
masterfrom
fix/chopNewline-empty-string

Conversation

@elharo

@elharo elharo commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

StringUtils.chopNewline("") throws StringIndexOutOfBoundsException because lastIdx = str.length() - 1 evaluates to -1 when str is empty, then str.charAt(lastIdx) reads at index -1.

Added an empty-string guard before computing lastIdx, consistent with the guard already used by chop().

Fixes #379

@elharo
elharo requested a review from slachiewicz July 1, 2026 11:57
@slachiewicz slachiewicz added the bug Something isn't working label Jul 1, 2026
@elharo
elharo merged commit 73b979e into master Jul 1, 2026
15 checks passed
@elharo
elharo deleted the fix/chopNewline-empty-string branch July 1, 2026 13:56
@github-actions github-actions Bot added this to the 3.5.0 milestone Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringUtils.chopNewline("") throws StringIndexOutOfBoundsException

2 participants