From 135f4ab3dd6c14f10a7bd1b1a4f9bfcf2135acb8 Mon Sep 17 00:00:00 2001 From: Tao Wu Date: Fri, 16 Sep 2016 13:01:24 -0700 Subject: [PATCH] Fix bug: Doesn't respect ANDROID_ADB_SERVER_PORT BUG: 31549442 Test: export ANDROID_ADB_SERVER_PORT=12345;killall adb;adb devices Change-Id: If2bfaf44c6567af16cae0d4def2f11be39c2d437 Signed-off-by: Tao Wu --- adb/commandline.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/adb/commandline.cpp b/adb/commandline.cpp index 77c5f96b5..a9185a0b1 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -1549,23 +1550,32 @@ int adb_commandline(int argc, const char **argv) { // If -L, -H, or -P are specified, ignore environment variables. // Otherwise, prefer ADB_SERVER_SOCKET over ANDROID_ADB_SERVER_ADDRESS/PORT. - if (!(server_host_str || server_port_str || server_socket_str)) { - server_socket_str = server_socket_str ? server_socket_str : getenv("ADB_SERVER_SOCKET"); + if (!server_host_str && !server_port_str && !server_socket_str) { + server_socket_str = getenv("ADB_SERVER_SOCKET"); } if (!server_socket_str) { // tcp:1234 and tcp:localhost:1234 are different with -a, so don't default to localhost server_host_str = server_host_str ? server_host_str : getenv("ANDROID_ADB_SERVER_ADDRESS"); - long server_port = DEFAULT_ADB_PORT; + int server_port = DEFAULT_ADB_PORT; server_port_str = server_port_str ? server_port_str : getenv("ANDROID_ADB_SERVER_PORT"); + if (server_port_str && strlen(server_port_str) > 0) { + if (!android::base::ParseInt(server_port_str, &server_port, 1, 65535)) { + fprintf(stderr, + "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive" + " number less than 65535. Got \"%s\"\n", + server_port_str); + exit(1); + } + } int rc; char* temp; if (server_host_str) { - rc = asprintf(&temp, "tcp:%s:%ld", server_host_str, server_port); + rc = asprintf(&temp, "tcp:%s:%d", server_host_str, server_port); } else { - rc = asprintf(&temp, "tcp:%ld", server_port); + rc = asprintf(&temp, "tcp:%d", server_port); } if (rc < 0) { fatal("failed to allocate server socket specification");