FixMorph
FixMorph copied to clipboard
Generated Patch Includes Extra Parenthesis
Hi all, I'm facing a strange issue where the generated patch is created exactly how I'd expect it to. The issue is that there will usually be an accompanying unmatched parenthesis.
To give you an idea of what I'm talking about, I'm working with the iotop-c package as an example.
Patch to generate the upstream
diff --git a/src/configfile.c b/../../../upstreams/iotop/src/configfile.c
index e91f1ea..1b5ed82 100644
--- a/src/configfile.c
+++ b/../../../upstreams/iotop/src/configfile.c
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0-or-later
Copyright (C) 2014 Vyacheslav Trushkin
-Copyright (C) 2020-2023 Boian Bonev
+Copyright (C) 2020-2024 Boian Bonev
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
@@ -20,7 +20,8 @@ You should have received a copy of the GNU General Public License along with thi
#include <sys/stat.h>
#include <sys/types.h>
-#define CONFIG_PATH "/.config/iotop"
+#define CONFIG_DIR1 "/.config"
+#define CONFIG_DIR2 "/iotop"
#define CONFIG_NAME "/iotoprc"
#define MAX_OPT 50
@@ -48,15 +49,25 @@ static inline void mkdir_p(const char *dir) {
static inline FILE *config_file_open(const char *mode) {
char path[PATH_MAX];
+ char *xdgconfig;
char *home;
+ xdgconfig=getenv("XDG_CONFIG_HOME");
home=getenv("HOME");
- if (!home)
- home="";
- strcpy(path,home);
- strcat(path,CONFIG_PATH);
- mkdir_p(path);
+ if (xdgconfig) {
+ strcpy(path,xdgconfig);
+ strcat(path,CONFIG_DIR2);
+ mkdir_p(path);
+ } else {
+ if (home)
+ strcpy(path,home);
+ else
+ strcpy(path,"");
+ strcat(path,CONFIG_DIR1);
+ strcat(path,CONFIG_DIR2);
+ mkdir_p(path);
+ }
strcat(path,CONFIG_NAME);
return fopen(path,mode);
@@ -247,6 +258,9 @@ inline int config_file_save(void) {
// --ascii
if (!config.f.unicode)
fprintf(cf,"--ascii\n");
+ // --hide-time
+ if (config.f.hideclock)
+ fprintf(cf,"--hide-time\n");
fclose(cf);
(this is part of a larger patch, which I've omitted here for readability)
Upstream patch to backport:
diff --git a/src/configfile.c b/src/configfile.c
index 1b5ed82..effe23f 100644
--- a/src/configfile.c
+++ b/src/configfile.c
@@ -55,20 +55,22 @@ static inline FILE *config_file_open(const char *mode) {
xdgconfig=getenv("XDG_CONFIG_HOME");
home=getenv("HOME");
+ const size_t copy_path_max = PATH_MAX-1;
+ const size_t cat_max_minus_path = PATH_MAX-(strlen(path)+1);
if (xdgconfig) {
- strcpy(path,xdgconfig);
- strcat(path,CONFIG_DIR2);
+ strncpy(path,xdgconfig, copy_path_max);
+ strncat(path, CONFIG_DIR2, cat_max_minus_path);
mkdir_p(path);
} else {
if (home)
- strcpy(path,home);
+ strncpy(path,home, copy_path_max);
else
- strcpy(path,"");
- strcat(path,CONFIG_DIR1);
- strcat(path,CONFIG_DIR2);
+ strncpy(path,"", PATH_MAX-1);
+ strncat(path,CONFIG_DIR1, cat_max_minus_path);
+ strncat(path,CONFIG_DIR2, cat_max_minus_path);
mkdir_p(path);
}
- strcat(path,CONFIG_NAME);
+ strncat(path,CONFIG_NAME, cat_max_minus_path);
return fopen(path,mode);
}
Generated Backport Patch:
--- /dirs/iotop-c-c/src/configfile.c 2024-02-20 16:49:48.531837809 +0000
+++ /dirs/iotop-c-c-patch//src/configfile.c 2024-02-20 16:49:48.678840528 +0000
@@ -51,13 +51,17 @@
char *home;
home=getenv("HOME");
+ const size_t copy_path_max = PATH_MAX-1;
+
+ const size_t cat_max_minus_path = PATH_MAX-(strlen(path)+1);
+
if (!home)
home="";
- strcpy(path,home);
- strcat(path,CONFIG_PATH);
+ strncpy(path,home, copy_path_max) );
+ strncat(path,CONFIG_PATH, cat_max_minus_path) );
mkdir_p(path);
- strcat(path,CONFIG_NAME);
+ strncat(path,CONFIG_NAME, cat_max_minus_path) );
return fopen(path,mode);
}
As you can see in this example, there are additional )
characters being added to the function backport where there shouldn't be.
Otherwise, the patch performs as expected.