On Sat, 2020-10-10 at 17:21 +0200, Christian Weisgerber wrote:
> Changing basename(3) and dirname(3) to the POSIX-mandated non-const
> parameters produces this warnings when compiling sed(1):
> 
> /usr/src/usr.bin/sed/main.c:401:16: warning: passing 'const char *' to 
> parameter
>  of type 'char *' discards qualifiers 
> [-Wincompatible-pointer-types-discards-qua
> lifiers]
> 
> Here's a fix to accommodate a basename(3) that takes a non-const
> parameter and may in fact modify the string buffer.  Based on FreeBSD
> like the surrounding in-place editing code.
> 
> OK?
> 
Wouldn't the following diff be a little simpler?
No need to check the return value of strlcpy, since we do a lstat
before, which can return a ENAMETOOLONG.

martijn@

Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/sed/main.c,v
retrieving revision 1.40
diff -u -p -r1.40 main.c
--- main.c      8 Dec 2018 23:11:24 -0000       1.40
+++ main.c      10 Oct 2020 17:51:53 -0000
@@ -343,6 +343,7 @@ mf_fgets(SPACE *sp, enum e_spflag spflag
 {
        struct stat sb;
        size_t len;
+       char dirbuf[PATH_MAX];
        char *p;
        int c, fd;
        static int firstfile;
@@ -397,8 +398,9 @@ mf_fgets(SPACE *sp, enum e_spflag spflag
                                if (len > sizeof(oldfname))
                                        error(FATAL, "%s: name too long", 
fname);
                        }
-                       len = snprintf(tmpfname, sizeof(tmpfname), 
"%s/sedXXXXXXXXXX",
-                           dirname(fname));
+                       strlcpy(dirbuf, fname, sizeof(dirbuf));
+                       len = snprintf(tmpfname, sizeof(tmpfname),
+                           "%s/sedXXXXXXXXXX", dirname(dirbuf));
                        if (len >= sizeof(tmpfname))
                                error(FATAL, "%s: name too long", fname);
                        if ((fd = mkstemp(tmpfname)) == -1)

Reply via email to