Blame SOURCES/0010-Fix-GCC-warnings-about-possible-string-truncations-a.patch

9067cc
From 00241c65a5c0b4bb32a847a6abb5a86d0c704a8f Mon Sep 17 00:00:00 2001
9067cc
From: Javier Martinez Canillas <javierm@redhat.com>
9067cc
Date: Tue, 5 Feb 2019 20:08:43 +0100
9067cc
Subject: [PATCH] Fix GCC warnings about possible string truncations and buffer
9067cc
 overflows
9067cc
9067cc
Building with -Werror=stringop-truncation and -Werror=stringop-overflow
9067cc
leads to GCC complaining about possible string truncation and overflows.
9067cc
9067cc
Fix this by using memcpy(), explicitly calculating the buffers lenghts
9067cc
and set a NUL byte terminator after copying the buffers.
9067cc
9067cc
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
9067cc
---
9067cc
 grubby.c | 35 +++++++++++++++++++++++++++--------
9067cc
 1 file changed, 27 insertions(+), 8 deletions(-)
9067cc
9067cc
diff --git a/grubby.c b/grubby.c
9067cc
index 96d252a0a83..5ca689539cf 100644
9067cc
--- a/grubby.c
9067cc
+++ b/grubby.c
9067cc
@@ -459,20 +459,26 @@ char *grub2ExtractTitle(struct singleLine * line) {
9067cc
     snprintf(result, resultMaxSize, "%s", ++current);
9067cc
     
9067cc
     i++;
9067cc
+    int result_len = 0;
9067cc
     for (; i < line->numElements; ++i) {
9067cc
 	current = line->elements[i].item;
9067cc
 	current_len = strlen(current);
9067cc
 	current_indent = line->elements[i].indent;
9067cc
 	current_indent_len = strlen(current_indent);
9067cc
 
9067cc
-	strncat(result, current_indent, current_indent_len);
9067cc
+	memcpy(result + result_len, current_indent, current_indent_len);
9067cc
+	result_len += current_indent_len;
9067cc
+
9067cc
 	if (!isquote(current[current_len-1])) {
9067cc
-	    strncat(result, current, current_len);
9067cc
+	    memcpy(result + result_len, current_indent, current_indent_len);
9067cc
+	    result_len += current_len;
9067cc
 	} else {
9067cc
-	    strncat(result, current, current_len - 1);
9067cc
+	    memcpy(result + result_len, current_indent, current_indent_len);
9067cc
+	    result_len += (current_len - 1);
9067cc
 	    break;
9067cc
 	}
9067cc
     }
9067cc
+    result[result_len] = '\0';
9067cc
     return result;
9067cc
 }
9067cc
 
9067cc
@@ -1281,6 +1287,7 @@ static struct grubConfig * readConfig(const char * inName,
9067cc
 	    extras = malloc(len + 1);
9067cc
 	    *extras = '\0';
9067cc
 
9067cc
+	    int buf_len = 0;
9067cc
 	    /* get title. */
9067cc
 	    for (int i = 0; i < line->numElements; i++) {
9067cc
 		if (!strcmp(line->elements[i].item, "menuentry"))
9067cc
@@ -1292,13 +1299,18 @@ static struct grubConfig * readConfig(const char * inName,
9067cc
 
9067cc
 		len = strlen(title);
9067cc
 	        if (isquote(title[len-1])) {
9067cc
-		    strncat(buf, title,len-1);
9067cc
+		    memcpy(buf + buf_len, title, len - 1);
9067cc
+		    buf_len += (len - 1);
9067cc
 		    break;
9067cc
 		} else {
9067cc
-		    strcat(buf, title);
9067cc
-		    strcat(buf, line->elements[i].indent);
9067cc
+		    memcpy(buf + buf_len, title, len);
9067cc
+		    buf_len += len;
9067cc
+		    len = strlen(line->elements[i].indent);
9067cc
+		    memcpy(buf + buf_len, line->elements[i].indent, len);
9067cc
+		    buf_len += len;
9067cc
 		}
9067cc
 	    }
9067cc
+	    buf[buf_len] = '\0';
9067cc
 
9067cc
 	    /* get extras */
9067cc
 	    int count = 0;
9067cc
@@ -4494,10 +4506,17 @@ int main(int argc, const char ** argv) {
9067cc
 	exit(1);
9067cc
     }
9067cc
     saved_command_line[0] = '\0';
9067cc
+    int cmdline_len = 0, arg_len;
9067cc
     for (int j = 1; j < argc; j++) {
9067cc
-	strcat(saved_command_line, argv[j]);
9067cc
-	strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
9067cc
+	arg_len = strlen(argv[j]);
9067cc
+	memcpy(saved_command_line + cmdline_len, argv[j], arg_len);
9067cc
+	cmdline_len += arg_len;
9067cc
+	if (j != argc - 1) {
9067cc
+	    memcpy(saved_command_line + cmdline_len, " ", 1);
9067cc
+	    cmdline_len++;
9067cc
+	}
9067cc
     }
9067cc
+    saved_command_line[cmdline_len] = '\0';
9067cc
 
9067cc
     optCon = poptGetContext("grubby", argc, argv, options, 0);
9067cc
     poptReadDefaultConfig(optCon, 1);
9067cc
-- 
9067cc
2.20.1
9067cc