From 14b56624c8e79b7f1630978c5995a98ef116248a Mon Sep 17 00:00:00 2001 From: наб Date: Mon, 3 May 2021 12:01:13 +0200 Subject: zfs_get_enclosure_sysfs_path(): don't leak dev path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also always free tmp2 at the end Before: nabijaczleweli@tarta:~/uwu$ valgrind --leak-check=full ./blergh ==8947== Memcheck, a memory error detector ==8947== Using Valgrind-3.14.0 and LibVEX ==8947== Command: ./blergh ==8947== (null) ==8947== ==8947== HEAP SUMMARY: ==8947== in use at exit: 23 bytes in 1 blocks ==8947== total heap usage: 3 allocs, 2 frees, 1,147 bytes allocated ==8947== ==8947== 23 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==8947== at 0x483577F: malloc (vg_replace_malloc.c:299) ==8947== by 0x48D74B7: vasprintf (vasprintf.c:73) ==8947== by 0x48B7833: asprintf (asprintf.c:35) ==8947== by 0x401258: zfs_get_enclosure_sysfs_path (zutil_device_path_os.c:191) ==8947== by 0x401482: main (blergh.c:107) ==8947== ==8947== LEAK SUMMARY: ==8947== definitely lost: 23 bytes in 1 blocks ==8947== indirectly lost: 0 bytes in 0 blocks ==8947== possibly lost: 0 bytes in 0 blocks ==8947== still reachable: 0 bytes in 0 blocks ==8947== suppressed: 0 bytes in 0 blocks ==8947== ==8947== For counts of detected and suppressed errors, rerun with: -v ==8947== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) nabijaczleweli@tarta:~/uwu$ sed -n 191p zutil_device_path_os.c tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name); After: nabijaczleweli@tarta:~/uwu$ valgrind --leak-check=full ./blergh ==9512== Memcheck, a memory error detector ==9512== Using Valgrind-3.14.0 and LibVEX ==9512== Command: ./blergh ==9512== (null) ==9512== ==9512== HEAP SUMMARY: ==9512== in use at exit: 0 bytes in 0 blocks ==9512== total heap usage: 3 allocs, 3 frees, 1,147 bytes allocated ==9512== ==9512== All heap blocks were freed -- no leaks are possible ==9512== ==9512== For counts of detected and suppressed errors, rerun with: -v ==9512== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) Reviewed-by: Brian Behlendorf Signed-off-by: Ahelenia Ziemiańska Closes #11993 --- lib/libzutil/os/linux/zutil_device_path_os.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/libzutil/os/linux/zutil_device_path_os.c b/lib/libzutil/os/linux/zutil_device_path_os.c index dae5b8971..71134a538 100644 --- a/lib/libzutil/os/linux/zutil_device_path_os.c +++ b/lib/libzutil/os/linux/zutil_device_path_os.c @@ -195,10 +195,8 @@ zfs_get_enclosure_sysfs_path(const char *dev_name) } dp = opendir(tmp1); - if (dp == NULL) { - tmp1 = NULL; /* To make free() at the end a NOP */ + if (dp == NULL) goto end; - } /* * Look though all sysfs entries in /sys/block//device for @@ -216,11 +214,8 @@ zfs_get_enclosure_sysfs_path(const char *dev_name) size = readlink(tmp2, buf, sizeof (buf)); /* Did readlink fail or crop the link name? */ - if (size == -1 || size >= sizeof (buf)) { - free(tmp2); - tmp2 = NULL; /* To make free() at the end a NOP */ + if (size == -1 || size >= sizeof (buf)) break; - } /* * We got a valid link. readlink() doesn't terminate strings -- cgit v1.2.3