grubby/Fix-GCC-warnings-about-possible-string-truncations-a.patch

109 lines
3.5 KiB
Diff
Raw Normal View History

2019-12-30 14:36:43 +08:00
From 9a2fc457659cc2baee7d13ed6b0f8c864ae605d9 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Tue, 5 Feb 2019 17:29:11 +0100
Subject: [PATCH 58/60] Fix GCC warnings about possible string
truncations and
buffer overflows
Building with -Werror=stringop-truncation and -Werror=stringop-overflow
leads to GCC complaining about possible string truncation and overflows.
Fix this by using memcpy(), explicitly calculating the buffers lenghts
and set a NUL byte terminator after copying the buffers.
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---
grubby.c | 38 ++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/grubby.c b/grubby.c
index 1d3e924..1de7b52 100644
--- a/grubby.c
+++ b/grubby.c
@@ -463,20 +463,28 @@ char *grub2ExtractTitle(struct singleLine * line) {
snprintf(result, resultMaxSize, "%s", ++current);
i++;
+ int result_len = 0;
for (; i < line->numElements; ++i) {
current = line->elements[i].item;
current_len = strlen(current);
current_indent = line->elements[i].indent;
current_indent_len = strlen(current_indent);
- strncat(result, current_indent, current_indent_len);
+ memcpy(result + result_len, current_indent, current_indent_len);
+ result_len += current_indent_len;
+
if (current[current_len-1] != quote_char) {
- strncat(result, current, current_len);
+ memcpy(result + result_len, current_indent,
+ current_indent_len);
+ result_len += current_len;
} else {
- strncat(result, current, current_len - 1);
+ memcpy(result + result_len, current_indent,
+ current_indent_len);
+ result_len += (current_len - 1);
break;
}
}
+ result[result_len] = '\0';
return result;
}
@@ -1300,6 +1308,7 @@ static struct grubConfig * readConfig(const char * inName,
extras = malloc(len + 1);
*extras = '\0';
+ int buf_len = 0;
/* get title. */
for (int i = 0; i < line->numElements; i++) {
if (!strcmp(line->elements[i].item, "menuentry"))
@@ -1314,13 +1323,18 @@ static struct grubConfig * readConfig(const char * inName,
len = strlen(title);
if (title[len-1] == quote_char) {
- strncat(buf, title,len-1);
+ memcpy(buf + buf_len, title, len - 1);
+ buf_len += (len - 1);
break;
} else {
- strcat(buf, title);
- strcat(buf, line->elements[i].indent);
+ memcpy(buf + buf_len, title, len);
+ buf_len += len;
+ len = strlen(line->elements[i].indent);
+ memcpy(buf + buf_len, line->elements[i].indent, len);
+ buf_len += len;
}
}
+ buf[buf_len] = '\0';
/* get extras */
int count = 0;
@@ -4589,10 +4603,18 @@ int main(int argc, const char ** argv) {
exit(1);
}
saved_command_line[0] = '\0';
+ int cmdline_len = 0, arg_len;
for (int j = 1; j < argc; j++) {
- strcat(saved_command_line, argv[j]);
- strncat(saved_command_line, j == argc -1 ? "" : " ", 1);
+ arg_len = strlen(argv[j]);
+ memcpy(saved_command_line + cmdline_len, argv[j], arg_len);
+ cmdline_len += arg_len;
+ if (j != argc - 1) {
+ memcpy(saved_command_line + cmdline_len, " ", 1);
+ cmdline_len++;
+ }
+
}
+ saved_command_line[cmdline_len] = '\0';
optCon = poptGetContext("grubby", argc, argv, options, 0);
poptReadDefaultConfig(optCon, 1);
--
2.19.1