From patchwork Fri Mar 2 10:30:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 144203 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from chlorine.canonical.com (chlorine.canonical.com [91.189.94.204]) by ozlabs.org (Postfix) with ESMTP id DE5AF1007D6 for ; Fri, 2 Mar 2012 21:30:53 +1100 (EST) Received: from localhost ([127.0.0.1] helo=chlorine.canonical.com) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1S3PlE-0003IB-B6 for incoming@patchwork.ozlabs.org; Fri, 02 Mar 2012 10:30:52 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by chlorine.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1S3PlC-0003Hh-0q for fwts-devel@lists.ubuntu.com; Fri, 02 Mar 2012 10:30:50 +0000 Received: from cpc19-craw6-2-0-cust5.croy.cable.virginmedia.com ([77.102.228.6] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1S3PlB-0006bH-LB for fwts-devel@lists.ubuntu.com; Fri, 02 Mar 2012 10:30:49 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] lib: fwts_args: handle out of memory failures Date: Fri, 2 Mar 2012 10:30:48 +0000 Message-Id: <1330684248-20025-1-git-send-email-colin.king@canonical.com> X-Mailer: git-send-email 1.7.9 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.13 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: fwts-devel-bounces@lists.ubuntu.com Errors-To: fwts-devel-bounces@lists.ubuntu.com From: Colin Ian King Signed-off-by: Colin Ian King --- src/lib/src/fwts_args.c | 18 +++++++++++++++--- 1 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c index 9837959..7538497 100644 --- a/src/lib/src/fwts_args.c +++ b/src/lib/src/fwts_args.c @@ -69,8 +69,10 @@ int fwts_args_add_options(fwts_option *options, fwts_args_optarg_handler handler if (!options_init) (void)fwts_args_init(); - if ((options_table = calloc(1, sizeof(fwts_options_table))) == NULL) + if ((options_table = calloc(1, sizeof(fwts_options_table))) == NULL) { + fwts_log_error(fw, "Out of memory allocating options table."); return FWTS_ERROR; + } for (n=0; options[n].long_name != NULL; n++) ; @@ -100,11 +102,13 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[]) int c; int option_index; int ret = FWTS_OK; - char *short_options = NULL; - if ((long_options = calloc(1, (total_options + 1) * sizeof(struct option))) == NULL) + long_options = calloc(1, (total_options + 1) * sizeof(struct option)); + if (long_options == NULL) { + fwts_log_error(fw, "Out of memory allocating long options."); return FWTS_ERROR; + } /* * Build a getopt_long options table from all the options tables @@ -128,6 +132,12 @@ int fwts_args_parse(fwts_framework *fw, int argc, char * const argv[]) strcat(short_options, short_name); } else { short_options = calloc(1, len + 1); + if (short_options == NULL) { + fwts_log_error(fw, + "Out of memory " + "allocating options."); + return FWTS_ERROR; + } strcpy(short_options, short_name); } } @@ -322,6 +332,8 @@ char *fwts_args_comma_list(const char *arg) /* Any empty list should return an empty string and not NULL */ if (retstr == NULL) retstr = calloc(1, 1); + if (retstr == NULL) + fwts_log_error(fw, "Out of memory allocating list."); return retstr; }