diff options
author | Elliott Hughes <enh@google.com> | 2015-04-21 19:39:52 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-04-21 19:43:22 -0700 |
commit | dc3b459ff9f0ff71d404ba7198083e532a0dd894 (patch) | |
tree | 3e64efa2fefb22f72fbeb303a672353fb219de16 | |
parent | 9a0cea92c8b74114b70ac3339b2b611633f6a31a (diff) | |
download | system_core-dc3b459ff9f0ff71d404ba7198083e532a0dd894.zip system_core-dc3b459ff9f0ff71d404ba7198083e532a0dd894.tar.gz system_core-dc3b459ff9f0ff71d404ba7198083e532a0dd894.tar.bz2 |
Add missing null checks after allocations.
Bug: http://b/20317729
Change-Id: I62bb761d48ee59a1f4ddd0cdd0632432305ca2ca
-rw-r--r-- | adb/commandline.cpp | 6 | ||||
-rw-r--r-- | adb/services.cpp | 4 | ||||
-rw-r--r-- | adb/transport.cpp | 17 | ||||
-rw-r--r-- | adb/usb_linux.cpp | 4 | ||||
-rw-r--r-- | adb/usb_linux_client.cpp | 4 | ||||
-rw-r--r-- | adb/usb_osx.cpp | 1 |
6 files changed, 26 insertions, 10 deletions
diff --git a/adb/commandline.cpp b/adb/commandline.cpp index 58e1ade..c503558 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -312,6 +312,7 @@ static void read_status_line(int fd, char* buf, size_t count) static void copy_to_file(int inFd, int outFd) { const size_t BUFSIZE = 32 * 1024; char* buf = (char*) malloc(BUFSIZE); + if (buf == nullptr) fatal("couldn't allocate buffer for copy_to_file"); int len; long total = 0; @@ -419,6 +420,11 @@ static int interactive_shell() { fdi = 0; //dup(0); int* fds = reinterpret_cast<int*>(malloc(sizeof(int) * 2)); + if (fds == nullptr) { + fprintf(stderr, "couldn't allocate fds array: %s\n", strerror(errno)); + return 1; + } + fds[0] = fd; fds[1] = fdi; diff --git a/adb/services.cpp b/adb/services.cpp index ff13722..e6c84a4 100644 --- a/adb/services.cpp +++ b/adb/services.cpp @@ -689,6 +689,10 @@ asocket* host_service_to_socket(const char* name, const char *serial) return create_device_tracker(); } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) { auto sinfo = reinterpret_cast<state_info*>(malloc(sizeof(state_info))); + if (sinfo == nullptr) { + fprintf(stderr, "couldn't allocate state_info: %s", strerror(errno)); + return NULL; + } if (serial) sinfo->serial = strdup(serial); diff --git a/adb/transport.cpp b/adb/transport.cpp index 37f9d7b..d395a80 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -493,10 +493,8 @@ device_tracker_ready( asocket* socket ) asocket* create_device_tracker(void) { - device_tracker* tracker = reinterpret_cast<device_tracker*>( - calloc(1, sizeof(*tracker))); - - if(tracker == 0) fatal("cannot allocate device tracker"); + device_tracker* tracker = reinterpret_cast<device_tracker*>(calloc(1, sizeof(*tracker))); + if (tracker == nullptr) fatal("cannot allocate device tracker"); D( "device tracker %p created\n", tracker); @@ -1002,8 +1000,11 @@ void close_usb_devices() int register_socket_transport(int s, const char *serial, int port, int local) { - atransport *t = reinterpret_cast<atransport*>( - calloc(1, sizeof(atransport))); + atransport *t = reinterpret_cast<atransport*>(calloc(1, sizeof(atransport))); + if (t == nullptr) { + return -1; + } + atransport *n; char buff[32]; @@ -1102,8 +1103,8 @@ void unregister_all_tcp_transports() void register_usb_transport(usb_handle *usb, const char *serial, const char *devpath, unsigned writeable) { - atransport *t = reinterpret_cast<atransport*>( - calloc(1, sizeof(atransport))); + atransport *t = reinterpret_cast<atransport*>(calloc(1, sizeof(atransport))); + if (t == nullptr) fatal("cannot allocate USB atransport"); D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, serial ? serial : ""); init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM)); diff --git a/adb/usb_linux.cpp b/adb/usb_linux.cpp index 6fd2b40..9f23511 100644 --- a/adb/usb_linux.cpp +++ b/adb/usb_linux.cpp @@ -598,8 +598,8 @@ static void register_device(const char *dev_name, const char *devpath, D("[ usb located new device %s (%d/%d/%d) ]\n", dev_name, ep_in, ep_out, interface); - usb_handle* usb = reinterpret_cast<usb_handle*>( - calloc(1, sizeof(usb_handle))); + usb_handle* usb = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); + if (usb == nullptr) fatal("couldn't allocate usb_handle"); strcpy(usb->fname, dev_name); usb->ep_in = ep_in; usb->ep_out = ep_out; diff --git a/adb/usb_linux_client.cpp b/adb/usb_linux_client.cpp index 6d21104..18289e2 100644 --- a/adb/usb_linux_client.cpp +++ b/adb/usb_linux_client.cpp @@ -241,6 +241,8 @@ static void usb_adb_kick(usb_handle *h) static void usb_adb_init() { usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); + if (h == nullptr) fatal("couldn't allocate usb_handle"); + h->write = usb_adb_write; h->read = usb_adb_read; h->kick = usb_adb_kick; @@ -468,6 +470,8 @@ static void usb_ffs_init() D("[ usb_init - using FunctionFS ]\n"); usb_handle* h = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); + if (h == nullptr) fatal("couldn't allocate usb_handle"); + h->write = usb_ffs_write; h->read = usb_ffs_read; h->kick = usb_ffs_kick; diff --git a/adb/usb_osx.cpp b/adb/usb_osx.cpp index 303ae45..a795ce3 100644 --- a/adb/usb_osx.cpp +++ b/adb/usb_osx.cpp @@ -336,6 +336,7 @@ CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 produc goto err_bad_adb_interface; handle = reinterpret_cast<usb_handle*>(calloc(1, sizeof(usb_handle))); + if (handle == nullptr) goto err_bad_adb_interface; //* Iterate over the endpoints for this interface and find the first //* bulk in/out pipes available. These will be our read/write pipes. |