basename(3) can fail, prevent segfault in strlen(3).
authorflorian <florian@openbsd.org>
Wed, 12 Jul 2023 15:44:47 +0000 (15:44 +0000)
committerflorian <florian@openbsd.org>
Wed, 12 Jul 2023 15:44:47 +0000 (15:44 +0000)
OK tb, sthen

usr.bin/patch/pch.c

index af1af7a..a3e0a9c 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: pch.c,v 1.64 2023/07/12 11:26:13 tb Exp $     */
+/*     $OpenBSD: pch.c,v 1.65 2023/07/12 15:44:47 florian Exp $        */
 
 /*
  * patch - a program to apply diffs to original files
@@ -1422,7 +1422,7 @@ compare_names(const struct file_name *names, bool assume_exists)
 {
        size_t min_components, min_baselen, min_len, tmp;
        char *best = NULL;
-       char *path;
+       char *path, *bn;
        int i;
 
        /*
@@ -1443,7 +1443,10 @@ compare_names(const struct file_name *names, bool assume_exists)
                        min_components = tmp;
                        best = path;
                }
-               if ((tmp = strlen(basename(path))) > min_baselen)
+               bn = basename(path);
+               if (bn == NULL)
+                       continue;
+               if ((tmp = strlen(bn)) > min_baselen)
                        continue;
                if (tmp < min_baselen) {
                        min_baselen = tmp;