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
diff --git a/tests/iptables/qtaguid/Android.mk b/tests/iptables/qtaguid/Android.mk
index b92b662..6328883 100644
--- a/tests/iptables/qtaguid/Android.mk
+++ b/tests/iptables/qtaguid/Android.mk
@@ -20,7 +20,7 @@
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 01e3502..8505fca 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 @@
* 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 @@
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)");
- }
- else {
- /* Allocate for symmetry */
- asprintf(&match_template, "%s", " tag=0x%" PRIx64 " (uid=%u)");
+ buff = android::base::StringPrintf("sock=%" PRIxPTR, (uintptr_t)addr) + buff;
}
- 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 @@
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 @@
break;
}
}
- free(buff);
- free(match_template);
return pos != std::string::npos;
}