diff options
author | Hung-Te Lin <hungte@chromium.org> | 2018-10-04 14:11:42 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-10-13 11:42:12 -0700 |
commit | 050f66f11e1879594d0705974daf1075863e1d21 (patch) | |
tree | d66e5798c16e13e7da5b2a02a5df81005561fa02 | |
parent | 00d4be66721b24903f977e770148179035254e19 (diff) | |
download | vboot-050f66f11e1879594d0705974daf1075863e1d21.tar.gz |
futility: updater: Add ASPRINTF macro
When calling `asprintf`, if the return value is negative value then the
strp parameter is not allocated. Updater will need to call asprintf very
often in future, and we should abort immediately if asprintf can't
allocate buffer, since that implies either we are running out of memory,
or the system has gone very wrong.
Instead of writing if (asprintf(...) < 0) { ERROR(); return...}
everywhere, it seems easier to just add a macro and abort as exit(1).
BUG=chromium:875551
TEST=make futil; tests/futility/run_test_scripts.sh $(pwd)/build/futility
BRANCH=None
Change-Id: I8ea5f6c22dcc8225bc53fbd54b4b41a928f84910
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1260803
Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r-- | futility/updater.c | 21 | ||||
-rw-r--r-- | futility/updater.h | 2 | ||||
-rw-r--r-- | futility/updater_quirks.c | 25 |
3 files changed, 18 insertions, 30 deletions
diff --git a/futility/updater.c b/futility/updater.c index e1ae0769..e52f27f7 100644 --- a/futility/updater.c +++ b/futility/updater.c @@ -283,15 +283,9 @@ static int host_flashrom(enum flashrom_ops op, const char *image_path, } /* TODO(hungte) In future we should link with flashrom directly. */ - r = asprintf(&command, "flashrom %s %s -p %s %s %s %s %s", op_cmd, - image_path, programmer, dash_i, section_name, ignore_lock, - postfix); - - if (r == -1) { - /* `command` will be not available. */ - ERROR("Cannot allocate memory for command to execute."); - return -1; - } + ASPRINTF(&command, "flashrom %s %s -p %s %s %s %s %s", op_cmd, + image_path, programmer, dash_i, section_name, ignore_lock, + postfix); if (verbose) printf("Executing: %s\n", command); @@ -1244,12 +1238,9 @@ static int cbfs_file_exists(const char *image_file, char *cmd; int r; - if (asprintf(&cmd, - "cbfstool '%s' print -r %s 2>/dev/null | grep -q '^%s '", - image_file, section_name, cbfs_entry_name) < 0) { - ERROR("Failed to allocate buffer."); - return 0; - } + ASPRINTF(&cmd, + "cbfstool '%s' print -r %s 2>/dev/null | grep -q '^%s '", + image_file, section_name, cbfs_entry_name); r = system(cmd); free(cmd); return !r; diff --git a/futility/updater.h b/futility/updater.h index 3993f073..b315f2ed 100644 --- a/futility/updater.h +++ b/futility/updater.h @@ -17,6 +17,8 @@ extern int debugging_enabled; "DEBUG: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__); } while (0) #define ERROR(format, ...) fprintf(stderr, \ "ERROR: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__) +#define ASPRINTF(strp, ...) do { if (asprintf(strp, __VA_ARGS__) >= 0) break; \ + ERROR("Failed to allocate memory, abort."); exit(1); } while (0) /* FMAP section names. */ static const char * const FMAP_RO_FRID = "RO_FRID", diff --git a/futility/updater_quirks.c b/futility/updater_quirks.c index f3989e78..f625a74c 100644 --- a/futility/updater_quirks.c +++ b/futility/updater_quirks.c @@ -237,12 +237,10 @@ static const char *extract_cbfs_file(struct updater_config *cfg, const char *output = create_temp_file(cfg); char *command, *result; - if (asprintf(&command, "cbfstool \"%s\" extract -r %s -n \"%s\" " - "-f \"%s\" 2>&1", image_file, cbfs_region, - cbfs_name, output) < 0) { - ERROR("Failed to allocate internal buffer."); - return NULL; - } + ASPRINTF(&command, "cbfstool \"%s\" extract -r %s -n \"%s\" " + "-f \"%s\" 2>&1", image_file, cbfs_region, + cbfs_name, output); + result = host_shell(command); free(command); @@ -285,15 +283,12 @@ static int quirk_eve_smm_store(struct updater_config *cfg) return -1; /* crosreview.com/1165109: The offset is fixed at 0x1bf000. */ - if (asprintf(&command, - "cbfstool \"%s\" remove -r %s -n \"%s\" 2>/dev/null; " - "cbfstool \"%s\" add -r %s -n \"%s\" -f \"%s\" " - " -t raw -b 0x1bf000", temp_image, FMAP_RW_LEGACY, - smm_store_name, temp_image, FMAP_RW_LEGACY, - smm_store_name, old_store) < 0) { - ERROR("Failed to allocate internal buffer."); - return -1; - } + ASPRINTF(&command, + "cbfstool \"%s\" remove -r %s -n \"%s\" 2>/dev/null; " + "cbfstool \"%s\" add -r %s -n \"%s\" -f \"%s\" " + " -t raw -b 0x1bf000", temp_image, FMAP_RW_LEGACY, + smm_store_name, temp_image, FMAP_RW_LEGACY, + smm_store_name, old_store); host_shell(command); free(command); |