There are several problems with data types, casts, and checks in
getlocalbase(). This attempts to fix them
Details
Details
- Reviewers
jrtc27 bapt bdrewery atsharma623_gmail.com - Group Reviewers
Restricted Owners Package (Owns No Changed Paths)
Diff Detail
Diff Detail
- Repository
- rS FreeBSD src repository - subversion
- Lint
Lint Passed - Unit
No Test Coverage - Build Status
Buildable 34810 Build 31851: arc lint + arc unit
Event Timeline
| lib/libutil/getlocalbase.c | ||
|---|---|---|
| 77 | strlcpy doesn't include the NUL so this to stay >=. | |
| lib/libutil/getlocalbase.c | ||
|---|---|---|
| 53–54 | This should probably be EINVAL (and could then be folded into the previous if). | |
Comment Actions
Fix items suggested by Jessica, move to sysctlnametomib() to account for
inconsistencies in the user/kernel interface
| lib/libutil/getlocalbase.c | ||
|---|---|---|
| 60 | miblen is currently uninitialised before the call; presumably it works because it's reading random garbage on the stack that's most likely >= 2. You should probably also be checking the return value here. | |
Comment Actions
The type cleanup in this patch looks good :D
However, the new sysctl code has a few bugs that need fixing before this can go in:
- miblen is uninitialised before the call to sysctlnametomib(). It is an in/out parameter holding the capacity of mib in elements, so it needs to be set first, for example miblen = nitems(mib). This is the issue jrtc27 raised in the last round and it is still present in the latest diff.
- sysctl(mib, 4, ...) hard-codes the length instead of using miblen. user.localbase only has two components, so mib[2] and mib[3] are uninitialised stack and the kernel gets a garbage OID. It should be sysctl(mib, miblen, ...).
- The return value of sysctlnametomib() is not checked. If it fails, we still call sysctl() with an uninitialised mib. Something like this would cover items 1 to 3:
miblen = nitems(mib);
if (sysctlnametomib("user.localbase", mib, &miblen) == 0 &&
sysctl(mib, miblen, path, &tmplen, NULL, 0) == 0)
return (tmplen);- Since tmplen is now a size_t, I do not see why we need to move off sysctlbyname(). A plain sysctlbyname("user.localbase", path, &tmplen, NULL, 0) would do the job and avoid all of the above. The update note mentions inconsistencies in the user/kernel interface. If there is a real problem there, please document it in a comment or the commit message. Otherwise I would prefer to keep sysctlbyname().
Minor notes:
- The sysctl path returns a length that includes the NUL, while the getenv/default path returns the strlcpy length without it. Worth deciding which is intended and documenting it.
- If the sysctl fails because the caller's buffer is too small, we silently fall back to /usr/local. That may not be what callers expect.
- Please double check that limits.h (SSIZE_MAX) and sys/param.h (nitems) are included.
Happy to approve once items 1 to 4 are addressed. This is my first time writing a review, so please correct me if i got anything wrong :3