From 8f25dbf394990eaa696d7c5262e8fc46c3c74b6c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 7 Mar 2020 10:59:23 +0100 Subject: [PATCH] Shell: Fix silly stack overflow in 'cd' builtin Let's write in C++ and we won't have as many C problems. :^) --- Shell/main.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Shell/main.cpp b/Shell/main.cpp index 99a4b780bab..aa492d98434 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -187,33 +187,35 @@ static String expand_tilde(const char* expression) static int sh_cd(int argc, const char** argv) { - char pathbuf[PATH_MAX]; + String new_path; if (argc == 1) { - strcpy(pathbuf, g.home.characters()); + new_path = g.home; } else { if (strcmp(argv[1], "-") == 0) { char* oldpwd = getenv("OLDPWD"); if (oldpwd == nullptr) return 1; - size_t len = strlen(oldpwd); - ASSERT(len + 1 <= PATH_MAX); - memcpy(pathbuf, oldpwd, len + 1); + new_path = oldpwd; } else if (argv[1][0] == '~') { auto path = expand_tilde(argv[1]); if (path.is_empty()) return 1; - strcpy(pathbuf, path.characters()); + new_path = path; } else if (argv[1][0] == '/') { - memcpy(pathbuf, argv[1], strlen(argv[1]) + 1); + new_path = argv[1]; } else { - sprintf(pathbuf, "%s/%s", g.cwd.characters(), argv[1]); + StringBuilder builder; + builder.append(g.cwd); + builder.append('/'); + builder.append(argv[1]); + new_path = builder.to_string(); } } - FileSystemPath canonical_path(pathbuf); + FileSystemPath canonical_path(new_path); if (!canonical_path.is_valid()) { - printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf); + printf("FileSystemPath failed to canonicalize '%s'\n", new_path.characters()); return 1; } const char* path = canonical_path.string().characters();