Fix: handle leak in abi tests
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 17 May 2021 12:40:17 +0000 (08:40 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 17 May 2021 14:07:00 +0000 (10:07 -0400)
Coverity finds that dlopen_ust leaks handles. Modify the code structure
to keep track of those handles in library descriptors so they are not
leaked.

*** CID 1453155:    (RESOURCE_LEAK)
/tests/regression/abi0-conflict/app_ust_dlopen.c: 35 in dlopen_ust()
29                     printf("Error: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
30                     ret = EXIT_FAILURE;
31             } else {
32                     printf("Success: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
33             }
34
>>>     CID 1453155:    (RESOURCE_LEAK)
>>>     Variable "handle" going out of scope leaks the storage it points to.
35             return ret;
36     }
37
38     static
39     int dlopen_abi0(void)
40     {
/tests/regression/abi0-conflict/app_noust_dlopen.c: 31 in dlopen_ust()
25                     printf("Error: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
26                     ret = EXIT_FAILURE;
27             } else {
28                     printf("Success: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
29             }
30
>>>     CID 1453155:    (RESOURCE_LEAK)
>>>     Variable "handle" going out of scope leaks the storage it points to.
31             return ret;
32     }
33
34     static
35     int dlopen_abi0(void)
36     {

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I967c2e8741c6d42c0c12e19fbadc81e1c91d1e0f

tests/regression/abi0-conflict/app_noust_dlopen.c
tests/regression/abi0-conflict/app_ust_dlopen.c

index 9a52bc79da8429d0aeb5e1c4d89670278b32f001..e0519c81d6bcda96e5aa2f94b310af2cdc1c793a 100644 (file)
 #define LTTNG_UST_LIB_ABI0_SO_NAME "libfakeust0.so"
 #define LTTNG_UST_LIB_ABI1_SO_NAME "liblttng-ust.so.1"
 
+struct lib_desc {
+       const char *soname;
+       void *handle;
+};
+
+static struct lib_desc lib_desc[] = {
+       [0] = {
+               .soname = LTTNG_UST_LIB_ABI0_SO_NAME,
+       },
+       [1] = {
+               .soname = LTTNG_UST_LIB_ABI1_SO_NAME,
+       },
+       [2] = {
+               .soname = LTTNG_UST_LIB_ABI1_SO_NAME,
+       },
+};
+
 static
-int dlopen_ust(const char *lib_soname)
+int dlopen_ust(struct lib_desc *desc)
 {
        int ret = EXIT_SUCCESS;
-       void *handle;
 
-       handle = dlopen(lib_soname, RTLD_NOW | RTLD_GLOBAL);
-       if (!handle) {
-               printf("Error: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
+       desc->handle = dlopen(desc->soname, RTLD_NOW | RTLD_GLOBAL);
+       if (!desc->handle) {
+               printf("Error: dlopen of liblttng-ust shared library (%s).\n", desc->soname);
                ret = EXIT_FAILURE;
        } else {
-               printf("Success: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
+               printf("Success: dlopen of liblttng-ust shared library (%s).\n", desc->soname);
        }
 
        return ret;
@@ -34,13 +50,13 @@ int dlopen_ust(const char *lib_soname)
 static
 int dlopen_abi0(void)
 {
-       return dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       return dlopen_ust(&lib_desc[0]);
 }
 
 static
 int dlopen_abi1(void)
 {
-       return dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       return dlopen_ust(&lib_desc[1]);
 }
 
 static
@@ -48,11 +64,11 @@ int dlopen_abi0_abi1(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       ret = dlopen_ust(&lib_desc[0]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
 
        return ret;
 }
@@ -62,11 +78,11 @@ int dlopen_abi1_abi0(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       ret = dlopen_ust(&lib_desc[0]);
 
        return ret;
 }
@@ -76,11 +92,11 @@ int dlopen_abi1_abi1(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[2]);
 
        return ret;
 }
index ad9d1b294bca1d2789c6e1eafd1cb4d44de6518c..fe7fccc7d6e673e338f8116ae4dd106728876de7 100644 (file)
 #define LTTNG_UST_LIB_ABI0_SO_NAME "libfakeust0.so"
 #define LTTNG_UST_LIB_ABI1_SO_NAME "liblttng-ust.so.1"
 
+struct lib_desc {
+       const char *soname;
+       void *handle;
+};
+
+static struct lib_desc lib_desc[] = {
+       [0] = {
+               .soname = LTTNG_UST_LIB_ABI0_SO_NAME,
+       },
+       [1] = {
+               .soname = LTTNG_UST_LIB_ABI1_SO_NAME,
+       },
+       [2] = {
+               .soname = LTTNG_UST_LIB_ABI1_SO_NAME,
+       },
+};
+
 static
-int dlopen_ust(const char *lib_soname)
+int dlopen_ust(struct lib_desc *desc)
 {
        int ret = EXIT_SUCCESS;
-       void *handle;
 
-       handle = dlopen(lib_soname, RTLD_NOW | RTLD_GLOBAL);
-       if (!handle) {
-               printf("Error: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
+       desc->handle = dlopen(desc->soname, RTLD_NOW | RTLD_GLOBAL);
+       if (!desc->handle) {
+               printf("Error: dlopen of liblttng-ust shared library (%s).\n", desc->soname);
                ret = EXIT_FAILURE;
        } else {
-               printf("Success: dlopen of liblttng-ust shared library (%s).\n", lib_soname);
+               printf("Success: dlopen of liblttng-ust shared library (%s).\n", desc->soname);
        }
 
        return ret;
@@ -38,13 +54,13 @@ int dlopen_ust(const char *lib_soname)
 static
 int dlopen_abi0(void)
 {
-       return dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       return dlopen_ust(&lib_desc[0]);
 }
 
 static
 int dlopen_abi1(void)
 {
-       return dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       return dlopen_ust(&lib_desc[1]);
 }
 
 static
@@ -52,11 +68,11 @@ int dlopen_abi0_abi1(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       ret = dlopen_ust(&lib_desc[0]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
 
        return ret;
 }
@@ -66,11 +82,11 @@ int dlopen_abi1_abi0(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI0_SO_NAME);
+       ret = dlopen_ust(&lib_desc[0]);
 
        return ret;
 }
@@ -80,11 +96,11 @@ int dlopen_abi1_abi1(void)
 {
        int ret = EXIT_SUCCESS;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[1]);
        if (ret != EXIT_SUCCESS)
                return ret;
 
-       ret = dlopen_ust(LTTNG_UST_LIB_ABI1_SO_NAME);
+       ret = dlopen_ust(&lib_desc[2]);
 
        return ret;
 }
@@ -100,7 +116,6 @@ int main(int argc, char **argv)
 {
        int ret = EXIT_SUCCESS;
        const char *test_type;
-
        int i, netint;
        long values[] = { 1, 2, 3 };
        char text[10] = "test";
@@ -108,7 +123,6 @@ int main(int argc, char **argv)
        float flt = 2222.0;
        bool mybool = 123;      /* should print "1" */
 
-
        if (argc != 2) {
                usage(argv);
                return EXIT_FAILURE;
This page took 0.028554 seconds and 4 git commands to generate.