Page MenuHomeFreeBSD

whereis(1): Fix sourcedirs array size calculation
AcceptedPublic

Authored by kgalazka on Mon, Oct 5, 2:52 PM.

Details

Reviewers
fernape
ngie
Summary

While populating the sourcedirs array, its initial size
was calculated as the length of the first string in sourcepaths
rather than the number of array elements, causing memory corruption.
Use nitems() to calculate the number of elements in sourcepaths,
and check the initial malloc() result before populating the array.

Also, when adding directories from /usr/ports, the code used nele,
which was not initialized for the entries copied from sourcepaths.
Use the loop index instead.

Signed-off-by: Krzysztof Galazka <krzysztof.galazka@intel.com>

Fixes: 3963483523a4 ("whereis(1): Simplify code")
Assisted-by: Github Copilot (GPT-5.6 Terra)
Sponsored by: Intel Corporation

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 77720
Build 74603: arc lint + arc unit

Event Timeline

ngie added inline comments.
usr.bin/whereis/whereis.c
312

sizeof(*sourcedirs) works, but it kind of obfuscates the target type *shrugs*.

This revision is now accepted and ready to land.Mon, Oct 5, 4:14 PM

For the record: this is why code changes for utilities like this need tests written beforehand (@des has been leading by example in this area).