diff mbox

lib: fwts_args: handle out of memory failures

Message ID 1330684248-20025-1-git-send-email-colin.king@canonical.com
State Rejected
Headers show

Commit Message

Colin Ian King March 2, 2012, 10:30 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_args.c |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 deletions(-)

Comments

Colin Ian King March 2, 2012, 10:34 a.m. UTC | #1
On 02/03/12 10:30, Colin King wrote:
> From: Colin Ian King<colin.king@canonical.com>
>
> Signed-off-by: Colin Ian King<colin.king@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;
>   }

NACK: This is the incorrect fix. Doh
diff mbox

Patch

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;
 }