Minor clean up to minadbd_services.cpp

Switch some function signatures to std::string to avoid memory leak.

Bug: 30039381
Test: sideload a package on angler
Change-Id: Iae1e75871a782d6e5d6dde5dcf3f18469eb63f7d
This commit is contained in:
Tianjie Xu 2017-04-19 14:00:52 -07:00
parent ba3f078f6f
commit 6af51a0eef

View file

@ -21,6 +21,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <string>
#include <thread> #include <thread>
#include "adb.h" #include "adb.h"
@ -28,33 +29,30 @@
#include "fuse_adb_provider.h" #include "fuse_adb_provider.h"
#include "sysdeps.h" #include "sysdeps.h"
static void sideload_host_service(int sfd, void* data) { static void sideload_host_service(int sfd, const std::string& args) {
char* args = reinterpret_cast<char*>(data);
int file_size; int file_size;
int block_size; int block_size;
if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) { if (sscanf(args.c_str(), "%d:%d", &file_size, &block_size) != 2) {
printf("bad sideload-host arguments: %s\n", args); printf("bad sideload-host arguments: %s\n", args.c_str());
exit(1); exit(1);
} }
free(args);
printf("sideload-host file size %d block size %d\n", file_size, block_size); printf("sideload-host file size %d block size %d\n", file_size, block_size);
int result = run_adb_fuse(sfd, file_size, block_size); int result = run_adb_fuse(sfd, file_size, block_size);
printf("sideload_host finished\n"); printf("sideload_host finished\n");
sleep(1);
exit(result == 0 ? 0 : 1); exit(result == 0 ? 0 : 1);
} }
static int create_service_thread(void (*func)(int, void *), void *cookie) { static int create_service_thread(void (*func)(int, const std::string&), const std::string& args) {
int s[2]; int s[2];
if (adb_socketpair(s)) { if (adb_socketpair(s)) {
printf("cannot create service socket pair\n"); printf("cannot create service socket pair\n");
return -1; return -1;
} }
std::thread([s, func, cookie]() { func(s[1], cookie); }).detach(); std::thread([s, func, args]() { func(s[1], args); }).detach();
VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1]; VLOG(SERVICES) << "service thread started, " << s[0] << ":" << s[1];
return s[0]; return s[0];
@ -69,7 +67,7 @@ int service_to_fd(const char* name, const atransport* transport) {
// sideload-host). // sideload-host).
exit(3); exit(3);
} else if (!strncmp(name, "sideload-host:", 14)) { } else if (!strncmp(name, "sideload-host:", 14)) {
char* arg = strdup(name + 14); std::string arg(name + 14);
ret = create_service_thread(sideload_host_service, arg); ret = create_service_thread(sideload_host_service, arg);
} }
if (ret >= 0) { if (ret >= 0) {