From ce198a4ac75aa517e9553db3ab18b8df8947c0a0 Mon Sep 17 00:00:00 2001 From: florian Date: Wed, 12 Jul 2023 15:44:47 +0000 Subject: [PATCH] basename(3) can fail, prevent segfault in strlen(3). OK tb, sthen --- usr.bin/patch/pch.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index af1af7a86c5..a3e0a9cf5f8 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -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; -- 2.20.1