From 34b558246079ef588852c0bd050cee8ae857a338 Mon Sep 17 00:00:00 2001 From: matt335672 <30179339+matt335672@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:24:07 +0100 Subject: [PATCH] Remove unnecessary copy from clipboard_get_files() The routine clipboard_get_files() parses a potentially long string, and copies portions of it into a temporary buffer. This buffer is then passed to clipboard_get_file() as pointer + length; The buffer is inadequately sized for very long filenames which may approach XFS_MAXFILENAMELEN in length. This can cause chansrv to fail when the user copies such filenames. It turns out the buffer is unnecessary, as the filenames can be passed directly into clipboard_get_file() from the source string, using pointer + length. This avoids the length limitation entirely. --- sesman/chansrv/clipboard_file.c | 41 +++++++++++++++++---------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/sesman/chansrv/clipboard_file.c b/sesman/chansrv/clipboard_file.c index d6e02c58a..2d515a779 100644 --- a/sesman/chansrv/clipboard_file.c +++ b/sesman/chansrv/clipboard_file.c @@ -285,37 +285,38 @@ clipboard_get_file(const char *file, int bytes) } /*****************************************************************************/ +/* + * Calls clipboard_get_file() for each filename in a list. + * + * List items are separated by line terminators. Blank items are ignored */ static int clipboard_get_files(const char *files, int bytes) { - int index; - int file_index; - char file[512]; + const char *start = files; + const char *end = files + bytes; + const char *p; - file_index = 0; - for (index = 0; index < bytes; index++) + for (p = start ; p < end ; ++p) { - if (files[index] == '\n' || files[index] == '\r') + if (*p == '\n' || *p == '\r') { - if (file_index > 0) + /* Skip zero-length files (which might be caused by + * multiple line terminators */ + if (p > start) { - if (clipboard_get_file(file, file_index) == 0) - { - } - file_index = 0; + /* Get file. Errors are logged */ + (void)clipboard_get_file(start, p - start); } - } - else - { - file[file_index] = files[index]; - file_index++; + + /* Move the start of filename pointer to either 'end', or + * the next character which will either be a filename or + * another terminator */ + start = p + 1; } } - if (file_index > 0) + if (end > start) { - if (clipboard_get_file(file, file_index) == 0) - { - } + (void)clipboard_get_file(start, end - start); } if (g_files_list->count < 1) {