Merge "Remove net_bt_stack group and replace it with bluetooth"
diff --git a/adb/Android.mk b/adb/Android.mk
index b2a0dc4..0114ca3 100644
--- a/adb/Android.mk
+++ b/adb/Android.mk
@@ -62,6 +62,7 @@
     adb_listeners_test.cpp \
     adb_utils_test.cpp \
     fdevent_test.cpp \
+    socket_spec_test.cpp \
     socket_test.cpp \
     sysdeps_test.cpp \
     sysdeps/stat_test.cpp \
diff --git a/adb/shell_service.cpp b/adb/shell_service.cpp
index 01e206a..b0b31f1 100644
--- a/adb/shell_service.cpp
+++ b/adb/shell_service.cpp
@@ -493,10 +493,10 @@
                 // We also need to close the pipes connected to the child process
                 // so that if it ignores SIGHUP and continues to write data it
                 // won't fill up the pipe and block.
-                stdinout_sfd_.clear();
-                stderr_sfd_.clear();
+                stdinout_sfd_.reset();
+                stderr_sfd_.reset();
             }
-            dead_sfd->clear();
+            dead_sfd->reset();
         }
     }
 }
diff --git a/adb/socket_spec.cpp b/adb/socket_spec.cpp
index 18e6e6d..14eb16b 100644
--- a/adb/socket_spec.cpp
+++ b/adb/socket_spec.cpp
@@ -20,6 +20,8 @@
 #include <unordered_map>
 #include <vector>
 
+#include <android-base/parseint.h>
+#include <android-base/parsenetaddress.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <cutils/sockets.h>
@@ -62,55 +64,47 @@
     { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } },
 });
 
