From 33d47b93a6e6fcf20e95a54bc7eae81df50d9daf Mon Sep 17 00:00:00 2001 From: Chunmei Xu Date: Fri, 29 May 2020 14:22:42 +0800 Subject: [PATCH 1/2] [build][gcc9] fix build error with >=gcc9.2 Signed-off-by: weitao zhou --- ...-about-possible-string-truncations-a.patch | 107 ++++++++++++++++++ grubby.spec | 7 +- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch diff --git a/1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch b/1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch new file mode 100644 index 0000000..db16955 --- /dev/null +++ b/1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch @@ -0,0 +1,107 @@ +From 9a2fc457659cc2baee7d13ed6b0f8c864ae605d9 Mon Sep 17 00:00:00 2001 +From: Javier Martinez Canillas +Date: Tue, 5 Feb 2019 17:29:11 +0100 +Subject: [PATCH] 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 +--- + grubby.c | 38 ++++++++++++++++++++++++++++++-------- + 1 file changed, 30 insertions(+), 8 deletions(-) + +diff --git a/grubby.c b/grubby.c +index 396041a..947b458 100644 +--- a/grubby.c ++++ b/grubby.c +@@ -479,20 +479,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; + } + +@@ -1437,6 +1445,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 +@@ -1453,13 +1462,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; +@@ -5209,10 +5223,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); +-- +1.8.3.1 + diff --git a/grubby.spec b/grubby.spec index f23e252..ba6be8d 100644 --- a/grubby.spec +++ b/grubby.spec @@ -76,8 +76,10 @@ Patch0056: 0056-Improve-man-page-for-info-option.patch #Add by anolis Patch1000: 0001-grubby-anolis-rebrand.patch -#end +# backport from upstream +Patch1001: 1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch +#end BuildRequires: gcc BuildRequires: pkgconfig glib2-devel popt-devel BuildRequires: libblkid-devel git-core sed make @@ -179,6 +181,9 @@ current boot environment. %{_mandir}/man8/*.8* %changelog +* Mon Feb 21 2022 Weitao Zhou 8.40-42.0.3 +- fix build error with gcc9.2 + * Tue Jan 25 2022 zhangwenlong - 8.40-42.0.2 - Support loongarch64 -- Gitee From e923b9807285db91280a432e3bc371a7f33fa36f Mon Sep 17 00:00:00 2001 From: Liwei Ge Date: Sun, 27 Sep 2020 22:47:11 +0800 Subject: [PATCH 2/2] Fix gcc10 build issue for better compatibility on both gcc8&gcc10 this change has given better compatible with both gcc8 and gcc10 toolchain, should be maintained util upstream fixes Signed-off-by: weitao zhou --- 1002-Fix-stringop-overflow-warning.patch | 73 ++++++++++++++++++++++++ grubby.spec | 6 +- 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 1002-Fix-stringop-overflow-warning.patch diff --git a/1002-Fix-stringop-overflow-warning.patch b/1002-Fix-stringop-overflow-warning.patch new file mode 100644 index 0000000..f7f2988 --- /dev/null +++ b/1002-Fix-stringop-overflow-warning.patch @@ -0,0 +1,73 @@ +From a38efb3c072811eb4e3bce265f1b8903a10ffad4 Mon Sep 17 00:00:00 2001 +From: Javier Martinez Canillas +Date: Sun, 27 Sep 2020 22:40:28 +0800 +Subject: [PATCH] Fix stringop-overflow warning + +GCC gives the following compile warning: + +grubby.c: In function 'main': +grubby.c:4508:27: error: writing 1 byte into a region of size 0 +[-Werror=stringop-overflow=] + 4508 | saved_command_line[0] = '\0'; + | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~ +grubby.c:4503:26: note: at offset 0 to an object with size 0 allocated +by 'malloc' here + 4503 | saved_command_line = malloc(i); + | ^~~~~~~~~ +cc1: all warnings being treated as errors +make: *** [Makefile:38: grubby.o] Error 1 + +Signed-off-by: Javier Martinez Canillas +--- + grubby.c | 32 +++++++++++++++++--------------- + 1 file changed, 17 insertions(+), 15 deletions(-) + +diff --git a/grubby.c b/grubby.c +index 947b458..7923a7d 100644 +--- a/grubby.c ++++ b/grubby.c +@@ -5217,24 +5217,26 @@ int main(int argc, const char **argv) + int i = 0; + for (int j = 1; j < argc; j++) + i += strlen(argv[j]) + 1; +- saved_command_line = malloc(i); +- if (!saved_command_line) { +- fprintf(stderr, "grubby: %m\n"); +- exit(1); +- } +- saved_command_line[0] = '\0'; +- int cmdline_len = 0, arg_len; +- for (int j = 1; j < argc; j++) { +- 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++; ++ if (i > 0) { ++ saved_command_line = malloc(i); ++ if (!saved_command_line) { ++ fprintf(stderr, "grubby: %m\n"); ++ exit(1); + } ++ saved_command_line[0] = '\0'; ++ int cmdline_len = 0, arg_len; ++ for (int j = 1; j < argc; j++) { ++ 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'; + } +- saved_command_line[cmdline_len] = '\0'; + + optCon = poptGetContext("grubby", argc, argv, options, 0); + poptReadDefaultConfig(optCon, 1); +-- +2.19.1.6.gb485710b + diff --git a/grubby.spec b/grubby.spec index ba6be8d..b9eae4c 100644 --- a/grubby.spec +++ b/grubby.spec @@ -75,11 +75,12 @@ Patch0056: 0056-Improve-man-page-for-info-option.patch #Add by anolis Patch1000: 0001-grubby-anolis-rebrand.patch - # backport from upstream Patch1001: 1001-Fix-GCC-warnings-about-possible-string-truncations-a.patch - +# port patch from fc33 +Patch1002: 1002-Fix-stringop-overflow-warning.patch #end + BuildRequires: gcc BuildRequires: pkgconfig glib2-devel popt-devel BuildRequires: libblkid-devel git-core sed make @@ -183,6 +184,7 @@ current boot environment. %changelog * Mon Feb 21 2022 Weitao Zhou 8.40-42.0.3 - fix build error with gcc9.2 +- Fix gcc10 build issue * Tue Jan 25 2022 zhangwenlong - 8.40-42.0.2 - Support loongarch64 -- Gitee