The man pages for glob give a few return values for the glob function itself:
On successful completion, glob() returns zero. Other possible returns are: GLOB_NOSPACE for running out of memory, GLOB_ABORTED for a read error, and GLOB_NOMATCH for no found matches.
It also describes the globfree function:
The globfree() function frees the dynamically allocated storage from an earlier call to glob().
My question is: If I want to avoid leaking glob_t data, should I call globfree always after calling glob, or should I only call it if glob returns 0?
As far as I can see, the man page doesn't go into detail of whether or not an unsuccessful call to glob allocates memory (the GLOB_NOMATCH return value case is the most ambiguous to me). The example in the man page doesn't call globfree, either.
In other words, which of these, if any, is correct:
glob_t result;
if (!glob("*", 0, NULL, &result)) {
/* do something with result */
}
globfree(&result);
Or:
glob_t result;
if (!glob("*", 0, NULL, &result)) {
/* do something with result */
globfree(&result);
}