diff options
author | Lorenz Meier <lm@inf.ethz.ch> | 2014-07-10 07:22:35 +0200 |
---|---|---|
committer | Lorenz Meier <lm@inf.ethz.ch> | 2014-07-10 07:22:35 +0200 |
commit | 58cae259e40a3dcab132f42348d57767fca600a1 (patch) | |
tree | 9fa71f7d9e115f1c1277237ce0e95515fe7e87bf | |
parent | 8f2340acaf37288d2507a94611f2994fd1a89756 (diff) | |
parent | 12da0efbb24ad135dbacb4b8f90199b3b38cb40d (diff) | |
download | px4-firmware-58cae259e40a3dcab132f42348d57767fca600a1.tar.gz px4-firmware-58cae259e40a3dcab132f42348d57767fca600a1.tar.bz2 px4-firmware-58cae259e40a3dcab132f42348d57767fca600a1.zip |
Merge pull request #1158 from jean-m-cyr/master
Prevent stack overflow when flashing px4io
-rw-r--r-- | src/drivers/px4io/px4io_uploader.cpp | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/src/drivers/px4io/px4io_uploader.cpp b/src/drivers/px4io/px4io_uploader.cpp index 7b6361a7c..d134c0246 100644 --- a/src/drivers/px4io/px4io_uploader.cpp +++ b/src/drivers/px4io/px4io_uploader.cpp @@ -39,6 +39,7 @@ #include <nuttx/config.h> #include <sys/types.h> +#include <stdlib.h> #include <stdint.h> #include <stdbool.h> #include <assert.h> @@ -413,11 +414,17 @@ static int read_with_retry(int fd, void *buf, size_t n) int PX4IO_Uploader::program(size_t fw_size) { - uint8_t file_buf[PROG_MULTI_MAX]; + uint8_t *file_buf; ssize_t count; int ret; size_t sent = 0; + file_buf = (uint8_t *)malloc(PROG_MULTI_MAX); + if (!file_buf) { + log("Can't allocate program buffer"); + return -ENOMEM; + } + log("programming %u bytes...", (unsigned)fw_size); ret = lseek(_fw_fd, 0, SEEK_SET); @@ -425,8 +432,8 @@ PX4IO_Uploader::program(size_t fw_size) while (sent < fw_size) { /* get more bytes to program */ size_t n = fw_size - sent; - if (n > sizeof(file_buf)) { - n = sizeof(file_buf); + if (n > PROG_MULTI_MAX) { + n = PROG_MULTI_MAX; } count = read_with_retry(_fw_fd, file_buf, n); @@ -438,8 +445,10 @@ PX4IO_Uploader::program(size_t fw_size) (int)errno); } - if (count == 0) + if (count == 0) { + free(file_buf); return OK; + } sent += count; @@ -455,9 +464,12 @@ PX4IO_Uploader::program(size_t fw_size) ret = get_sync(1000); - if (ret != OK) + if (ret != OK) { + free(file_buf); return ret; + } } + free(file_buf); return OK; } |