Greetings. Recently, I have used my memory leak detector to examine the latest version of nanomq. From the latest version of nanomq/nng, I have discovered 5 memory leak defects. Below are the detailed descriptions of these defects:

First defect:
At line 938, a pointer named mqdata is defined and a dynamic memory region is allocated using nng_alloc. When the if statement at line 939 returns false, it indicates successful allocation of the dynamic memory region. At line 952, the program uses this pointer to create an object named mqs_obj. When the if statement at line 953 returns true, it signifies the failure of creating the object. However, even if the object creation fails, the dynamic memory region pointed to by mqdata is successfully allocated. Therefore, the memory region pointed to by mqdata should be freed. The defect path is as follows:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_12.png

Second defect:
At line 1378, dynamic memory allocation for the pointer db and at line 1379, dynamic memory allocation for the pointer topic may not necessarily fail simultaneously. Therefore, when the if statement at line 1387 returns true, an attempt should be made to free the pointers db and topic. Detailed information on the defect is depicted in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_13.png

Third defect:
When the if statement at line 1574 returns false, it indicates successful allocation of dynamic memory region for the pointer sn. However, when the if statement at line 1576 returns true, the dynamic memory region pointed to by sn should be freed. Detailed information on the defect is illustrated in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_14.png

Fourth defect:
At line 1692, a dynamic memory region is allocated for the pointer topic using the function nng_alloc. When the if statement at line 1700 returns true, the program returns at line 1701 without freeing the dynamic memory region pointed to by topic. This results in a memory leak as depicted in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_15.png

The fifth defect:
At line 446, the condition should be checking the pointer lens instead of the variable len:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_16.png
Several potential memory leak defects in the latest version of nanomq/nng.
This is a GitHub Issue
repo:nanomq
owner:emqx
Title : Several potential memory leak defects in the latest version of nanomq/nng.
Issue date:

--- start body ---
Greetings. Recently, I have used my memory leak detector to examine the latest version of nanomq. From the latest version of nanomq/nng, I have discovered 5 memory leak defects. Below are the detailed descriptions of these defects:

First defect:
At line 938, a pointer named mqdata is defined and a dynamic memory region is allocated using nng_alloc. When the if statement at line 939 returns false, it indicates successful allocation of the dynamic memory region. At line 952, the program uses this pointer to create an object named mqs_obj. When the if statement at line 953 returns true, it signifies the failure of creating the object. However, even if the object creation fails, the dynamic memory region pointed to by mqdata is successfully allocated. Therefore, the memory region pointed to by mqdata should be freed. The defect path is as follows:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_12.png

Second defect:
At line 1378, dynamic memory allocation for the pointer db and at line 1379, dynamic memory allocation for the pointer topic may not necessarily fail simultaneously. Therefore, when the if statement at line 1387 returns true, an attempt should be made to free the pointers db and topic. Detailed information on the defect is depicted in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_13.png

Third defect:
When the if statement at line 1574 returns false, it indicates successful allocation of dynamic memory region for the pointer sn. However, when the if statement at line 1576 returns true, the dynamic memory region pointed to by sn should be freed. Detailed information on the defect is illustrated in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_14.png

Fourth defect:
At line 1692, a dynamic memory region is allocated for the pointer topic using the function nng_alloc. When the if statement at line 1700 returns true, the program returns at line 1701 without freeing the dynamic memory region pointed to by topic. This results in a memory leak as depicted in the following diagram:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_15.png

The fifth defect:
At line 446, the condition should be checking the pointer lens instead of the variable len:
https://github.com/LuMingYinDetect/nanomq_defects/blob/main/nanomq_16.png
--- end body ---

The issue describes several potential memory leak defects in the nanomq/nng codebase. These defects could lead to memory not being freed properly, which can cause increased memory usage over time and potentially lead to application crashes or degraded performance. However, these issues do not represent direct security vulnerabilities, as they do not allow for unauthorized access or manipulation of data. They are more related to resource management and stability of the application.