From 5ac19c96570c1b6df2c479e29d38034abc16b466 Mon Sep 17 00:00:00 2001 From: Arvin Schnell Date: Tue, 16 Aug 2016 15:56:29 +0200 Subject: [PATCH] - fixed memory leak --- snapper/AppUtil.cc | 15 ++++------ testsuite-real/.gitignore | 1 + testsuite-real/Makefile.am | 4 ++- testsuite-real/ug-tests.cc | 58 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 testsuite-real/ug-tests.cc diff --git a/snapper/AppUtil.cc b/snapper/AppUtil.cc index 9eb17d33..8d3393c6 100644 --- a/snapper/AppUtil.cc +++ b/snapper/AppUtil.cc @@ -1,5 +1,6 @@ /* * Copyright (c) [2004-2015] Novell, Inc. + * Copyright (c) 2016 SUSE LLC * * All Rights Reserved. * @@ -349,18 +350,14 @@ namespace snapper getgrouplist(const char* username, gid_t gid) { int n = 16; - gid_t* buf = (gid_t*) malloc(sizeof(gid_t) * n); + vector gids(n); - if (::getgrouplist(username, gid, buf, &n) == -1) - { - buf = (gid_t*) realloc(buf, sizeof(gid_t) * n); - ::getgrouplist(username, gid, buf, &n); - } + while (::getgrouplist(username, gid, &gids[0], &n) == -1) + gids.resize(n); - vector gids(&buf[0], &buf[n]); - sort(gids.begin(), gids.end()); + gids.resize(n); - free(buf); + sort(gids.begin(), gids.end()); return gids; } diff --git a/testsuite-real/.gitignore b/testsuite-real/.gitignore index 74cae1b1..b333f30f 100644 --- a/testsuite-real/.gitignore +++ b/testsuite-real/.gitignore @@ -17,3 +17,4 @@ xattrs2 xattrs3 xattrs4 test-btrfsutils +ug-tests diff --git a/testsuite-real/Makefile.am b/testsuite-real/Makefile.am index 31d4e926..8148b54e 100644 --- a/testsuite-real/Makefile.am +++ b/testsuite-real/Makefile.am @@ -16,7 +16,7 @@ noinst_SCRIPTS = run-all noinst_PROGRAMS = simple1 permissions1 permissions2 permissions3 owner1 owner2 \ owner3 directory1 missing-directory1 error1 error2 error4 \ - $(TMP_XATST) test-btrfsutils + $(TMP_XATST) test-btrfsutils ug-tests simple1_SOURCES = simple1.cc common.h common.cc @@ -43,5 +43,7 @@ xattrs4_SOURCES = xattrs4.cc xattrs_utils.cc xattrs_utils.h common.h common.cc test_btrfsutils_SOURCES = test-btrfsutils.cc +ug_tests_SOURCES = ug-tests.cc + EXTRA_DIST = $(noinst_SCRIPTS) diff --git a/testsuite-real/ug-tests.cc b/testsuite-real/ug-tests.cc new file mode 100644 index 00000000..f5af1b43 --- /dev/null +++ b/testsuite-real/ug-tests.cc @@ -0,0 +1,58 @@ + +#include +#include + +#include + +#include + +using namespace std; +using namespace snapper; + + +void +test1() +{ + uid_t uid = getuid(); + cout << "uid:" << uid << endl; + + string username; + gid_t gid; + if (!get_uid_username_gid(uid, username, gid)) + cerr << "failed to get username and gid" << endl; + cout << "username:" << username << endl; + cout << "gid:" << gid << endl; + + vector gids = getgrouplist(username.c_str(), gid); + cout << "gids:"; + for (gid_t gid : gids) + cout << gid << " "; + cout << endl; + + cout << endl; +} + + +void +test2() +{ + uid_t uid; + if (!get_user_uid("root", uid)) + cerr << "failed to get uid" << endl; + cout << "uid:" << uid << endl; + + gid_t gid; + if (!get_group_gid("audio", gid)) + cerr << "failed to get gid" << endl; + cout << "gid:" << gid << endl; + + cout << endl; +} + + +int +main() +{ + test1(); + test2(); +} -- 2.47.3