Skip to content

Commit 1c2fd39

Browse files
reximysoftware
andcommitted
Fix read_entire_dir crash on empty path
Co-authored-by: Yaroslav Erohin <ysoftware@yandex.ru>
1 parent 91c53d1 commit 1c2fd39

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

nob.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ NOBDEF bool nob_dir_entry_open(const char *dir_path, Nob_Dir_Entry *dir)
19231923
}
19241924
#else
19251925
dir->nob__private.posix_dir = opendir(dir_path);
1926-
if (dir == NULL) {
1926+
if (dir->nob__private.posix_dir == NULL) {
19271927
nob_log(NOB_ERROR, "Could not open directory %s: %s", dir_path, strerror(errno));
19281928
dir->error = true;
19291929
return false;
@@ -2074,6 +2074,10 @@ NOBDEF bool nob_walk_dir_opt(const char *root, Nob_Walk_Func func, Nob_Walk_Dir_
20742074

20752075
NOBDEF bool nob_read_entire_dir(const char *parent, Nob_File_Paths *children)
20762076
{
2077+
if (strlen(parent) == 0) {
2078+
nob_log(NOB_ERROR, "Cannot read empty path");
2079+
return false;
2080+
}
20772081
bool result = true;
20782082
Nob_Dir_Entry dir = {0};
20792083
if (!nob_dir_entry_open(parent, &dir)) nob_return_defer(false);
@@ -2910,6 +2914,7 @@ NOBDEF char *nob_temp_running_executable_path(void)
29102914
/*
29112915
Revision history:
29122916
2917+
3.2.2 (2026-02-06) Fix read_entire_dir crash on empty path (by @ysoftware)
29132918
3.2.1 (2026-01-29) Fix the implicit declaration error when nob is included as a header (by @ysoftware)
29142919
3.2.0 (2026-01-28) Introduce Chain API
29152920
- Nob_Chain

tests/read_entire_dir.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#include "shared.h"
22

3+
size_t error_counter = 0;
4+
5+
void error_counting_log_handler(Nob_Log_Level level, const char *fmt, va_list args)
6+
{
7+
UNUSED(level);
8+
UNUSED(fmt);
9+
UNUSED(args);
10+
if (level == ERROR) error_counter++;
11+
}
12+
313
int compar_paths(const void *a, const void *b)
414
{
515
const char* const* ap = (const char* const*)a;
@@ -9,6 +19,17 @@ int compar_paths(const void *a, const void *b)
919

1020
int main(void)
1121
{
22+
Nob_File_Paths empty = {0};
23+
24+
error_counter = 0;
25+
nob_log_handler *saved_log_hander = get_log_handler();
26+
set_log_handler(error_counting_log_handler);
27+
{
28+
bool ok = nob_read_entire_dir("", &empty);
29+
printf("Reading empty dir: ok=%s, error_counter=%zu\n", ok ? "true" : "false", error_counter);
30+
}
31+
set_log_handler(saved_log_hander);
32+
1233
if (!write_entire_file("foo.txt", NULL, 0)) return 1;
1334
if (!write_entire_file("bar.txt", NULL, 0)) return 1;
1435
if (!write_entire_file("baz.txt", NULL, 0)) return 1;

tests/read_entire_dir.stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Reading empty dir: ok=false, error_counter=1
12
Tests:
23
bar.txt
34
baz.txt

tests/read_entire_dir.win32.stdout.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Reading empty dir: ok=false, error_counter=1
12
Tests:
23
bar.txt
34
baz.txt

0 commit comments

Comments
 (0)