Skip to content

[LANG-1828] Fix OOM in StringUtils.leftPad/rightPad when size is Integer.MIN_VALUE#1736

Closed
dhruvaggarwal2000 wants to merge 1 commit into
apache:masterfrom
dhruvaggarwal2000:LANG-1828-StringUtil-rightpad-OOM
Closed

[LANG-1828] Fix OOM in StringUtils.leftPad/rightPad when size is Integer.MIN_VALUE#1736
dhruvaggarwal2000 wants to merge 1 commit into
apache:masterfrom
dhruvaggarwal2000:LANG-1828-StringUtil-rightpad-OOM

Conversation

@dhruvaggarwal2000

@dhruvaggarwal2000 dhruvaggarwal2000 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This issue is tracked as LANG-1828.

Repro:
size - str.length() underflows when size = Integer.MIN_VALUE, bypassing the pads <= 0 early return and causing an OOM allocation.

Fix: Short-circuiting on size <= str.length() before the subtraction — no behavior change for any valid input.

Methods affected:

  • leftPad(String, int, char)
  • leftPad(String, int, String)
  • rightPad(String, int, char)
  • rightPad(String, int, String)
Compatibility report (mvn package japicmp:cmp against 3.20.0)

org.apache.commons.lang3.StringUtils

  • Binary-compatible
  • Source-compatible
  • Serialization-compatible
Status Modifiers Type Name Extends JDK Serialization Compatibility Changes
Modified public Class StringUtils [Object] JDK 8 ![Not serializable] ![No changes]

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • AI used: Claude (Anthropic, claude-opus-4-7) via the Claude Code CLI, to draft this PR description. Per ASF guidance: Anthropic's terms place no restrictions inconsistent with the OSD; no third-party material is reproduced in the output.
  • Run a successful build using the default Maven.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@dhruvaggarwal2000 dhruvaggarwal2000 force-pushed the LANG-1828-StringUtil-rightpad-OOM branch from b95effb to c89da12 Compare June 29, 2026 20:47
@dhruvaggarwal2000 dhruvaggarwal2000 force-pushed the LANG-1828-StringUtil-rightpad-OOM branch from c89da12 to 09745ce Compare June 29, 2026 21:08
garydgregory added a commit that referenced this pull request Jun 29, 2026
@garydgregory

Copy link
Copy Markdown
Member

@dhruvaggarwal2000
Thank you for the PR. Please try to reduce the usual AI verbosity of code generation. I refactored the guard clauses and pushed to git master (instead of going round and round here to save time). You are credited in changes.xml. Closing.

@dhruvaggarwal2000

Copy link
Copy Markdown
Contributor Author

Got it — trimmed the description. Noted for future PRs

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.

2 participants