]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- fixed memory leak 262/head
authorArvin Schnell <aschnell@suse.de>
Tue, 16 Aug 2016 13:56:29 +0000 (15:56 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 16 Aug 2016 13:56:29 +0000 (15:56 +0200)
snapper/AppUtil.cc
testsuite-real/.gitignore
testsuite-real/Makefile.am
testsuite-real/ug-tests.cc [new file with mode: 0644]

index 9eb17d33ab20ca5fd663dcbb2dd3c16b0470c8ad..8d3393c663061345bca1712b90e5483cc9101b09 100644 (file)
@@ -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<gid_t> 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<gid_t> gids(&buf[0], &buf[n]);
-       sort(gids.begin(), gids.end());
+       gids.resize(n);
 
-       free(buf);
+       sort(gids.begin(), gids.end());
 
        return gids;
     }
index 74cae1b10b09c5441ea90ac098c1991e244e0fac..b333f30f97e78ab2d3ff6dc681758f9a329c2c3a 100644 (file)
@@ -17,3 +17,4 @@ xattrs2
 xattrs3
 xattrs4
 test-btrfsutils
+ug-tests
index 31d4e926fb64fb50bdd7508748b3a9f7a8dd1e4b..8148b54e4e78ef57b8d2fa295124330c97009b05 100644 (file)
@@ -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 (file)
index 0000000..f5af1b4
--- /dev/null
@@ -0,0 +1,58 @@
+
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <iostream>
+
+#include <snapper/AppUtil.h>
+
+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<gid_t> 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();
+}