From 34f00fcfa21941629a7177ed401b1358a920c6b9 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 27 Apr 2015 14:20:17 -0700 Subject: Support the full length of USB serial numbers. Two bugs: we couldn't report the serial number correctly if it was long enough, and it wasn't possible to connect to a device whose serial number was long enough to overflow a different fixed-length buffer. Bug: http://b/20317730 Change-Id: Ic9cf3c847066449ac78069bd1718184935098ac7 --- adb_client.cpp | 54 ++++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'adb_client.cpp') diff --git a/adb_client.cpp b/adb_client.cpp index 4751bff..0f64998 100644 --- a/adb_client.cpp +++ b/adb_client.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include "adb_io.h" static transport_type __adb_transport = kTransportAny; @@ -108,44 +110,40 @@ const char *adb_error(void) static int switch_socket_transport(int fd) { - char service[64]; - char tmp[5]; - int len; - - if (__adb_serial) - snprintf(service, sizeof service, "host:transport:%s", __adb_serial); - else { + std::string service; + if (__adb_serial) { + service += "host:transport:"; + service += __adb_serial; + } else { const char* transport_type = "???"; - - switch (__adb_transport) { - case kTransportUsb: - transport_type = "transport-usb"; - break; - case kTransportLocal: - transport_type = "transport-local"; - break; - case kTransportAny: - transport_type = "transport-any"; - break; - case kTransportHost: - // no switch necessary - return 0; - break; + switch (__adb_transport) { + case kTransportUsb: + transport_type = "transport-usb"; + break; + case kTransportLocal: + transport_type = "transport-local"; + break; + case kTransportAny: + transport_type = "transport-any"; + break; + case kTransportHost: + // no switch necessary + return 0; } - - snprintf(service, sizeof service, "host:%s", transport_type); + service += "host:"; + service += transport_type; } - len = strlen(service); - snprintf(tmp, sizeof tmp, "%04x", len); - if(!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service, len)) { + char tmp[5]; + snprintf(tmp, sizeof(tmp), "%04zx", service.size()); + if (!WriteFdExactly(fd, tmp, 4) || !WriteFdExactly(fd, service.c_str(), service.size())) { strcpy(__adb_error, "write failure during connection"); adb_close(fd); return -1; } D("Switch transport in progress\n"); - if(adb_status(fd)) { + if (adb_status(fd)) { adb_close(fd); D("Switch transport failed\n"); return -1; -- cgit v1.2.3