From c061a78dc1f7242cfcaf42049d5248e4eed39ff4 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Sat, 18 Mar 2023 13:53:26 +0100 Subject: Clean up string encoding handling We should handle this in the low-level protocol code as much as possible to avoid mistakes. This way the rest of the code can assume that strings are always UTF-8 with \n line endings. --- unix/xserver/hw/vnc/RFBGlue.cc | 9 +++++++++ unix/xserver/hw/vnc/RFBGlue.h | 2 ++ unix/xserver/hw/vnc/vncSelection.c | 5 +++++ 3 files changed, 16 insertions(+) (limited to 'unix') diff --git a/unix/xserver/hw/vnc/RFBGlue.cc b/unix/xserver/hw/vnc/RFBGlue.cc index 2fbc27ee..25431b65 100644 --- a/unix/xserver/hw/vnc/RFBGlue.cc +++ b/unix/xserver/hw/vnc/RFBGlue.cc @@ -248,3 +248,12 @@ char* vncUTF8ToLatin1(const char* src, size_t bytes) return NULL; } } + +int vncIsValidUTF8(const char* str, size_t bytes) +{ + try { + return isValidUTF8(str, bytes); + } catch (...) { + return 0; + } +} diff --git a/unix/xserver/hw/vnc/RFBGlue.h b/unix/xserver/hw/vnc/RFBGlue.h index d2b2e8aa..30c13bd2 100644 --- a/unix/xserver/hw/vnc/RFBGlue.h +++ b/unix/xserver/hw/vnc/RFBGlue.h @@ -53,6 +53,8 @@ char* vncConvertLF(const char* src, size_t bytes); char* vncLatin1ToUTF8(const char* src, size_t bytes); char* vncUTF8ToLatin1(const char* src, size_t bytes); +int vncIsValidUTF8(const char* str, size_t bytes); + #ifdef __cplusplus } #endif diff --git a/unix/xserver/hw/vnc/vncSelection.c b/unix/xserver/hw/vnc/vncSelection.c index 1ed35149..f7f3c51d 100644 --- a/unix/xserver/hw/vnc/vncSelection.c +++ b/unix/xserver/hw/vnc/vncSelection.c @@ -611,6 +611,11 @@ static void vncHandleSelection(Atom selection, Atom target, if (prop->type != xaUTF8_STRING) return; + if (!vncIsValidUTF8(prop->data, prop->size)) { + LOG_ERROR("Invalid UTF-8 sequence in clipboard"); + return; + } + filtered = vncConvertLF(prop->data, prop->size); if (filtered == NULL) return; -- cgit v1.2.3