Skip to content

Commit 7cdf424

Browse files
authored
Try to fix #51
1 parent fc94023 commit 7cdf424

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/runtime/runtime.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* of this software and associated documentation files (the "Software"), to deal
1717
* in the Software without restriction, including without limitation the rights
1818
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19-
* copies of the Software, and to permit persons to whom the Software is
19+
* copies of the Software, and to permit persons to whom the Softwaref is
2020
* furnished to do so, subject to the following conditions:
2121
*
2222
* The above copyright notice and this permission notice shall be included in
@@ -865,22 +865,49 @@ bool rm_recursive(const char* const path) {
865865
void build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) {
866866
const size_t maxnamelen = 6;
867867

868+
// Check for NULL argv0
869+
if (argv0 == NULL) {
870+
fprintf(stderr, "Error: argv0 is NULL\n");
871+
return;
872+
}
873+
868874
// need to copy argv0 as it's a const value, basename intends to modify it
869875
char* argv0_copy = strdup(argv0);
876+
if (argv0_copy == NULL) {
877+
fprintf(stderr, "Error: strdup failed\n");
878+
return;
879+
}
880+
870881
char* path_basename = basename(argv0_copy);
871-
free(argv0_copy);
882+
if (path_basename == NULL) {
883+
free(argv0_copy);
884+
fprintf(stderr, "Error: basename returned NULL\n");
885+
return;
886+
}
872887

873888
size_t namelen = strlen(path_basename);
874889
// limit length of tempdir name
875890
if (namelen > maxnamelen) {
876891
namelen = maxnamelen;
877892
}
878893

894+
// Ensure mount_dir has enough space
895+
size_t required_length = templen + 8 + namelen + 6 + 1; // +1 for null terminator
896+
if (strlen(temp_base) + required_length > sizeof(mount_dir)) {
897+
free(argv0_copy);
898+
fprintf(stderr, "Error: mount_dir does not have enough space\n");
899+
return;
900+
}
901+
879902
strcpy(mount_dir, temp_base);
880903
strncpy(mount_dir + templen, "/.mount_", 8);
881904
strncpy(mount_dir + templen + 8, path_basename, namelen);
882905
strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6);
883-
mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination
906+
907+
// Null terminate the destination
908+
mount_dir[templen + 8 + namelen + 6] = '\0'; // null terminate destination
909+
910+
free(argv0_copy);
884911
}
885912

886913
int fusefs_main(int argc, char* argv[], void (* mounted)(void)) {

0 commit comments

Comments
 (0)