aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-07-16 16:31:43 -0700
committer3gg <3gg@shellblade.net>2023-07-16 16:31:43 -0700
commitf48986597e6ff092feb0f18a79c6fa51a3f0e3bb (patch)
tree4182eb80d0eedc30448cda4d69c24cccd1bdcaf4
parent987d395b7b88b58cb412c88a57deb0e1eada1c37 (diff)
Fix path concatenation.
-rw-r--r--filesystem/include/filesystem.h4
-rw-r--r--filesystem/src/filesystem.c18
2 files changed, 13 insertions, 9 deletions
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);
15 15
16/// Make a path relative to the parent directory of a file. 16/// Make a path relative to the parent directory of a file.
17bool make_relative_path( 17bool make_relative_path(
18 size_t max_path_length, const char* filepath, const char* path, 18 const char* filepath, const char* path, char* relative,
19 char* relative); 19 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:
56} 56}
57 57
58bool make_relative_path( 58bool make_relative_path(
59 size_t max_path_length, const char* filepath, const char* path, 59 const char* filepath, const char* path, char* relative,
60 char* relative) { 60 size_t relative_length) {
61 assert(filepath); 61 assert(filepath);
62 assert(path); 62 assert(path);
63 assert(relative); 63 assert(relative);
64 64
65 // Handle empty filepath.
66 const size_t filepath_len = strlen(filepath); 65 const size_t filepath_len = strlen(filepath);
66 const size_t path_len = strlen(path);
67 assert(filepath_len < relative_length);
68 assert(path_len < relative_length);
69
70 // Handle empty filepath.
67 if (filepath_len == 0) { 71 if (filepath_len == 0) {
68 memcpy(relative, path, max_path_length); 72 memcpy(relative, path, path_len);
69 return true; 73 return true;
70 } 74 }
71 75
72 memcpy(relative, filepath, max_path_length);
73 // Search for the last / in the tile map file path to get its parent 76 // Search for the last / in the tile map file path to get its parent
74 // directory. 77 // directory.
75 assert(filepath_len > 0); 78 assert(filepath_len > 0);
@@ -83,10 +86,11 @@ bool make_relative_path(
83 86
84 // Copy the tile set file path where the parent dir ends. 87 // Copy the tile set file path where the parent dir ends.
85 // Make sure there is enough space in the output. 88 // Make sure there is enough space in the output.
86 const size_t path_len = strlen(path); 89 if ((tm_dir_len + path_len + 1) >= relative_length) {
87 if ((tm_dir_len + path_len + 1) >= max_path_length) {
88 return false; 90 return false;
89 } 91 }
92 memcpy(relative, filepath, tm_dir_len);
90 memcpy(&relative[tm_dir_len], path, path_len); 93 memcpy(&relative[tm_dir_len], path, path_len);
94
91 return true; 95 return true;
92} 96}