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

Colin King colin.king at canonical.com
Sun Nov 4 22:19:41 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 |   17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

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




More information about the fwts-devel mailing list