Skip to content

--bug=fix bug:index out of range panic in v2.listItem (line 1369 in b… - #720

Open
chp0304 wants to merge 1 commit into
russross:masterfrom
chp0304:feature/bugfix/list_item
Open

--bug=fix bug:index out of range panic in v2.listItem (line 1369 in b…#720
chp0304 wants to merge 1 commit into
russross:masterfrom
chp0304:feature/bugfix/list_item

Conversation

@chp0304

@chp0304 chp0304 commented Jan 27, 2026

Copy link
Copy Markdown

Fix: Index out of range panic in listItem function

Problem

The listItem function in block.go can cause a panic with runtime error: index out of range when parsing markdown list items that don't end with a newline character.

Stack trace:

panic: runtime error: index out of range [329] with length 329
github.com/russross/blackfriday/v2.(*Markdown).listItem(...)
    block.go:1368

Root Cause

Several loops in the listItem function lack proper bounds checking:

  1. Line 1129-1131: Loop skipping whitespace doesn't check i < len(data)
  2. Line 1135-1137: Loop finding newline doesn't check bounds, and data[i-1] causes index -1 access when i=0
  3. Line 1160-1162: Similar missing bounds check when finding line endings
  4. Line 1253-1255: Loop in definition list handling lacks bounds check
  5. dliPrefix function: Accesses data[i+1] without checking array length

Fix

Added proper bounds checking to all affected loops:

// Before
for data[i] == ' ' { i++ }

// After  
for i < len(data) && data[i] == ' ' { i++ }
// Before
for i > 0 && data[i-1] != '\n' { i++ }

// After
for i < len(data) && (i == 0 || data[i-1] != '\n') { i++ }

Changes

  • block.go: Added bounds checking in listItem function (4 locations)
  • block.go: Added length check in dliPrefix function
  • block_test.go: Added TestListItemBoundsCheck with 9 test cases covering edge cases

Test Cases

The new test covers:

  • Unordered/ordered lists without trailing newline
  • Lists with excessive spaces
  • Nested lists without trailing newline
  • Definition lists without trailing newline
  • Empty list markers
  • Lists with only spaces after marker

All existing tests pass.

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