A few c++isms more
authorRobert Swiecki <robert@swiecki.net>
Wed, 23 May 2018 16:19:17 +0000 (18:19 +0200)
committerRobert Swiecki <robert@swiecki.net>
Wed, 23 May 2018 16:19:17 +0000 (18:19 +0200)
caps.cc
mnt.cc
subproc.cc
util.cc
util.h

diff --git a/caps.cc b/caps.cc
index 37d2fd1..83226ae 100644 (file)
--- a/caps.cc
+++ b/caps.cc
@@ -167,24 +167,23 @@ static void setInheritable(cap_user_data_t cap_data, unsigned int cap) {
 #define PR_CAP_AMBIENT_CLEAR_ALL 4
 #endif /* !defined(PR_CAP_AMBIENT) */
 static bool initNsKeepCaps(cap_user_data_t cap_data) {
-       char dbgmsg[4096];
 
        /* Copy all permitted caps to the inheritable set */
-       dbgmsg[0] = '\0';
+       std::string dbgmsg1;
        for (const auto& i : capNames) {
                if (getPermitted(cap_data, i.val)) {
-                       util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
+                       util::StrAppend(&dbgmsg1, " %s", i.name);
                        setInheritable(cap_data, i.val);
                }
        }
-       LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg);
+       LOG_D("Adding the following capabilities to the inheritable set:%s", dbgmsg1.c_str());
 
        if (!setCaps(cap_data)) {
                return false;
        }
 
        /* Make sure the inheritable set is preserved across execve via the ambient set */
-       dbgmsg[0] = '\0';
+       std::string dbgmsg2;
        for (const auto& i : capNames) {
                if (!getPermitted(cap_data, i.val)) {
                        continue;
@@ -193,10 +192,10 @@ static bool initNsKeepCaps(cap_user_data_t cap_data) {
                    -1) {
                        PLOG_W("prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, %s)", i.name);
                } else {
-                       util::sSnPrintf(dbgmsg, sizeof(dbgmsg), " %s", i.name);
+                       util::StrAppend(&dbgmsg2, " %s", i.name);
                }
        }
-       LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg);
+       LOG_D("Added the following capabilities to the ambient set:%s", dbgmsg2.c_str());
 
        return true;
 }
diff --git a/mnt.cc b/mnt.cc
index f51a348..a58b93c 100644 (file)
--- a/mnt.cc
+++ b/mnt.cc
@@ -99,9 +99,7 @@ static const std::string flagsToStr(uintptr_t flags) {
        if (((flags & ~(knownFlagMask)) == 0) && !res.empty()) {
                res.pop_back();
        } else {
-               char flagstr[32];
-               snprintf(flagstr, sizeof(flagstr), "%#tx", flags & ~(knownFlagMask));
-               res.append(flagstr);
+               util::StrAppend(&res, "%#tx", flags & ~(knownFlagMask));
        }
 
        return res;
index 04693a5..2c7b1c1 100644 (file)
@@ -100,9 +100,7 @@ static const std::string cloneFlagsToStr(uintptr_t flags) {
        }
 
        if (flags & ~(knownFlagMask)) {
-               char flagstr[32];
-               snprintf(flagstr, sizeof(flagstr), "%#tx|", flags & ~(knownFlagMask));
-               res.append(flagstr);
+               util::StrAppend(&res, "%#tx|", flags & ~(knownFlagMask));
        }
        res.append(util::sigName(flags & CSIGNAL).c_str());
        return res;
diff --git a/util.cc b/util.cc
index d0e4f8e..a0c4915 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -171,6 +171,25 @@ int sSnPrintf(char* str, size_t size, const char* format, ...) {
        return snprintf(str, size, "%s%s", buf1, buf2);
 }
 
+std::string* StrAppend(std::string* str, const char* format, ...) {
+       char* strp;
+
+       va_list args;
+       va_start(args, format);
+       int ret = vasprintf(&strp, format, args);
+       va_end(args);
+
+       if (ret == -1) {
+               PLOG_E("Memory allocation failed during asprintf()");
+               str->append(" [ERROR: mem_allocation_failed] ");
+               return str;
+       }
+
+       str->append(strp, ret);
+       free(strp);
+       return str;
+}
+
 bool isANumber(const char* s) {
        for (size_t i = 0; s[i]; s++) {
                if (!isdigit(s[i]) && s[i] != 'x') {
diff --git a/util.h b/util.h
index 47ccfdf..26f275c 100644 (file)
--- a/util.h
+++ b/util.h
@@ -38,7 +38,10 @@ ssize_t readFromFile(const char* fname, void* buf, size_t len);
 ssize_t writeToFd(int fd, const void* buf, size_t len);
 bool writeBufToFile(const char* filename, const void* buf, size_t len, int open_flags);
 bool createDirRecursively(const char* dir);
-int sSnPrintf(char* str, size_t size, const char* format, ...);
+int sSnPrintf(char* str, size_t size, const char* format, ...)
+    __attribute__((format(printf, 3, 4)));
+std::string* StrAppend(std::string* str, const char* format, ...)
+    __attribute__((format(printf, 2, 3)));
 bool isANumber(const char* s);
 uint64_t rnd64(void);
 const std::string sigName(int signo);