Fix segfault when accessing fields/field_types on freed result#1427
Open
jonathan-biskycreative wants to merge 1 commit into
Open
Conversation
3b3cd2c to
a8b1f7c
Compare
Add resultFreed check to rb_mysql_result_fetch_fields and rb_mysql_result_fetch_field_types before accessing wrapper->result. Additionally, eagerly populate fields when a result is created (for non-streaming results) to prevent the issue from occurring at all. This ensures fields are fetched before any iteration can free the result. The segfault occurs when: 1. A query returns 0 rows 2. Internal row caching iterates (0 iterations), never populating wrapper->fields 3. Result is freed after iteration completes 4. .fields is called, wrapper->fields is Qnil 5. mysql_num_fields(wrapper->result) accesses freed memory -> SEGFAULT This check already exists in other functions (e.g., rb_mysql_result_each_) but was missing in the fields accessor functions. Based on fix for brianmario#1426
a8b1f7c to
e3b4c3e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes segfault when accessing
.fieldsor.field_typeson a Result object after the internal result has been freed. This is particularly common with 0-row query results.Problem
A segfault occurs in the following sequence:
wrapper->fields.fieldsis called,wrapper->fieldsisQnilmysql_num_fields(wrapper->result)accesses freed memoryFix
This PR includes two improvements:
1. Eager field population (primary fix)
Fields are now populated immediately when a result is created, before any iteration can free the result:
This prevents the problematic scenario from occurring at all.
2. Safety check (fallback)
The
resultFreedcheck (which already exists in other functions likerb_mysql_result_each_) is now added to the fields accessor functions:If an edge case still occurs, this raises a catchable Ruby exception instead of crashing the process.
Testing
This fix has been tested in a production Rails 8.0 application where the segfault was occurring ~9 times per month during queries that return 0 rows. With the eager field population, the issue no longer occurs.
Fixes #1426