Changing basename(3) and dirname(3) to the POSIX-mandated non-const
parameters produces these warnings when compiling patch(1):

/usr/src/usr.bin/patch/backupfile.c:61:34: warning: passing 'const char *' to pa
rameter of type 'char *' discards qualifiers [-Wincompatible-pointer-types-disca
rds-qualifiers]
/usr/src/usr.bin/patch/backupfile.c:64:16: warning: passing 'const char *' to pa
rameter of type 'char *' discards qualifiers [-Wincompatible-pointer-types-disca
rds-qualifiers]

Here's a fix to accommodate a basename(3) that takes a non-const
parameter and may in fact modify the string buffer.  This is
originally from Joerg Sonnenberger for DragonFly BSD and has since
spread to the other BSDs.
https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff/41871674d0079dec70d55eb824f39d07dc7b3310
(The corresponding change to inp.c is no longer applicable as that
code has been removed.)

OK?

Index: usr.bin/patch/backupfile.c
===================================================================
RCS file: /cvs/src/usr.bin/patch/backupfile.c,v
retrieving revision 1.21
diff -u -p -r1.21 backupfile.c
--- usr.bin/patch/backupfile.c  26 Nov 2013 13:19:07 -0000      1.21
+++ usr.bin/patch/backupfile.c  10 Oct 2020 14:36:58 -0000
@@ -53,21 +53,32 @@ static void invalid_arg(const char *, co
 char *
 find_backup_file_name(const char *file)
 {
-       char    *dir, *base_versions;
+       char    *dir, *base_versions, *tmp_file;
        int     highest_backup;
 
        if (backup_type == simple)
                return concat(file, simple_backup_suffix);
-       base_versions = concat(basename(file), ".~");
+       tmp_file = strdup(file);
+       if (tmp_file == NULL)
+               return NULL;
+       base_versions = concat(basename(tmp_file), ".~");
+       free(tmp_file);
        if (base_versions == NULL)
                return NULL;
-       dir = dirname(file);
+       tmp_file = strdup(file);
+       if (tmp_file == NULL) {
+               free(base_versions);
+               return NULL;
+       }
+       dir = dirname(tmp_file);
        if (dir == NULL) {
                free(base_versions);
+               free(tmp_file);
                return NULL;
        }
        highest_backup = max_backup_version(base_versions, dir);
        free(base_versions);
+       free(tmp_file);
        if (backup_type == numbered_existing && highest_backup == 0)
                return concat(file, simple_backup_suffix);
        return make_version_name(file, highest_backup + 1);
-- 
Christian "naddy" Weisgerber                          na...@mips.inka.de

Reply via email to