-static bool parse_tcp_spec(const std::string& spec, std::string* hostname, int* port,
+bool parse_tcp_socket_spec(const std::string& spec, std::string* hostname, int* port,
                            std::string* error) {
-    std::vector<std::string> fragments = android::base::Split(spec, ":");
-    if (fragments.size() == 1 || fragments.size() > 3) {
-        *error = StringPrintf("invalid tcp specification: '%s'", spec.c_str());
-        return false;
-    }
-
-    if (fragments[0] != "tcp") {
+    if (!StartsWith(spec, "tcp:")) {
         *error = StringPrintf("specification is not tcp: '%s'", spec.c_str());
         return false;
     }
 
-    // strtol accepts leading whitespace.
-    const std::string& port_str = fragments.back();
-    if (port_str.empty() || port_str[0] < '0' || port_str[0] > '9') {
-        *error = StringPrintf("invalid port '%s'", port_str.c_str());
-        return false;
-    }
+    std::string hostname_value;
+    int port_value;
 
-    char* parsed_end;
-    long parsed_port = strtol(port_str.c_str(), &parsed_end, 10);
-    if (*parsed_end != '\0') {
-        *error = StringPrintf("trailing chars in port: '%s'", port_str.c_str());
-        return false;
-    }
-    if (parsed_port > 65535) {
-        *error = StringPrintf("invalid port %ld", parsed_port);
-        return false;
-    }
-
-    // tcp:123 is valid, tcp::123 isn't.
-    if (fragments.size() == 2) {
-        // Empty hostname.
-        if (hostname) {
-            *hostname = "";
-        }
-    } else {
-        if (fragments[1].empty()) {
-            *error = StringPrintf("empty host in '%s'", spec.c_str());
+    // If the spec is tcp:<port>, parse it ourselves.
+    // Otherwise, delegate to android::base::ParseNetAddress.
+    if (android::base::ParseInt(&spec[4], &port_value)) {
+        // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234'
+        // identically.
+        if (port_value < 0 || port_value > 65535) {
+            *error = StringPrintf("bad port number '%d'", port_value);
             return false;
         }
-        if (hostname) {
-            *hostname = fragments[1];
+    } else {
+        std::string addr = spec.substr(4);
+        port_value = -1;
+
+        // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening
+        //        on an address that isn't 'localhost' is unsupported.
+        if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, nullptr, error)) {
+            return false;
         }
+
+        if (port_value == -1) {
+            *error = StringPrintf("missing port in specification: '%s'", spec.c_str());
+            return false;
+        }
+    }
+
+    if (hostname) {
+        *hostname = std::move(hostname_value);
     }
 
     if (port) {
-        *port = parsed_port;
+        *port = port_value;
     }
 
     return true;
@@ -141,7 +135,7 @@
 
     std::string error;
     std::string hostname;
-    if (!parse_tcp_spec(spec, &hostname, nullptr, &error)) {
+    if (!parse_tcp_socket_spec(spec, &hostname, nullptr, &error)) {
         return false;
     }
     return tcp_host_is_local(hostname);
@@ -151,7 +145,7 @@
     if (StartsWith(spec, "tcp:")) {
         std::string hostname;
         int port;
-        if (!parse_tcp_spec(spec, &hostname, &port, error)) {
+        if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
             return -1;
         }
 
@@ -196,7 +190,7 @@
     if (StartsWith(spec, "tcp:")) {
         std::string hostname;
         int port;
-        if (!parse_tcp_spec(spec, &hostname, &port, error)) {
+        if (!parse_tcp_socket_spec(spec, &hostname, &port, error)) {
             return -1;
         }
 
diff --git a/adb/socket_spec.h b/adb/socket_spec.h
index 6302da5..6920e91 100644
--- a/adb/socket_spec.h
+++ b/adb/socket_spec.h
@@ -25,3 +25,7 @@
 int socket_spec_connect(const std::string& spec, std::string* error);
 int socket_spec_listen(const std::string& spec, std::string* error,
                        int* resolved_tcp_port = nullptr);
+
+// Exposed for testing.
+bool parse_tcp_socket_spec(const std::string& spec, std::string* hostname, int* port,
+                           std::string* error);
diff --git a/adb/socket_spec_test.cpp b/adb/socket_spec_test.cpp
new file mode 100644
index 0000000..40ce21c
--- /dev/null
+++ b/adb/socket_spec_test.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "socket_spec.h"
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+TEST(socket_spec, parse_tcp_socket_spec) {
+    std::string hostname, error;
+    int port;
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:5037", &hostname, &port, &error));
+    EXPECT_EQ("", hostname);
+    EXPECT_EQ(5037, port);
+
+    // Bad ports:
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:65536", &hostname, &port, &error));
+
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:localhost:1234", &hostname, &port, &error));
+    EXPECT_EQ("localhost", hostname);
+    EXPECT_EQ(1234, port);
+
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:localhost:65536", &hostname, &port, &error));
+
+    // IPv6:
+    EXPECT_TRUE(parse_tcp_socket_spec("tcp:[::1]:1234", &hostname, &port, &error));
+    EXPECT_EQ("::1", hostname);
+    EXPECT_EQ(1234, port);
+
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:[::1]:-1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1", &hostname, &port, &error));
+    EXPECT_FALSE(parse_tcp_socket_spec("tcp:::1:1234", &hostname, &port, &error));
+}
diff --git a/base/include/android-base/unique_fd.h b/base/include/android-base/unique_fd.h
index c323311..6cfcfcd 100644
--- a/base/include/android-base/unique_fd.h
+++ b/base/include/android-base/unique_fd.h
@@ -55,7 +55,7 @@
   unique_fd_impl() : value_(-1) {}
 
   explicit unique_fd_impl(int value) : value_(value) {}
-  ~unique_fd_impl() { clear(); }
+  ~unique_fd_impl() { reset(); }
 
   unique_fd_impl(unique_fd_impl&& other) : value_(other.release()) {}
   unique_fd_impl& operator=(unique_fd_impl&& s) {
@@ -63,17 +63,13 @@
     return *this;
   }
 
-  void reset(int new_value) {
+  void reset(int new_value = -1) {
     if (value_ != -1) {
       Closer::Close(value_);
     }
     value_ = new_value;
   }
 
-  void clear() {
-    reset(-1);
-  }
-
   int get() const { return value_; }
   operator int() const { return get(); }
 
diff --git a/include/cutils/trace.h b/include/cutils/trace.h
index c9790ad..dc3833f 100644
--- a/include/cutils/trace.h
+++ b/include/cutils/trace.h
@@ -188,8 +188,8 @@
 static inline void atrace_end(uint64_t tag)
 {
     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
-        char c = 'E';
-        write(atrace_marker_fd, &c, 1);
+        void atrace_end_body();
+        atrace_end_body();
     }
 }
 
diff --git a/include/log/log.h b/include/log/log.h
index 045feca..24c77fc 100644
--- a/include/log/log.h
+++ b/include/log/log.h
@@ -41,6 +41,15 @@
 extern "C" {
 #endif
 
+// This file uses ", ## __VA_ARGS__" zero-argument token pasting to
+// work around issues with debug-only syntax errors in assertions
+// that are missing format strings.  See commit
+// 19299904343daf191267564fe32e6cd5c165cd42
+#if defined(__clang__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+#endif
+
 // ---------------------------------------------------------------------
 
 /*
@@ -692,6 +701,10 @@
 #endif
     ;
 
+#if defined(__clang__)
+#pragma clang diagnostic pop
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libcutils/trace-dev.c b/libcutils/trace-dev.c
index 778e4f0..099ab45 100644
--- a/libcutils/trace-dev.c
+++ b/libcutils/trace-dev.c
@@ -196,6 +196,12 @@
     write(atrace_marker_fd, buf, len);
 }
 
+void atrace_end_body()
+{
+    char c = 'E';
+    write(atrace_marker_fd, &c, 1);
+}
+
 #define WRITE_MSG(format_begin, format_end, pid, name, value) { \
     char buf[ATRACE_MESSAGE_LENGTH]; \
     int len = snprintf(buf, sizeof(buf), format_begin "%s" format_end, pid, \
diff --git a/libcutils/trace-host.c b/libcutils/trace-host.c
index 6478e3e..05842cd 100644
--- a/libcutils/trace-host.c
+++ b/libcutils/trace-host.c
@@ -29,6 +29,7 @@
 void atrace_update_tags() { }
 void atrace_setup() { }
 void atrace_begin_body(const char* name __unused) { }
+void atrace_end_body() { }
 void atrace_async_begin_body(const char* name __unused, int32_t cookie __unused) { }
 void atrace_async_end_body(const char* name __unused, int32_t cookie __unused) { }
 void atrace_int_body(const char* name __unused, int32_t value __unused) { }
diff --git a/libusbhost/include/usbhost/usbhost.h b/libusbhost/include/usbhost/usbhost.h
index 88b5b44..84594c8 100644
--- a/libusbhost/include/usbhost/usbhost.h
+++ b/libusbhost/include/usbhost/usbhost.h
@@ -216,7 +216,7 @@
 int usb_device_bulk_transfer(struct usb_device *device,
                             int endpoint,
                             void* buffer,
-                            int length,
+                            unsigned int length,
                             unsigned int timeout);
 
 /** Reset USB bus for the device */
diff --git a/libusbhost/usbhost.c b/libusbhost/usbhost.c
index 299fdc4..68aca17 100644
--- a/libusbhost/usbhost.c
+++ b/libusbhost/usbhost.c
@@ -600,7 +600,7 @@
 int usb_device_bulk_transfer(struct usb_device *device,
                             int endpoint,
                             void* buffer,
-                            int length,
+                            unsigned int length,
                             unsigned int timeout)
 {
     struct usbdevfs_bulktransfer  ctrl;