Skip to content

Fix heap-buffer-overflow in keyword highlighting (#36)#116

Open
Nicolas0315 wants to merge 1 commit intoantirez:masterfrom
Nicolas0315:fix/heap-buffer-overflow-keyword-matching
Open

Fix heap-buffer-overflow in keyword highlighting (#36)#116
Nicolas0315 wants to merge 1 commit intoantirez:masterfrom
Nicolas0315:fix/heap-buffer-overflow-keyword-matching

Conversation

@Nicolas0315
Copy link

Summary

Fix a heap-buffer-overflow in editorUpdateSyntax() triggered by memcmp reading past the end of the rendered line buffer during keyword matching.

Root Cause

The keyword matching loop uses memcmp(p, keywords[j], klen) without checking that at least klen bytes remain from the current position p to the end of the line. When a short line ends with a prefix that partially matches a keyword (e.g. a line containing just int being compared against a 6-byte keyword), memcmp reads past the allocated buffer.

Fix

Add a bounds check before the memcmp call:

if (klen <= row->rsize-i &&
    !memcmp(p,keywords[j],klen) &&
    is_separator(*(p+klen)))

This ensures we only compare within the allocated line buffer. Short-circuit evaluation means memcmp is never called when there aren't enough bytes remaining.

Verification

Compiled with AddressSanitizer:

clang -fsanitize=address -O1 -g -o kilo kilo.c
./kilo kilo.c  # No ASAN errors

Fixes #36

editorUpdateSyntax() uses memcmp(p, keywords[j], klen) to match
keywords against the current position in the rendered line, but
does not check that at least klen bytes remain from p to the end
of the line. When a short line ends with a prefix that partially
matches a keyword, memcmp reads past the allocated buffer.

Add a bounds check (klen <= row->rsize - i) before the memcmp
call to ensure we only compare within the allocated line buffer.

Verified with AddressSanitizer: the overflow no longer triggers.

Fixes antirez#36
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.

Heap buffer overflow in kilo's editor_update_syntax(…)

1 participant