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

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