[PATCH] lib: fwts_args: handle out of memory failures

Colin King colin.king at canonical.com
Fri Mar 2 10:30:48 UTC 2012


From: Colin Ian King <colin.king at canonical.com>

Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
 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;
 }
-- 
1.7.9





More information about the fwts-devel mailing list