-
Notifications
You must be signed in to change notification settings - Fork 3.2k
FIX: Issue 4142 (segmentation fault that occurs whenon_conf_destroy)
#5240
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
base: master
Are you sure you want to change the base?
Conversation
…uble-free errors. Updated comments to clarify that interceptors are shallow-copied pointers and should not be destroyed when the application configuration is freed.
…with interceptors This commit introduces a new test case to verify that the on_conf_destroy callback is called correctly when rd_kafka_new() fails due to invalid SSL configuration. It ensures that interceptors are not destroyed multiple times, addressing a segfault issue in the error handling path.
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a critical double-free bug that caused segmentation faults when an on_conf_destroy interceptor callback was registered and rd_kafka_new() failed. The issue occurred because interceptor structures were being freed in the error path of rd_kafka_new(), even though they were shallow-copied pointers that belonged to the application's configuration object.
- Removed the call to
rd_kafka_interceptors_destroy()in therd_kafka_new()failure path - Added a regression test to verify the fix prevents the segfault
- Added clarifying comments explaining the shallow-copy ownership model
Reviewed Changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/rdkafka.c | Removed the interceptor destruction call in the error path and added documentation explaining the shallow-copy ownership |
| tests/0064-interceptors.c | Added regression test that registers an on_conf_destroy callback, forces rd_kafka_new() to fail, and verifies proper cleanup without crashes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
on_conf_destroy)on_conf_destroy)
Summary
This PR fixes a segmentation fault that occurs when an
on_conf_destroyinterceptor callback is registered andrd_kafka_new()fails. The bug caused interceptor structures to be freed twice (double-free), leading to a crash when the application properly calledrd_kafka_conf_destroy()after a failedrd_kafka_new().Motivation
Fixes #4142
When
rd_kafka_new()fails due to invalid configuration (e.g., invalid SSL settings), it takes thefail:cleanup path. In this path, the code was callingrd_kafka_interceptors_destroy(&rk->rk_conf), which freed the interceptor structures. However, these structures are shallow-copied pointers from the application's configuration object (app_conf). When the application then properly calledrd_kafka_conf_destroy(conf)to clean up, it attempted to access the already-freed interceptor structures, resulting in a double-free segmentation fault.Root Cause
In
src/rdkafka.c, therd_kafka_new()fail path (around line 2830):The problem is that
rk->rk_conf.interceptorscontains shallow-copied pointers to the same interceptor structures owned byapp_conf. Freeing them here causes a double-free when the user later callsrd_kafka_conf_destroy(app_conf).Changes
File:
src/rdkafka.cBefore:
After:
File:
tests/0064-interceptors.cAdded a new regression test
do_test_conf_destroy_interceptor_with_failed_new()that:on_conf_destroyinterceptor callbackrd_kafka_new()to fail with invalid SSL configurationrd_kafka_conf_destroy()and verifies the callback is called exactly onceTesting
Reproduction of Error
Created
bug_4142.c:Compilation:
gcc -o bug_4142_test bug_4142.c -I./src ./src/librdkafka.a \ -lssl -lcrypto -lsasl2 -lz -lcurl -lpthreadResults:
Before fix:
After fix:
Librdkafka Test Suite