From 037084abd552e5d339144aceaa8ef889ea577609 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Mon, 28 Mar 2022 15:44:58 +0200 Subject: [PATCH] Fix #7754 remove last newline in convert_filedescriptors_to_file_list --- winpr/libwinpr/clipboard/posix.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/winpr/libwinpr/clipboard/posix.c b/winpr/libwinpr/clipboard/posix.c index f06184af2..0d1c7936c 100644 --- a/winpr/libwinpr/clipboard/posix.c +++ b/winpr/libwinpr/clipboard/posix.c @@ -669,10 +669,7 @@ static void* convert_filedescriptors_to_file_list(wClipboard* clipboard, UINT32 NULL, NULL); /* # (1 char) -> %23 (3 chars) , the first char is replaced inplace */ alloc += count_special_chars(descriptors[x].cFileName) * 2; - if (skip_last_lineending && x == count - 1) - alloc += decoration_len - lineending_len; - else - alloc += decoration_len; + alloc += decoration_len; } } @@ -741,10 +738,7 @@ static void* convert_filedescriptors_to_file_list(wClipboard* clipboard, UINT32 previous_at = stop_at + 1; } - if (skip_last_lineending && x == count - 1) - rc = _snprintf(&dst[pos], alloc - pos, "%s", previous_at); - else - rc = _snprintf(&dst[pos], alloc - pos, "%s%s", previous_at, lineending); + rc = _snprintf(&dst[pos], alloc - pos, "%s%s", previous_at, lineending); if (rc < 0) { @@ -757,6 +751,18 @@ static void* convert_filedescriptors_to_file_list(wClipboard* clipboard, UINT32 pos += (size_t)rc; } + if (skip_last_lineending) + { + const size_t endlen = strlen(lineending); + if (alloc > endlen) + { + if (memcmp(&dst[alloc - endlen - 1], lineending, endlen) == 0) + { + memset(&dst[alloc - endlen - 1], 0, endlen); + alloc -= endlen; + } + } + } winpr_HexDump(TAG, WLOG_DEBUG, (const BYTE*)dst, alloc); *pSize = (UINT32)alloc; clipboard->fileListSequenceNumber = clipboard->sequenceNumber;