Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match: avoid segmentation fault with no_regex #332

Merged

Conversation

dscho
Copy link
Contributor

@dscho dscho commented Jan 30, 2023

In f398927 (Use different colors for subpattern matches., 2023-01-09), match_pattern() was changed so that the sp and ep pointers no longer refer to a single start/end but to an array.

Unfortunately, the match() function that is used in the no-regex case was not adjusted correctly. While it does advance sp and ep to the next array elements in case of a match, that advancement is only visible to the match() function, but not to its caller, match_pattern(), which subsequently assigns *sp = *ep = NULL, overwriting the values that were assigned by the match() function.

As a consequence, the invariant where search_range() expects the sp/ep values to be non-NULL if match_pattern() returns non-zero is violated, and the hilite_line() function will call create_hilites() with those invalid values and subsequently access memory at an insanely large start_index, and crash.

Fix this by changing the match() function in a way where the sp/ep modifications are visible to its caller.

In f398927 (Use different colors for subpattern matches., 2023-01-09),
`match_pattern()` was changed so that the `sp` and `ep` pointers no
longer refer to a single start/end but to an array.

Unfortunately, the `match()` function that is used in the no-regex case
was not adjusted correctly. While it does advance `sp` and `ep` to the
next array elements in case of a match, that advancement is only visible
to the `match()` function, but not to its caller, `match_pattern()`,
which subsequently assigns `*sp = *ep = NULL`, overwriting the values
that were assigned by the  `match()` function.

As a consequence, the invariant where `search_range()` expects the
`sp`/`ep` values to be non-`NULL` if `match_pattern()` returns non-zero
is violated, and the `hilite_line()` function will call
`create_hilites()` with those invalid values and subsequently access
memory at an insanely large `start_index`, and crash.

Fix this by changing the `match()` function in a way where the `sp`/`ep`
modifications are visible to its caller.

Signed-off-by: Johannes Schindelin <[email protected]>
@gwsw gwsw merged commit 40ba505 into gwsw:master Jan 30, 2023
@dscho dscho deleted the avoid-segfault-while-highlighting-with-no-matches branch January 30, 2023 20:05
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