Patchwork [04/12] lib: fwts_log: 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-5-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/197102/
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_log.c |   21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

Patch

diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
index 63983d5..6e5e169 100644
--- a/src/lib/src/fwts_log.c
+++ b/src/lib/src/fwts_log.c
@@ -337,7 +337,7 @@  static char *fwts_log_filename(const char *filename, const fwts_log_type type)
 	char *suffix;
 	size_t suffix_len;
 	size_t trunc_len;
-	size_t filename_len;
+	size_t new_len;
 	struct stat stat_buf;
 
 	/*
@@ -368,12 +368,13 @@  static char *fwts_log_filename(const char *filename, const fwts_log_type type)
 		 !strcmp(ptr, ".html"))) {
 
 		trunc_len = ptr - filename;
-		if ((new_name = calloc(trunc_len + suffix_len + 1, 1)) == NULL) {
+		new_len = trunc_len + suffix_len + 1;
+		if ((new_name = calloc(new_len, 1)) == NULL) {
 			fprintf(stderr, "Cannot allocate log name.\n");
 			return NULL;
 		}
 		strncpy(new_name, filename, trunc_len);
-		strcat(new_name, suffix); /* strcat OK because calloc zero'd all of new_name */
+		strncat(new_name, suffix, new_len); /* strncat OK because calloc zero'd all of new_name */
 		return new_name;
 	}
 
@@ -381,14 +382,14 @@  static char *fwts_log_filename(const char *filename, const fwts_log_type type)
 	 * We didn't find a suffix or a known suffix, so append
 	 * the appropriate one to the given log filename
 	 */
-	filename_len = strlen(filename);
-	if ((new_name = calloc(filename_len + suffix_len + 1, 1)) == NULL) {
+	new_len = strlen(filename) + suffix_len + 1;
+	if ((new_name = calloc(new_len, 1)) == NULL) {
 		fprintf(stderr, "Cannot allocate log name.\n");
 		return NULL;
 	}
 
-	strcpy(new_name, filename);
-	strcat(new_name, suffix);
+	strncpy(new_name, filename, new_len);
+	strncat(new_name, suffix, new_len);
 
 	return new_name;
 }
@@ -588,15 +589,15 @@  char *fwts_log_get_filenames(const char *filename, const fwts_log_type type)
 					free(tmp);
 					return NULL;
 				}
-				strcat(filenames, " ");
-				strcat(filenames, tmp);
+				strncat(filenames, " ", len);
+				strncat(filenames, tmp, len);
 			} else {
 				len = strlen(tmp) + 1;
 				if ((filenames = malloc(len)) == NULL) {
 					free(tmp);
 					return NULL;
 				}
-				strcpy(filenames, tmp);
+				strncpy(filenames, tmp, len);
 			}
 			free(tmp);
 		}