Skip to content

Fix memory leak in stonith__api_new() error path#4145

Open
Jazyy wants to merge 1 commit into
ClusterLabs:mainfrom
Jazyy:main
Open

Fix memory leak in stonith__api_new() error path#4145
Jazyy wants to merge 1 commit into
ClusterLabs:mainfrom
Jazyy:main

Conversation

@Jazyy

@Jazyy Jazyy commented Jul 7, 2026

Copy link
Copy Markdown

lib/fencing/st_client: Free callback table in stonith__api_new error path

When the third calloc() fails in stonith__api_new(), the previously allocated stonith_op_callback_table hash table was not being freed, causing a memory leak. Add proper cleanup to destroy the hash table before returning NULL. This ensures all allocated resources are properly released even in error conditions.

@knet-jenkins

knet-jenkins Bot commented Jul 7, 2026

Copy link
Copy Markdown

Can one of the project admins check and authorise this run please: https://haci.fast.eng.rdu2.dc.redhat.com/job/pacemaker/job/pacemaker-pipeline/job/PR-4145/1/input

@nrwahl2 nrwahl2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

Comment thread lib/fencing/st_client.c

new_stonith->cmds = calloc(1, sizeof(stonith_api_operations_t));
if (new_stonith->cmds == NULL) {
if (private->stonith_op_callback_table) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can't be NULL. We created it via pcmk__intkey_table(), which calls g_hash_table_new_full(). The GLib allocator functions behave similarly to pcmk__assert_alloc(): they exit with an error if allocation fails:

With that in mind, let's go ahead and replace all the calloc() calls with pcmk__assert_alloc() (unless @clumens objects) and drop all of these NULL checks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That sounds fine to me. And if we're starting a list of other things to fix in this function, let me add the following:

  • The private variable appears to only exist to save typing later.
  • notify_deletes is a bool, not a gboolean.
  • We don't typically line things up vertically on the equals sign like we do at the bottom of this function.

@nrwahl2 nrwahl2 Jul 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And if we're starting a list of other things to fix in this function, let me add the following:

Agreed, but I'm not trying to give @Jazyy a list of stylistic issues to fix. Just asking them to use pcmk__assert_alloc() instead of calloc(), which would avoid this issue, since this function already terminates on memory error.

@nrwahl2 nrwahl2 added the waiting for author Review has been provided, and we're waiting for a response from the author of the pull request label Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for author Review has been provided, and we're waiting for a response from the author of the pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants