diff options
author | Øyvind Harboe <oyvind.harboe@zylin.com> | 2010-09-29 09:11:01 +0200 |
---|---|---|
committer | Øyvind Harboe <oyvind.harboe@zylin.com> | 2010-09-29 18:56:07 +0200 |
commit | 3931b99d142d337ea6558fd09aad2e0812c04507 (patch) | |
tree | 20ca8392b75964e038b941b607dcce8162513b2d /src/flash/nor | |
parent | 3a693ef526575633cc350a69aa1a5d1f08e64c46 (diff) | |
download | openocd_libswd-3931b99d142d337ea6558fd09aad2e0812c04507.tar.gz openocd_libswd-3931b99d142d337ea6558fd09aad2e0812c04507.tar.bz2 openocd_libswd-3931b99d142d337ea6558fd09aad2e0812c04507.tar.xz openocd_libswd-3931b99d142d337ea6558fd09aad2e0812c04507.zip |
fileio: fileio_size() can now fail
Part of making the fileio API more robust.
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
Diffstat (limited to 'src/flash/nor')
-rw-r--r-- | src/flash/nor/tcl.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/flash/nor/tcl.c b/src/flash/nor/tcl.c index f36ab7da..142f31f7 100644 --- a/src/flash/nor/tcl.c +++ b/src/flash/nor/tcl.c @@ -604,9 +604,23 @@ COMMAND_HANDLER(handle_flash_write_bank_command) return ERROR_OK; } - buffer = malloc(fileio_size(&fileio)); + int filesize; + retval = fileio_size(&fileio, &filesize); + if (retval != ERROR_OK) + { + fileio_close(&fileio); + return retval; + } + + buffer = malloc(filesize); + if (buffer == NULL) + { + fileio_close(&fileio); + LOG_ERROR("Out of memory"); + return ERROR_FAIL; + } size_t buf_cnt; - if (fileio_read(&fileio, fileio_size(&fileio), buffer, &buf_cnt) != ERROR_OK) + if (fileio_read(&fileio, filesize, buffer, &buf_cnt) != ERROR_OK) { free(buffer); fileio_close(&fileio); @@ -622,8 +636,8 @@ COMMAND_HANDLER(handle_flash_write_bank_command) { command_print(CMD_CTX, "wrote %ld bytes from file %s to flash bank %u" " at offset 0x%8.8" PRIx32 " in %fs (%0.3f KiB/s)", - (long)fileio_size(&fileio), CMD_ARGV[1], p->bank_number, offset, - duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio))); + (long)filesize, CMD_ARGV[1], p->bank_number, offset, + duration_elapsed(&bench), duration_kbps(&bench, filesize)); } fileio_close(&fileio); |