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

apacheGH-491: Check return value of vasprintf and asprintf #781

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ShaiviAgarwal2
Copy link

Rationale for this change

The change addresses potential memory management issues in the discovery server, ensuring proper resource allocation, error handling, and cleanup to enhance stability and prevent crashes or memory leaks.

What changes are included in this PR?

  • Added checks for memory allocation failures (malloc, strdup, asprintf) with appropriate error handling.
  • Improved cleanup logic to prevent memory leaks during error scenarios.

Are these changes tested?

These changes are tested with existing functionality.

Are there any user-facing changes?

No user-facing changes. These improvements focus on internal stability and reliability.

@ShaiviAgarwal2
Copy link
Author

@PengZheng
I have made some changes to a few files. Could you please review them and confirm if these align with the improvements you were looking for?
Once confirmed, I will proceed with applying similar changes to the rest of the codebase.

@ShaiviAgarwal2
Copy link
Author

@xuzhenbao @PengZheng
Could you please help me to resolve the conflicts caused?

@PengZheng
Copy link
Contributor

Once confirmed, I will proceed with applying similar changes to the rest of the codebase.

As mentioned before, the framework part is undergoing active refactor, see #779 for a recent example.
Thus I suggest you leave that part untouched and let us finish the refactoring first.

Could you please help me to resolve the conflicts caused?

Please note that conflict should be (and can only be) resolved in your own branch.

rc= asprintf(&port_str, "%li", port);
if(rc < 0 || port_str == NULL) {
celix_bundleContext_log(ctx, CELIX_LOG_LEVEL_ERROR, "Cannot allocate memory for port string.");
free(httpRoot);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it double-free?

@@ -147,10 +147,12 @@ celix_event_admin_remote_provider_mqtt_t* celix_earpm_create(celix_bundle_contex
return NULL;
}

if (asprintf(&earpm->syncEventAckTopic, CELIX_EARPM_SYNC_EVENT_ACK_TOPIC_PREFIX"%s", earpm->fwUUID) < 0) {
earpm->syncEventAckTopic = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that earpm is calloced, it is unnecessary to set it to NULL explicitly.

Copy link
Contributor

@PengZheng PengZheng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that you need to familiarize yourself with the code base before making changes, which already introduced memory safety bugs.

It may help to learn how to add tests for your changes, especially the error injector tech. The double-free issue could be easily caught by such tests with the help of AddressSanitizer.

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.

3 participants