nob_get_file_type error check is dead code: nob_copy_directory_recursively aborts and nob_walk_dir returns true on unreadable paths
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 88/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- c
- Domain
- build-system, tooling
Research direction
Start in nob.h at nob__walk_dir_opt_impl() around line 2122 and nob_copy_directory_recursively() around line 2328, then inspect how nob_get_file_type() reports failure. Run the provided missing-path reproducer with GCC and Clang, and verify both callers return false after the stat error instead of continuing or aborting.
Written by the indexing model from the issue text.
Description
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 invalidfile_typeandnob_walk_dir()returnstruefor a path that could not be stat-ed.nob_copy_directory_recursively()(line 2328): execution falls through theswitchtodefault: NOB_UNREACHABLE(...), which callsabort(). A missing or unreadable source directory crashes the whole program instead of returningfalse.
Both compile without warning under gcc -Wall -Wextra and clang -Wall -Wextra.
Repro
#define NOB_IMPLEMENTATION
#include "nob.h"
static bool walk_cb(Nob_Walk_Entry entry)
{
(void) entry;
return true;
}
int main(void)
{
const char *missing = "this-path-does-not-exist";
// nob_walk_dir() reports success on a path that could not be stat-ed
bool walk_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 false
bool copy_ok = nob_copy_directory_recursively(missing, "dst");
nob_log(NOB_INFO, "nob_copy_directory_recursively returned %s", copy_ok ? "true" : "false");
return 0;
}
$ 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.
- Dominant language
- C++
- Stars
- 3.3k
- Forks
- 214
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tsoding/nob.h
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
nob__walk_dir_opt_impl() tries to close zeroed handle for non directory entriesPossibly taken @whophi claimed this 61 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
Similar issues
-
feature request
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
gavinlouuu-kpt/mib-studio-qt#517 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
EchoTools/nevr-runtime#116 ·
Maintainers usually reply within 1 day