OSDN Git Service

qtaguid replace asprintf with libbase StringPrintf
authorChenbo Feng <fengc@google.com>
Thu, 29 Sep 2016 00:03:31 +0000 (17:03 -0700)
committerChenbo Feng <fengc@google.com>
Thu, 29 Sep 2016 19:39:12 +0000 (12:39 -0700)
use libbase function android::base::StringPrintf for string
manipulation. Avoid free the char array at the end. Changed the program
logic of building the match template to make it more concise.

Test: manually tested on angler device. build and sync this module into
device and run adb shell ./data/nativetest64/socketTag/socketTag

Change-Id: Ic93ac4e91e87337acb2e12efadeb57833c36c95e

tests/iptables/qtaguid/Android.mk
tests/iptables/qtaguid/socketTag.cpp

index b92b662..6328883 100644 (file)
@@ -20,7 +20,7 @@ include $(CLEAR_VARS)
 LOCAL_MODULE_TAGS := tests
 LOCAL_MODULE := socketTag
 LOCAL_SRC_FILES := socketTag.cpp
-LOCAL_SHARED_LIBRARIES += libcutils libutils liblog
+LOCAL_SHARED_LIBRARIES += libcutils libutils liblog libbase
 LOCAL_STATIC_LIBRARIES += libtestUtil
 LOCAL_C_INCLUDES += system/extras/tests/include
 LOCAL_CFLAGS += -fno-strict-aliasing
index 01e3502..8505fca 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <fstream>
 
+#include <android-base/stringprintf.h>
 #include <gtest/gtest.h>
 #include <utils/Log.h>
 
@@ -136,12 +137,9 @@ int SockInfo::setup(uint64_t tag) {
  * Returns: true if tag found.
  */
 bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
-    char * buff;
     int res;
-    char *match_template;
     uint64_t k_tag;
     uint32_t k_uid;
-    uint64_t full_tag;
     long dummy_count;
     pid_t dummy_pid;
 
@@ -150,19 +148,13 @@ bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
         testPrintI("qtaguid ctrl open failed!");
     }
 
+    uint64_t full_tag = acct_tag | uid;
+    std::string buff = android::base::StringPrintf(" tag=0x%" PRIx64 " (uid=%u)", full_tag, uid);
     if (addr) {
-        assert(sizeof(void*) == sizeof(long int));  // Why does %p use 0x? grrr. %lx.
-        asprintf(&match_template, "sock=%" PRIxPTR " %s", (uintptr_t)addr, "tag=0x%" PRIx64" (uid=%u)");
+          buff = android::base::StringPrintf("sock=%" PRIxPTR, (uintptr_t)addr) + buff;
     }
-    else {
-        /* Allocate for symmetry */
-        asprintf(&match_template, "%s", " tag=0x%" PRIx64 " (uid=%u)");
-    }
-
-    full_tag = acct_tag | uid;
 
-    asprintf(&buff, match_template, full_tag | uid, uid);
-    testPrintI("looking for '%s'", buff);
+    testPrintI("looking for '%s'", buff.c_str());
     std::string ctrl_data;
     std::size_t pos = std::string::npos;
     while(std::getline(fctrl, ctrl_data)) {
@@ -171,7 +163,7 @@ bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
         if (pos != std::string::npos) {
             if(!addr) {
                 testPrintI("matched data : %s", ctrl_data.c_str());
-                assert(sizeof(void*) == sizeof(long int));  // Why does %p use 0x? grrr. %lx.
+                assert(sizeof(void*) == sizeof(long int));
                 res = sscanf(ctrl_data.c_str(),
                             "sock=%" SCNxPTR " tag=0x%" SCNx64 " (uid=%" SCNu32 ") pid=%u f_count=%lu",
                             (uintptr_t *)&addr, &k_tag, &k_uid, &dummy_pid, &dummy_count );
@@ -185,8 +177,6 @@ bool SockInfo::checkTag(uint64_t acct_tag, uid_t uid) {
             break;
         }
     }
-    free(buff);
-    free(match_template);
     return pos != std::string::npos;
 }