Index | Thread | Search

From:
Kirill A. Korinsky <kirill@korins.ky>
Subject:
Re: patch: simplify fetchname()
To:
"Omar Polo" <op@omarpolo.com>
Cc:
tech@openbsd.org
Date:
Sat, 07 Nov 2026 00:23:50 +0100

Download raw body.

Thread
On Thu, 05 Nov 2026 23:53:31 +0100,
"Omar Polo" <op@omarpolo.com> wrote:
> 
> "Omar Polo" <op@omarpolo.com> wrote:
> > This is a preparatory step to get more hardening being put into
> > fetchname().  The goal is to make fetchname() a bit more humanly
> > understandable before adding things to it.
> > 
> > While here, also add a regress for file names with spaces on them.
> > 
> > okay?
> 
> forgot to 'cvs add' a test file, and also added \f\v to the character
> set as discussed with kirill@ to match completely the previous
> behvaiour of isspace(3).
>

It simplifies a code and as I read it, it doesn't change the logic.

Did it?

> Index: regress/usr.bin/patch/Makefile
> ===================================================================
> RCS file: /cvs/src/regress/usr.bin/patch/Makefile,v
> diff -u -p -r1.14 Makefile
> --- regress/usr.bin/patch/Makefile	3 Aug 2022 07:35:04 -0000	1.14
> +++ regress/usr.bin/patch/Makefile	5 Nov 2026 19:52:58 -0000
> @@ -2,10 +2,11 @@
>  
>  PATCH?=		patch
>  PATCHFLAGS=	-sN
> -CLEANFILES=	*.copy *.orig *.rej t5 d19/*
> +CLEANFILES=	*.copy *.orig *.rej t5 d19/* t21?file
>  
>  REGRESS_TARGETS=     t1  t2  t3  t4  t5  t6  t7  t8  t9 \
> -		t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20
> +		t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 \
> +		t20 t21
>  
>  # .in: input file
>  # .diff: patch
> @@ -29,6 +30,7 @@ REGRESS_TARGETS=     t1  t2  t3  t4  t5 
>  # t18: diff in ed format that fully replaces input content.
>  # t19: git-produced unified diff.
>  # t20: reversal application of a patch to create a file with a single line.
> +# t21: diff contains a file name with whitespaces in it
>  
>  .SUFFIXES: .in
>  
> @@ -80,6 +82,12 @@ t20:
>  	@cmp -s ${*}.copy ${.CURDIR}/${*}.out || \
>  		(echo "XXX ${*} failed" && false)
>  
> -.PHONY: t5
> +t21:
> +	@echo ${*}
> +	@echo one > "${*} file"
> +	@${PATCH} ${PATCHFLAGS} < ${.CURDIR}/${*}.diff
> +	@echo two | cmp -s "${*} file" - || (echo "XXX t21 failed" && false)
> +
> +.PHONY: t5 t21
>  
>  .include <bsd.regress.mk>
> Index: regress/usr.bin/patch/t21.diff
> ===================================================================
> RCS file: regress/usr.bin/patch/t21.diff
> diff -N regress/usr.bin/patch/t21.diff
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ regress/usr.bin/patch/t21.diff	5 Nov 2026 17:46:31 -0000
> @@ -0,0 +1,5 @@
> +--- t21 file	Thu Nov  5 11:43:45 CST 2026
> ++++ t21 file	Thu Nov  5 11:43:45 CST 2026
> +@@ -1 +1 @@
> +-one
> ++two
> Index: usr.bin/patch/util.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/patch/util.c,v
> diff -u -p -r1.47 util.c
> --- usr.bin/patch/util.c	5 Nov 2026 11:27:33 -0000	1.47
> +++ usr.bin/patch/util.c	5 Nov 2026 22:51:46 -0000
> @@ -341,7 +341,7 @@ char *
>  fetchname(const char *at, bool *exists, int strip_leading)
>  {
>  	char		*fullname, *name, *t;
> -	int		sleading, tab;
> +	int		sleading;
>  	struct stat	filestat;
>  
>  	if (at == NULL || *at == '\0')
> @@ -355,19 +355,21 @@ fetchname(const char *at, bool *exists, 
>  	/* So files can be created by diffing against /dev/null.  */
>  	if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
>  		return NULL;
> -	name = fullname = t = savestr(at);
> +	name = fullname = savestr(at);
>  	if (out_of_mem)
>  		return NULL;
>  
> -	tab = strchr(t, '\t') != NULL;
> -	/* Strip off up to `strip_leading' path components and NUL terminate. */
> -	for (sleading = strip_leading; *t != '\0' && ((tab && *t != '\t') ||
> -	    !isspace((unsigned char)*t)); t++) {
> +	if ((t = strchr(fullname, '\t')) != NULL)
> +		*t = '\0';
> +	else
> +		fullname[strcspn(fullname, " \f\v\r\n")] = '\0';
> +
> +	/* Strip off up to `strip_leading' path components. */
> +	for (sleading = strip_leading, t = fullname; *t != '\0'; t++) {
>  		if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
>  			if (--sleading >= 0)
>  				name = t + 1;
>  	}
> -	*t = '\0';
>  
>  	/*
>  	 * If no -p option was given (957 is the default value!), we were
> 

-- 
wbr, Kirill