From f48986597e6ff092feb0f18a79c6fa51a3f0e3bb Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 16 Jul 2023 16:31:43 -0700 Subject: Fix path concatenation. --- filesystem/include/filesystem.h | 4 ++-- filesystem/src/filesystem.c | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'filesystem') diff --git a/filesystem/include/filesystem.h b/filesystem/include/filesystem.h index 62bd7f0..3dce87f 100644 --- a/filesystem/include/filesystem.h +++ b/filesystem/include/filesystem.h @@ -15,5 +15,5 @@ void* read_file(const char* filepath); /// Make a path relative to the parent directory of a file. bool make_relative_path( - size_t max_path_length, const char* filepath, const char* path, - char* relative); + const char* filepath, const char* path, char* relative, + size_t relative_length); diff --git a/filesystem/src/filesystem.c b/filesystem/src/filesystem.c index 6c0dcfb..5a8eeec 100644 --- a/filesystem/src/filesystem.c +++ b/filesystem/src/filesystem.c @@ -56,20 +56,23 @@ cleanup: } bool make_relative_path( - size_t max_path_length, const char* filepath, const char* path, - char* relative) { + const char* filepath, const char* path, char* relative, + size_t relative_length) { assert(filepath); assert(path); assert(relative); - // Handle empty filepath. const size_t filepath_len = strlen(filepath); + const size_t path_len = strlen(path); + assert(filepath_len < relative_length); + assert(path_len < relative_length); + + // Handle empty filepath. if (filepath_len == 0) { - memcpy(relative, path, max_path_length); + memcpy(relative, path, path_len); return true; } - memcpy(relative, filepath, max_path_length); // Search for the last / in the tile map file path to get its parent // directory. assert(filepath_len > 0); @@ -83,10 +86,11 @@ bool make_relative_path( // Copy the tile set file path where the parent dir ends. // Make sure there is enough space in the output. - const size_t path_len = strlen(path); - if ((tm_dir_len + path_len + 1) >= max_path_length) { + if ((tm_dir_len + path_len + 1) >= relative_length) { return false; } + memcpy(relative, filepath, tm_dir_len); memcpy(&relative[tm_dir_len], path, path_len); + return true; } -- cgit v1.2.3