Skip to content

Commit 81609f0

Browse files
gujjwal00bk138
authored andcommitted
libvncclient: Fix text length in extended clipboard encoding
Text data sent via extended clipboard encoding is null-terminated and this null character is counted in the length field sent to server. E.g. for "abc", bytes {'a', 'b', 'c', '\0'} are sent with length = 4.
1 parent 08e5ed6 commit 81609f0

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/libvncclient/rfbclient.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,20 +1811,21 @@ static rfbBool
18111811
sendExtClientCutTextProvide(rfbClient *client, char* data, int len)
18121812
{
18131813
rfbClientCutTextMsg cct = {0, };
1814+
int sentLen = len + 1; /* Sent data is null terminated*/
18141815
const uint32_t be_flags = rfbClientSwap32IfLE(rfbExtendedClipboard_Provide
18151816
| rfbExtendedClipboard_Text); /*text and provide*/
1816-
const uint32_t be_size = rfbClientSwap32IfLE(len);
1817-
const size_t sz_to_compressed = sizeof(be_size) + len; /*size, data*/
1818-
uLong csz = compressBound(sz_to_compressed + 1); /*tricky, some server need extar byte to flush data*/
1817+
const uint32_t be_size = rfbClientSwap32IfLE(sentLen);
1818+
const size_t sz_to_compressed = sizeof(be_size) + sentLen; /*size, data*/
1819+
uLong csz = compressBound(sz_to_compressed);
18191820

1820-
unsigned char *buf = malloc(sz_to_compressed + 1); /*tricky, some server need extra byte to flush data*/
1821+
unsigned char *buf = malloc(sz_to_compressed);
18211822
if (!buf) {
18221823
rfbClientLog("sendExtClientCutTextProvide. alloc buf failed\n");
18231824
return FALSE;
18241825
}
18251826
memcpy(buf, &be_size, sizeof(be_size));
18261827
memcpy(buf + sizeof(be_size), data, len);
1827-
buf[sz_to_compressed] = 0;
1828+
buf[sz_to_compressed - 1] = 0; /* Null terminate sent data */
18281829

18291830
unsigned char *cbuf = malloc(sizeof(be_flags) + csz); /*flag, compressed*/
18301831
if (!cbuf) {
@@ -1833,7 +1834,7 @@ sendExtClientCutTextProvide(rfbClient *client, char* data, int len)
18331834
return FALSE;
18341835
}
18351836
memcpy(cbuf, &be_flags, sizeof(be_flags));
1836-
if (compress(cbuf + sizeof(be_flags), &csz, buf, sz_to_compressed + 1) != Z_OK) {
1837+
if (compress(cbuf + sizeof(be_flags), &csz, buf, sz_to_compressed) != Z_OK) {
18371838
rfbClientLog("sendExtClientCutTextProvide: compress cbuf failed\n");
18381839
free(buf);
18391840
free(cbuf);

0 commit comments

Comments
 (0)