Skip to content

Commit 112d0e3

Browse files
authored
Fix #51 segfault (#53)
Fix #51 segfault
1 parent fc94023 commit 112d0e3

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/runtime/runtime.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -862,25 +862,26 @@ bool rm_recursive(const char* const path) {
862862
return rv == 0;
863863
}
864864

865-
void build_mount_point(char* mount_dir, const char* const argv0, char const* const temp_base, const size_t templen) {
865+
void build_mount_point(char* mount_dir, const char* const argv0, const char* const temp_base, const size_t templen) {
866866
const size_t maxnamelen = 6;
867+
const size_t prefix_len = 8; // Length of "/.mount_"
868+
const size_t suffix_len = 6; // Length of "XXXXXX"
869+
870+
// Create a modifiable copy of argv0
871+
char argv0_copy[PATH_MAX]; // Ensure this is large enough for your use case
872+
strncpy(argv0_copy, argv0, sizeof(argv0_copy) - 1);
873+
argv0_copy[sizeof(argv0_copy) - 1] = '\0'; // Ensure null termination
867874

868-
// need to copy argv0 as it's a const value, basename intends to modify it
869-
char* argv0_copy = strdup(argv0);
870875
char* path_basename = basename(argv0_copy);
871-
free(argv0_copy);
872876

873877
size_t namelen = strlen(path_basename);
874-
// limit length of tempdir name
878+
// Limit length of tempdir name
875879
if (namelen > maxnamelen) {
876880
namelen = maxnamelen;
877881
}
878882

879-
strcpy(mount_dir, temp_base);
880-
strncpy(mount_dir + templen, "/.mount_", 8);
881-
strncpy(mount_dir + templen + 8, path_basename, namelen);
882-
strncpy(mount_dir + templen + 8 + namelen, "XXXXXX", 6);
883-
mount_dir[templen + 8 + namelen + 6] = 0; // null terminate destination
883+
// Ensure mount_dir is large enough before copying
884+
snprintf(mount_dir, templen + prefix_len + namelen + suffix_len + 1, "%s/.mount_%.*sXXXXXX", temp_base, (int)namelen, path_basename);
884885
}
885886

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

0 commit comments

Comments
 (0)