Skip to content

Memory Leak in pgmoneta_management_create_header function #460

Closed

Description

@Annupamaa

Describe the bug

While trying to create a master-key hitting LeakSanitizer mem leak error.

To Reproduce

  1. Create a key using this command
    pgmoneta-admin master-key

  2. then again retry to create using same command.

Version

What is the version of pgmoneta ?
pgmoneta 0.16.0

PostgreSQL

What is the version of PostgreSQL ?
PostgresSQL 16

OS

Which Operating System (OS) is used ?
Linux

Error:
`pgmoneta@ubuntudemo:~$ pgmoneta-admin master-key > out
pgmoneta-admin: The file ~/.pgmoneta/master.key already exists
pgmoneta-admin: Cannot generate master key

=================================================================
==1371207==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
#0 0x6444fb74c353 in malloc (/usr/local/bin/pgmoneta-admin+0xcb353) (BuildId: 00c99f0778469853d3870775b2959db9d4d3cc3a)
#1 0x73b811d1796d in pgmoneta_json_create /usr/local/pgmoneta/src/libpgmoneta/json.c:407:21
#2 0x73b811d2873f in pgmoneta_management_create_header /usr/local/pgmoneta/src/libpgmoneta/management.c:1658:8
#3 0x6444fb78b984 in master_key /usr/local/pgmoneta/src/admin.c:318:8
#4 0x6444fb78b042 in main /usr/local/pgmoneta/src/admin.c:250:11
#5 0x73b81182a1c9 (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6)
#6 0x73b81182a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6)
#7 0x6444fb6b1504 in _start (/usr/local/bin/pgmoneta-admin+0x30504) (BuildId: 00c99f0778469853d3870775b2959db9d4d3cc3a)`

Activity

  1. added
    bug
    on Jan 17, 2025
  2. Annupamaa commented on Jan 17, 2025

    @Annupamaa
    ContributorAuthor

    So I tried to debug this and seems like in pgmoneta_management_create_header() we are allocation the struct json object j and freeing as well in error section by calling pgmoneta_json_destroy() where we are also checking if object is NULL. Hence, the flow looks good and the issue seems to be strange to me.
    However, I tried to add explicit checks for NULL before calling pgmoneta_json_destroy() for both j and header in the error part and it seems to be resolving the issue.
    However I am not sure, if this is the correct way, I am really keen interested to work on this issue, let me know what need to be done.

  3. jesperpedersen commented on Jan 17, 2025

    @jesperpedersen
    Member

    @shikharish Can you take a look ?

  4. added a commit that references this issue on Jan 18, 2025
  5. shikharish commented on Jan 18, 2025

    @shikharish
    Collaborator

    @Annupamaa There was no cleanup in case of error in master_key(). I missed it in my last PR, thanks for reporting this. I have fixed it for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions