From 3931b99d142d337ea6558fd09aad2e0812c04507 Mon Sep 17 00:00:00 2001 From: Øyvind Harboe Date: Wed, 29 Sep 2010 09:11:01 +0200 Subject: fileio: fileio_size() can now fail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of making the fileio API more robust. Signed-off-by: Øyvind Harboe --- src/flash/nor/tcl.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/flash/nor') 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); -- cgit v1.2.3