Skip to content

Commit

Permalink
Fix buffer overflow in tinydir_file_open with long path names
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Dec 2, 2023
1 parent 74acffd commit 8124807
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# macOS
.DS_Store
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Makefile
# OS X
CMakeScripts/
*.xcodeproj
build/

# Test files
*.tmp
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)

project(tinydir_tests C)

Expand Down
14 changes: 14 additions & 0 deletions tests/file_open_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ FEATURE(file_open, "File open")
SHOULD_INT_EQUAL(r, 0);
remove(name);
SCENARIO_END
SCENARIO("Open file with a very long filename")
GIVEN("a file with a long filename in a folder with a long filename")
char folder[4096];
make_temp_dir("temp_dir_", folder);
strcat(folder, "/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
mkdir(folder, 0700);
strcat(folder, "/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
mkdir(folder, 0700);
WHEN("we open it")
tinydir_file file;
int r = tinydir_file_open(&file, folder);
THEN("the result should be successful")
SHOULD_INT_EQUAL(r, 0);
SCENARIO_END
FEATURE_END

CBEHAVE_RUN("File open:", TEST_FEATURE(file_open))
19 changes: 19 additions & 0 deletions tests/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ void make_temp_file(const char *prefix, char *out)
close(mkstemp(out));
#endif
}

void make_temp_dir(const char *prefix, char *out)
{
#ifdef _MSC_VER
if (GetTempFileName(".", prefix, 0, out) != 0)
{
// Strip the ".\\" prefix
if (strncmp(out, ".\\", 2) == 0)
{
memmove(out, out + 2, strlen(out));
}
// Create file
fclose(fopen(out, "w"));
}
#else
sprintf(out, "%sXXXXXX", prefix);
mkdtemp(out);
#endif
}
1 change: 1 addition & 0 deletions tests/util.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

void make_temp_file(const char *prefix, char *out);
void make_temp_dir(const char *prefix, char *out);
2 changes: 1 addition & 1 deletion tinydir.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path)
int result = 0;
int found = 0;
_tinydir_char_t dir_name_buf[_TINYDIR_PATH_MAX];
_tinydir_char_t file_name_buf[_TINYDIR_FILENAME_MAX];
_tinydir_char_t file_name_buf[_TINYDIR_PATH_MAX];
_tinydir_char_t *dir_name;
_tinydir_char_t *base_name;
#if (defined _MSC_VER || defined __MINGW32__)
Expand Down

0 comments on commit 8124807

Please sign in to comment.