You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
nob_get_file_type error check is dead code: nob_copy_directory_recursively aborts and nob_walk_dir returns true on unreadable paths #262
nob_get_file_type() returns (Nob_File_Type)-1 to signal failure, but two callers check for that with if (type < 0). Nob_File_Type is an enum with only non-negative enumerators, so the compiler gives it an unsigned underlying type and the < 0 check is always false. The error is never caught.
nob__walk_dir_opt_impl() (line 2122): the walk continues with an invalid file_type and nob_walk_dir() returns true for a path that could not be stat-ed.
nob_copy_directory_recursively() (line 2328): execution falls through the switch to default: NOB_UNREACHABLE(...), which calls abort(). A missing or unreadable source directory crashes the whole program instead of returning false.
Both compile without warning under gcc -Wall -Wextra and clang -Wall -Wextra.
Repro
#defineNOB_IMPLEMENTATION#include"nob.h"staticboolwalk_cb(Nob_Walk_Entryentry)
{
(void) entry;
return true;
}
intmain(void)
{
constchar*missing="this-path-does-not-exist";
// nob_walk_dir() reports success on a path that could not be stat-edboolwalk_ok=nob_walk_dir(missing, walk_cb);
nob_log(NOB_INFO, "nob_walk_dir returned %s", walk_ok ? "true" : "false");
// nob_copy_directory_recursively() aborts instead of returning falseboolcopy_ok=nob_copy_directory_recursively(missing, "dst");
nob_log(NOB_INFO, "nob_copy_directory_recursively returned %s", copy_ok ? "true" : "false");
return0;
}
$ cc -o poc poc.c && ./poc[ERROR] Could not get stat of this-path-does-not-exist: No such file or directory[INFO] nob_walk_dir returned true[ERROR] Could not get stat of this-path-does-not-exist: No such file or directory./nob.h:2372: UNREACHABLE: nob_copy_directory_recursively
$ echo$?134
Reproduced on nob.h v3.10.0 with clang on macOS and gcc 14 on Linux (same output on both).
Potential fix
Compare against the sentinel value instead of < 0, which works whether the enum is signed or unsigned:
- if (file_type < 0) nob_return_defer(false);+ if (file_type == (Nob_File_Type)-1) nob_return_defer(false);
- if (type < 0) return false;+ if (type == (Nob_File_Type)-1) return false;
With that change nob_copy_directory_recursively() returns false on the missing path and nob_walk_dir() returns false, each after logging the stat error once.
Activity