Page MenuHomeFreeBSD

pwd(1): De-obfuscate, style(9)
ClosedPublic

Authored by olce on Tue, Sep 15, 4:07 PM.
Tags
None
Referenced Files
F175041993: D59709.id186784.diff
Wed, Oct 7, 8:33 PM
Unknown Object (File)
Tue, Oct 6, 10:23 AM
Unknown Object (File)
Tue, Oct 6, 10:20 AM
Unknown Object (File)
Mon, Oct 5, 11:16 PM
Unknown Object (File)
Fri, Oct 2, 2:40 AM
Unknown Object (File)
Thu, Oct 1, 11:27 AM
Unknown Object (File)
Tue, Sep 29, 10:10 AM
Unknown Object (File)
Tue, Sep 29, 7:14 AM
Subscribers

Details

Summary

In getcwd_logical(), test for a '.' or '..' component in the most
straightforward and intelligible way possible. This removes
a superfluous retest of the the first character being '.' when the first
one did not pass and, more importantly, prevents the second test from
relying on a side-effect in the first.

While here, for better clarity, remove another side-effect in the
initialization statement of the inner loop, by incrementing 'p' before
the loop and leaving a small comment explaining why.

While here, test explicitly that pointed 'char' values are not 0 ('\0')
(style(9)).

No functional change (intended).

Fixes: 2df923c5d2d0 ("pwd: Clean up and adopt POSIX semantics")
MFC after: 3 days
Sponsored by: The FreeBSD Foundation

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

olce requested review of this revision.Tue, Sep 15, 4:07 PM

The proposed change is fine with me, but maybe still more clever than necessary.

bin/pwd/pwd.c
57

tbh I find the for loop here slightly less clear than

q = p;
while (*q != '\0` && *q != '/')
        q++;

But what about just using strcspn, something like

len = strcspn(p, "/");
if ((len == 1 && p[0] == '.') || (len == 2 && p[0] == '.' && p[1] == '.'))
59

If keeping this form, perhaps p[0] instead of *p?

Switch to strchrnul() to find / separators. Further simplifications.

olce marked 2 inline comments as done.

Upload the correct diff (previous one was missing #include <string.h>).

bin/pwd/pwd.c
57

I switched to strchrnul() instead as only a single char is needed (and we have optimized versions of it, FWIW). Since this one does return a pointer, I just kept the previous way of testing (with a small change as per your other inline comment).

This revision was not accepted when it landed; it landed in state Needs Review.Wed, Sep 23, 11:38 AM
This revision was automatically updated to reflect the committed changes.