Index | Thread | Search

From:
Theo Buehler <tb@theobuehler.org>
Subject:
Re: Fix rmdir -p /foo/bar
To:
Christian Weisgerber <naddy@mips.inka.de>
Cc:
tech@openbsd.org
Date:
Sun, 11 May 2025 08:29:15 +0200

Download raw body.

Thread
> @@ -92,9 +92,11 @@ rm_path(char *path)
>  
>  	while ((p = strrchr(path, '/')) != NULL) {
>  		/* Delete trailing slashes. */
> -		while (--p > path && *p == '/')
> +		while (--p >= path && *p == '/')
>  			continue;

I don't think this approach is kosher. p can now end up pointing before
path, which is UB. I think NetBSD's solution is cleaner, if slightly
less efficient:

https://github.com/NetBSD/src/blob/trunk/bin/rmdir/rmdir.c#L100

>  		*++p = '\0';
> +		if (p == path)
> +			break;
>  
>  		if (rmdir(path) == -1) {
>  			warn("%s", path);
> 
> -- 
> Christian "naddy" Weisgerber                          naddy@mips.inka.de
>