diff options
author | Chenbo Feng <fengc@google.com> | 2016-09-28 17:03:31 -0700 |
---|---|---|
committer | Chenbo Feng <fengc@google.com> | 2016-09-29 12:39:12 -0700 |
commit | 31399ff50100414c07b42d50691954ae9c027fbd (patch) | |
tree | ceed008990e21f1c689d2d7e0e49334a21461915 /tests/iptables | |
parent | 2e363e48f8b0f6c11ad3184d9c010052605b480d (diff) | |
download | extras-31399ff50100414c07b42d50691954ae9c027fbd.tar.gz |
qtaguid replace asprintf with libbase StringPrintf
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
Diffstat (limited to 'tests/iptables')
-rw-r--r-- | tests/iptables/qtaguid/Android.mk | 2 | ||||
-rw-r--r-- | tests/iptables/qtaguid/socketTag.cpp | 22 |
2 files changed, 7 insertions, 17 deletions
diff --git a/tests/iptables/qtaguid/Android.mk b/tests/iptables/qtaguid/Android.mk index b92b662a..6328883d 100644 --- a/tests/iptables/qtaguid/Android.mk +++ b/tests/iptables/qtaguid/Android.mk @@ -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 diff --git a/tests/iptables/qtaguid/socketTag.cpp b/tests/iptables/qtaguid/socketTag.cpp index 01e35022..8505fca3 100644 --- a/tests/iptables/qtaguid/socketTag.cpp +++ b/tests/iptables/qtaguid/socketTag.cpp @@ -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; } |