diff options
author | Elliott Hughes <enh@google.com> | 2015-04-17 17:03:59 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-04-17 17:58:35 -0700 |
commit | a7090b94c181f3efe5b53d2c8367b78d99074dfe (patch) | |
tree | 6702f5455bac81bc5c42022fc4c472faf0f70a3c | |
parent | 8d2abbea76f13b76ef814ff426d88f70526f6923 (diff) | |
download | system_core-a7090b94c181f3efe5b53d2c8367b78d99074dfe.zip system_core-a7090b94c181f3efe5b53d2c8367b78d99074dfe.tar.gz system_core-a7090b94c181f3efe5b53d2c8367b78d99074dfe.tar.bz2 |
Remove yet more fixed-length buffers (and their overruns).
Bug: 20317724
Change-Id: If137fc96f5f23576ccecd388ac87afefa47337c6
-rw-r--r-- | adb/adb_main.cpp | 2 | ||||
-rw-r--r-- | adb/adb_utils.cpp | 8 | ||||
-rw-r--r-- | adb/adb_utils.h | 1 | ||||
-rw-r--r-- | adb/commandline.cpp | 96 | ||||
-rw-r--r-- | adb/services.cpp | 4 | ||||
-rw-r--r-- | adb/usb_linux_client.cpp | 6 |
6 files changed, 27 insertions, 90 deletions
diff --git a/adb/adb_main.cpp b/adb/adb_main.cpp index fb17e89..5acaf80 100644 --- a/adb/adb_main.cpp +++ b/adb/adb_main.cpp @@ -383,7 +383,7 @@ int main(int argc, char **argv) { /* If adbd runs inside the emulator this will enable adb tracing via * adb-debug qemud service in the emulator. */ adb_qemu_trace_init(); - while (1) { + while (true) { int c; int option_index = 0; static struct option opts[] = { diff --git a/adb/adb_utils.cpp b/adb/adb_utils.cpp index 5a6ac33..b515f59 100644 --- a/adb/adb_utils.cpp +++ b/adb/adb_utils.cpp @@ -16,10 +16,18 @@ #include "adb_utils.h" +#include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> +bool getcwd(std::string* s) { + char* cwd = getcwd(nullptr, 0); + if (cwd != nullptr) *s = cwd; + free(cwd); + return (cwd != nullptr); +} + bool directory_exists(const std::string& path) { struct stat sb; return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); diff --git a/adb/adb_utils.h b/adb/adb_utils.h index ec8e8b5..4b64afa 100644 --- a/adb/adb_utils.h +++ b/adb/adb_utils.h @@ -19,6 +19,7 @@ #include <string> +bool getcwd(std::string* cwd); bool directory_exists(const std::string& path); std::string escape_arg(const std::string& s); diff --git a/adb/commandline.cpp b/adb/commandline.cpp index 603ea09..c6d7de6 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -321,7 +321,7 @@ static void copy_to_file(int inFd, int outFd) { stdin_raw_init(STDIN_FILENO); } - for (;;) { + while (true) { if (inFd == STDIN_FILENO) { len = unix_read(inFd, buf, BUFSIZE); } else { @@ -554,7 +554,7 @@ static int adb_sideload_host(const char* fn) { opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); - for (;;) { + while (true) { if (!ReadFdExactly(fd, buf, 8)) { fprintf(stderr, "* failed to read command: %s\n", adb_error()); status = -1; @@ -872,81 +872,9 @@ static int restore(int argc, const char** argv) { return 0; } -#define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make" -static int top_works(const char *top) -{ - if (top != NULL && adb_is_absolute_host_path(top)) { - char path_buf[PATH_MAX]; - snprintf(path_buf, sizeof(path_buf), - "%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top); - return access(path_buf, F_OK) == 0; - } - return 0; -} - -static char *find_top_from(const char *indir, char path_buf[PATH_MAX]) -{ - strcpy(path_buf, indir); - while (1) { - if (top_works(path_buf)) { - return path_buf; - } - char *s = adb_dirstop(path_buf); - if (s != NULL) { - *s = '\0'; - } else { - path_buf[0] = '\0'; - return NULL; - } - } -} - -static char *find_top(char path_buf[PATH_MAX]) -{ - char *top = getenv("ANDROID_BUILD_TOP"); - if (top != NULL && top[0] != '\0') { - if (!top_works(top)) { - fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top); - return NULL; - } - } else { - top = getenv("TOP"); - if (top != NULL && top[0] != '\0') { - if (!top_works(top)) { - fprintf(stderr, "adb: bad TOP value \"%s\"\n", top); - return NULL; - } - } else { - top = NULL; - } - } - - if (top != NULL) { - /* The environment pointed to a top directory that works. - */ - strcpy(path_buf, top); - return path_buf; - } - - /* The environment didn't help. Walk up the tree from the CWD - * to see if we can find the top. - */ - char dir[PATH_MAX]; - top = find_top_from(getcwd(dir, sizeof(dir)), path_buf); - if (top == NULL) { - /* If the CWD isn't under a good-looking top, see if the - * executable is. - */ - get_my_path(dir, PATH_MAX); - top = find_top_from(dir, path_buf); - } - return top; -} - /* <hint> may be: * - A simple product name * e.g., "sooner" -TODO: debug? sooner-debug, sooner:debug? * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir * e.g., "out/target/product/sooner" * - An absolute path to the PRODUCT_OUT dir @@ -967,26 +895,26 @@ static std::string find_product_out_path(const char* hint) { // If there are any slashes in it, assume it's a relative path; // make it absolute. - if (adb_dirstart(hint) != NULL) { - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) == NULL) { - fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno)); + if (adb_dirstart(hint) != nullptr) { + std::string cwd; + if (!getcwd(&cwd)) { + fprintf(stderr, "adb: getcwd failed: %s\n", strerror(errno)); return ""; } - return android::base::StringPrintf("%s%s%s", cwd, OS_PATH_SEPARATOR_STR, hint); + return android::base::StringPrintf("%s%s%s", cwd.c_str(), OS_PATH_SEPARATOR_STR, hint); } // It's a string without any slashes. Try to do something with it. // // Try to find the root of the build tree, and build a PRODUCT_OUT // path from there. - char top_buf[PATH_MAX]; - const char* top = find_top(top_buf); + char* top = getenv("ANDROID_BUILD_TOP"); if (top == nullptr) { - fprintf(stderr, "adb: Couldn't find top of build tree\n"); + fprintf(stderr, "adb: ANDROID_BUILD_TOP not set!\n"); return ""; } - std::string path = top_buf; + + std::string path = top; path += OS_PATH_SEPARATOR_STR; path += "out"; path += OS_PATH_SEPARATOR_STR; @@ -998,7 +926,7 @@ static std::string find_product_out_path(const char* hint) { if (!directory_exists(path)) { fprintf(stderr, "adb: Couldn't find a product dir based on -p %s; " "\"%s\" doesn't exist\n", hint, path.c_str()); - return NULL; + return ""; } return path; } diff --git a/adb/services.cpp b/adb/services.cpp index 769d58e..fa0e73f 100644 --- a/adb/services.cpp +++ b/adb/services.cpp @@ -192,7 +192,7 @@ void reboot_service(int fd, void* arg) if (reboot_service_impl(fd, static_cast<const char*>(arg))) { // Don't return early. Give the reboot command time to take effect // to avoid messing up scripts which do "adb reboot && adb wait-for-device" - while (1) { + while (true) { pause(); } } @@ -373,7 +373,7 @@ static void subproc_waiter_service(int fd, void *cookie) pid_t pid = (pid_t) (uintptr_t) cookie; D("entered. fd=%d of pid=%d\n", fd, pid); - for (;;) { + while (true) { int status; pid_t p = waitpid(pid, &status, 0); if (p == pid) { diff --git a/adb/usb_linux_client.cpp b/adb/usb_linux_client.cpp index 8d670f8..343f20c 100644 --- a/adb/usb_linux_client.cpp +++ b/adb/usb_linux_client.cpp @@ -163,7 +163,7 @@ static void *usb_adb_open_thread(void *x) struct usb_handle *usb = (struct usb_handle *)x; int fd; - while (1) { + while (true) { // wait until the USB device needs opening adb_mutex_lock(&usb->lock); while (usb->fd != -1) @@ -347,14 +347,14 @@ static void *usb_ffs_open_thread(void *x) { struct usb_handle *usb = (struct usb_handle *)x; - while (1) { + while (true) { // wait until the USB device needs opening adb_mutex_lock(&usb->lock); while (usb->control != -1 && usb->bulk_in != -1 && usb->bulk_out != -1) adb_cond_wait(&usb->notify, &usb->lock); adb_mutex_unlock(&usb->lock); - while (1) { + while (true) { init_functionfs(usb); if (usb->control >= 0 && usb->bulk_in >= 0 && usb->bulk_out >= 0) |