From fed9677104982d5e83d267aec485cdb1083f4d90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Tue, 7 Jul 2020 17:35:21 -0300 Subject: [PATCH] sshfs-map: fix segfaults under musl. - line and line_size should be reset each loop, otherwise getline() will use a freed memory address. This worked under glibc, but broke under musl for some reason. More specifically, it would segfault when running setmntent for the second time. - General clean-up: is_mounted checks shouldn't call continue, otherwise they will skip the clean-up routines. This would be a memory leak, but not a functionality issue. The calls to continue were probably the reason that the program didn't segfault when running for a consecutive time, after the first map had already been mounted. - Move printf calls to outside fork block. --- sourcecode/sshfs-map/sshfs-map.c | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/sourcecode/sshfs-map/sshfs-map.c b/sourcecode/sshfs-map/sshfs-map.c index 5c67165..22f8d17 100644 --- a/sourcecode/sshfs-map/sshfs-map.c +++ b/sourcecode/sshfs-map/sshfs-map.c @@ -31,11 +31,12 @@ int main(int argc, char **argv) exit(1); } - char *line = NULL; - size_t line_size = 0; int i = 0; while (true) { + char *line = NULL; + size_t line_size = 0; + size_t result = getline(&line, &line_size, map); // EOF if (result == -1) { @@ -52,7 +53,6 @@ int main(int argc, char **argv) // guarantee null-terminated terms if (line[result - 1] == '\n') line[result - 1] = '\0'; - char *source, *dest, *full_dest; source = dest = full_dest = NULL; sscanf(line, "%ms %ms", &source, &dest); @@ -85,36 +85,36 @@ int main(int argc, char **argv) switch (mode) { pid_t p; + case SSHFS_MAP: if (is_mounted) { printf("%s is already mounted\n", full_dest); - continue; - } - p = fork(); - if (p == 0) { - printf("%s <=> %s\n", source, full_dest); - fflush(stdout); - - char *args[] = {"sshfs", source, full_dest, NULL}; - execvp("sshfs", args); } else { - int wstatus; - waitpid(p, &wstatus, 0); + printf("%s <=> %s\n", source, full_dest); + + p = fork(); + if (p == 0) { + char *args[] = {"sshfs", "-o", "follow_symlinks", source, full_dest, NULL}; + execvp("sshfs", args); + } else { + int wstatus; + waitpid(p, &wstatus, 0); + } } break; case SSHFS_UNMAP: if (!is_mounted) { printf("%s isn't mounted\n", full_dest); - continue; - } - p = fork(); - if (p == 0) { - char *args[] = {"umount", full_dest, NULL}; - execvp("umount", args); } else { - int wstatus; - waitpid(p, &wstatus, 0); + p = fork(); + if (p == 0) { + char *args[] = {"umount", full_dest, NULL}; + execvp("umount", args); + } else { + int wstatus; + waitpid(p, &wstatus, 0); + } } break; }