Skip to content

fix: allocate longest palindrome result by length - #1593

Open
wjc2821296948 wants to merge 1 commit into
TheAlgorithms:masterfrom
wjc2821296948:fix-longest-palindrome-allocation-960
Open

wjc2821296948 wants to merge 1 commit into
TheAlgorithms:masterfrom
wjc2821296948:fix-longest-palindrome-allocation-960

Conversation

@wjc2821296948

Copy link
Copy Markdown

longestPalindrome() allocated the result with malloc(sizeof(s)), which only reserves the size of the input pointer rather than the palindrome string. A palindrome longer than that allocation can therefore overflow the result buffer.

Allocate sz_max + 1 bytes instead so the selected palindrome and its terminating null byte always fit.

Validated locally with a 10-character palindrome result under AddressSanitizer.

Refs #960

Co-Authored-By: chaogeshuodiannao <chaogeshuodiannao@users.noreply.github.com>
@github-actions github-actions Bot added the Leetcode folder changes Changes to Leetcode folder. Known CI issues. label Oct 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Leetcode folder changes Changes to Leetcode folder. Known CI issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant