From: Theo Buehler Subject: Re: Fix rmdir -p /foo/bar To: Christian Weisgerber Cc: tech@openbsd.org Date: Mon, 12 May 2025 08:24:34 +0200 On Sun, May 11, 2025 at 08:35:37PM +0200, Christian Weisgerber wrote: > Theo Buehler: > > > > - 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. > > Is it UB? Those are all pointers, not arrays. Yes, I'm pretty sure it is. Pointer arithmetic is only defined if the initial pointer and the result point to the same "array" or the element immediately past it. A pointer to an object that isn't an array is to be interpreted as an array of size one. Ref: C99 6.5.6 items 7 and 8. Same language in C23 6.5.6 itmes 8 and 9. Whether compilers can actually benefit from this is a different question. > Diff for the sake of completeness below, but... That's too bad.