From 7f7e56c698529bfd49a990caf1c55dcf651a9493 Mon Sep 17 00:00:00 2001 From: Danny Al-Gaaf Date: Thu, 26 Jun 2014 03:49:22 +0200 Subject: [PATCH] scratchtool.c: fix resource leak and error handling Check the result of rados_ioctx_create()before using io_ctx. Free io_ctx where needed. Fix messages at the end of testrados(). CID 1219613 (#1 of 1): Resource leak (RESOURCE_LEAK) leaked_storage: Variable io_ctx going out of scope leaks the storage it points to. Signed-off-by: Danny Al-Gaaf --- src/tools/scratchtool.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/tools/scratchtool.c b/src/tools/scratchtool.c index 22c6a9fb16bad..596d5cfc21dd1 100644 --- a/src/tools/scratchtool.c +++ b/src/tools/scratchtool.c @@ -163,6 +163,10 @@ static int testrados(void) rados_ioctx_t io_ctx; r = rados_ioctx_create(cl, "foo", &io_ctx); + if (r < 0) { + printf("error creating ioctx\n"); + goto out_err; + } printf("rados_ioctx_create = %d, io_ctx = %p\n", r, io_ctx); /* list all pools */ @@ -174,7 +178,7 @@ static int testrados(void) if (r != buf_sz) { printf("buffer size mismatch: got %d the first time, but %d " "the second.\n", buf_sz, r); - goto out_err; + goto out_err_cleanup; } const char *b = buf; printf("begin pools.\n"); @@ -224,21 +228,21 @@ static int testrados(void) /* attrs */ if (do_rados_setxattr(io_ctx, oid, "b", "2")) - goto out_err; + goto out_err_cleanup; if (do_rados_setxattr(io_ctx, oid, "a", "1")) - goto out_err; + goto out_err_cleanup; if (do_rados_setxattr(io_ctx, oid, "c", "3")) - goto out_err; + goto out_err_cleanup; if (do_rados_getxattr(io_ctx, oid, "a", "1")) - goto out_err; + goto out_err_cleanup; if (do_rados_getxattr(io_ctx, oid, "b", "2")) - goto out_err; + goto out_err_cleanup; if (do_rados_getxattr(io_ctx, oid, "c", "3")) - goto out_err; + goto out_err_cleanup; const char *exkeys[] = { "a", "b", "c", NULL }; const char *exvals[] = { "1", "2", "3", NULL }; if (do_rados_getxattrs(io_ctx, oid, exkeys, exvals)) - goto out_err; + goto out_err_cleanup; uint64_t size; time_t mtime; @@ -296,14 +300,15 @@ static int testrados(void) r = rados_ioctx_pool_stat(io_ctx, &st); printf("rados_stat_pool = %d, %lld KB, %lld objects\n", r, (long long)st.num_kb, (long long)st.num_objects); + ret = 0; + +out_err_cleanup: /* delete a pool */ - printf("rados_delete_pool = %d\n", r); rados_ioctx_destroy(io_ctx); r = rados_pool_delete(cl, "foo"); - printf("rados_ioctx_pool_delete = %d\n", r); + printf("rados_delete_pool = %d\n", r); - ret = 0; out_err: rados_shutdown(cl); return ret;