Patchwork [03/12] lib: fwts_args: use strncpy, strncat instead of strcpy, strcat

login
register
mail settings
Submitter Colin King
Date Nov. 4, 2012, 10:19 p.m.
Message ID <1352067590-11820-4-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/197101/
State Rejected
Headers show

Comments

Colin King - Nov. 4, 2012, 10:19 p.m.
From: Colin Ian King <colin.king@canonical.com>

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_args.c |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Patch

diff --git a/src/lib/src/fwts_args.c b/src/lib/src/fwts_args.c
index f2038ac..5e823d6 100644
--- a/src/lib/src/fwts_args.c
+++ b/src/lib/src/fwts_args.c
@@ -129,26 +129,27 @@  int fwts_args_parse(fwts_framework *fw, const int argc, char * const argv[])
 
 			if (short_name && (len = strlen(short_name)) > 0) {
 				if (short_options) {
-					short_options = realloc(short_options,
-						short_options_len + len + 1);
+					size_t new_len = short_options_len + len + 1;
+					short_options = realloc(short_options, new_len);
 					if (short_options == NULL) {
 						fwts_log_error(fw,
 							"Out of memory "
 							"allocating options.");
 						return FWTS_ERROR;
 					}
-					strcat(short_options, short_name);
+					strncat(short_options, short_name, new_len);
 					short_options_len += (len + 1);
 				} else {
-					short_options = calloc(1, len + 1);
+					size_t new_len = len + 1;
+					short_options = calloc(1, new_len);
 					if (short_options == NULL) {
 						fwts_log_error(fw,
 							"Out of memory "
 							"allocating options.");
 						return FWTS_ERROR;
 					}
-					strcpy(short_options, short_name);
-					short_options_len += (len + 1);
+					strncpy(short_options, short_name, new_len);
+					short_options_len += new_len;
 				}
 			}
 		}
@@ -292,8 +293,8 @@  void fwts_args_show_options(void)
 				}
 			}
 		}
-		strcat(ptr, "--");
-		strcat(ptr, option->long_name);
+		strncat(ptr, "--", buffer - ptr);
+		strncat(ptr, option->long_name, buffer - ptr);
 
 		fwts_args_show_option(width, buffer, option->explanation);
 	}