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/target | |
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/target')
-rw-r--r-- | src/target/etm.c | 10 | ||||
-rw-r--r-- | src/target/image.c | 25 | ||||
-rw-r--r-- | src/target/target.c | 8 |
3 files changed, 37 insertions, 6 deletions
diff --git a/src/target/etm.c b/src/target/etm.c index c71c5d19..9f7bc83d 100644 --- a/src/target/etm.c +++ b/src/target/etm.c @@ -1897,7 +1897,15 @@ COMMAND_HANDLER(handle_etm_load_command) return ERROR_FAIL; } - if (fileio_size(&file) % 4) + int filesize; + int retval = fileio_size(&file, &filesize); + if (retval != ERROR_OK) + { + fileio_close(&file); + return retval; + } + + if (filesize % 4) { command_print(CMD_CTX, "size isn't a multiple of 4, no valid trace data"); fileio_close(&file); diff --git a/src/target/image.c b/src/target/image.c index e77e1901..b0d957f8 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -158,7 +158,13 @@ static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine, /* we can't determine the number of sections that we'll have to create ahead of time, * so we locally hold them until parsing is finished */ - ihex->buffer = malloc(fileio_size(fileio) >> 1); + int filesize; + int retval; + retval = fileio_size(fileio, &filesize); + if (retval != ERROR_OK) + return retval; + + ihex->buffer = malloc(filesize >> 1); cooked_bytes = 0x0; image->num_sections = 0; section[image->num_sections].private = &ihex->buffer[cooked_bytes]; @@ -537,7 +543,13 @@ static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine, /* we can't determine the number of sections that we'll have to create ahead of time, * so we locally hold them until parsing is finished */ - mot->buffer = malloc(fileio_size(fileio) >> 1); + int retval; + int filesize; + retval = fileio_size(fileio, &filesize); + if (retval != ERROR_OK) + return retval; + + mot->buffer = malloc(filesize >> 1); cooked_bytes = 0x0; image->num_sections = 0; section[image->num_sections].private = &mot->buffer[cooked_bytes]; @@ -743,11 +755,18 @@ int image_open(struct image *image, const char *url, const char *type_string) { return retval; } + int filesize; + retval = fileio_size(&image_binary->fileio, &filesize); + if (retval != ERROR_OK) + { + fileio_close(&image_binary->fileio); + return retval; + } image->num_sections = 1; image->sections = malloc(sizeof(struct imagesection)); image->sections[0].base_address = 0x0; - image->sections[0].size = fileio_size(&image_binary->fileio); + image->sections[0].size = filesize; image->sections[0].flags = 0; } else if (image->type == IMAGE_IHEX) diff --git a/src/target/target.c b/src/target/target.c index fcdcc363..82cbbff5 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -2648,9 +2648,13 @@ COMMAND_HANDLER(handle_dump_image_command) if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) { + int filesize; + retval = fileio_size(&fileio, &filesize); + if (retval != ERROR_OK) + return retval; command_print(CMD_CTX, - "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio_size(&fileio), - duration_elapsed(&bench), duration_kbps(&bench, fileio_size(&fileio))); + "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize, + duration_elapsed(&bench), duration_kbps(&bench, filesize)); } return retval; |