diff options
| author | Johannes Altmanninger <aclopte@gmail.com> | 2024-11-11 13:28:27 +0100 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2024-11-12 08:54:19 +1100 |
| commit | d86e505fad397251a8a618c545a7d78f43a70a34 (patch) | |
| tree | 75faa59c1fafa0e9b510b88e5eee12ec40fba3a8 /src/remote.cc | |
| parent | 919934177d6f3149b523c476ab41c37e1ba51034 (diff) | |
Fail rename-session instead of creating overlong socket paths
Commit 9cf8a3ccd (Check for buffer overflow when constructing the socket path., 2022-04-07)
made
$ kak -s $(printf %0100d)
fail but forgot to do the same for
$ kak -e "rename-session $(printf %0100d)"
which silently succeeds, only to fail at the next
$ echo nop | kak -p $(printf %0100d)
Fatal error: socket path too long: '/run/user/1000/kakoune/0000...'
Let's fail earlier.
While at it, don't validate "m_session" redundantly.
I'm not sure if we should validate the socket names in "kak -clear";
I guess it doesn't matter.
Diffstat (limited to 'src/remote.cc')
| -rw-r--r-- | src/remote.cc | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/remote.cc b/src/remote.cc index 97a62b02..3ef2c152 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -619,21 +619,21 @@ const String& session_directory() return session_dir; } -String session_path(StringView session) +String session_path(StringView session, bool assume_valid) { - if (not all_of(session, is_identifier)) + if (not assume_valid and not all_of(session, is_identifier)) throw runtime_error{format("invalid session name: '{}'", session)}; - return format("{}/{}", session_directory(), session); + String path = format("{}/{}", session_directory(), session); + if (not assume_valid and path.length() + 1 > sizeof sockaddr_un{}.sun_path) + throw runtime_error{format("socket path too long: '{}'", path)}; + return path; } static sockaddr_un session_addr(StringView session) { sockaddr_un addr; addr.sun_family = AF_UNIX; - String path = session_path(session); - if (path.length() + 1 > sizeof addr.sun_path) - throw runtime_error{format("socket path too long: '{}'", path)}; - strcpy(addr.sun_path, path.c_str()); + strcpy(addr.sun_path, session_path(session).c_str()); return addr; } @@ -889,7 +889,7 @@ Server::Server(String session_name, bool is_daemon) bool Server::rename_session(StringView name) { - String old_socket_file = session_path(m_session); + String old_socket_file = session_path(m_session, true); String new_socket_file = session_path(name); if (file_exists(new_socket_file)) @@ -906,7 +906,7 @@ void Server::close_session(bool do_unlink) { if (do_unlink) { - String socket_file = session_path(m_session); + String socket_file = session_path(m_session, true); unlink(socket_file.c_str()); } m_listener->close_fd(); |
