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