From: Kruti Pendharkar Date: Tue, 25 Feb 2025 09:29:11 +0000 (-0800) Subject: Fix a couple of memory leaks in drag-and-drop/copy-paste code: X-Git-Tag: stable-13.0.0~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c58276668b6a6b608f99203204e315bbf040f53;p=thirdparty%2Fopen-vm-tools.git Fix a couple of memory leaks in drag-and-drop/copy-paste code: * `CopyPasteRpcV3::HandleMsg` calls `CPClipboard_Unserialize` which allocates memory to the supplied `Clipboard` structure, but it neglected to call `CPClipboard_Destroy` to free it when done. Also update the documentation for `CPClipboard_Unserialize` to make the contract more explicit. * `DnD_SetCPClipboardFromLocalText` completely neglected to free its destination buffer. Bonus cleanup: * Make some pointers `const` (which also allows us to remove some casts). * Replace a call to `UNICODE_RELEASE_UTF16` with `free` since the memory was allocated by lib/dnd with `malloc` and was not allocated by lib/unicode. --- diff --git a/open-vm-tools/services/plugins/dndcp/dnd/dnd.h b/open-vm-tools/services/plugins/dndcp/dnd/dnd.h index f6b950351..913cf5c11 100644 --- a/open-vm-tools/services/plugins/dndcp/dnd/dnd.h +++ b/open-vm-tools/services/plugins/dndcp/dnd/dnd.h @@ -231,10 +231,10 @@ HGLOBAL DnD_CreateHDropForGuest(const char *path, const char *fileList); size_t DnD_CPStringToLocalString(const char *bufIn, utf16_t **bufOut); -size_t DnD_LocalStringToCPString(utf16_t *bufIn, +size_t DnD_LocalStringToCPString(const utf16_t *bufIn, char **bufOut); Bool DnD_SetCPClipboardFromLocalText(CPClipboard *clip, - utf16_t *bufIn); + const utf16_t *bufIn); Bool DnD_SetCPClipboardAndTruncateLocalText(CPClipboard *clip, utf16_t *bufIn); Bool DnD_SetCPClipboardFromLocalRtf(CPClipboard *clip, diff --git a/open-vm-tools/services/plugins/dndcp/dnd/dndClipboard.c b/open-vm-tools/services/plugins/dndcp/dnd/dndClipboard.c index 849c6699d..fcde5fd28 100644 --- a/open-vm-tools/services/plugins/dndcp/dnd/dndClipboard.c +++ b/open-vm-tools/services/plugins/dndcp/dnd/dndClipboard.c @@ -1,5 +1,6 @@ /********************************************************* - * Copyright (C) 2007-2021 VMware, Inc. All rights reserved. + * Copyright (c) 2007-2024 Broadcom. All Rights Reserved. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -690,7 +691,8 @@ CPClipboard_Serialize(const CPClipboard *clip, // IN * * Side effects: * The clip passed in should be empty, otherwise will cause memory leakage. - * On success, arguments found in buf are unserialized into clip. + * On success, arguments found in buf are unserialized into clip, which + * must be destroyed by calling CPClipboard_Destroy. * *---------------------------------------------------------------------------- */ diff --git a/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.c b/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.c index 6c2d08593..427e2a21d 100644 --- a/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.c +++ b/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.c @@ -1,5 +1,6 @@ /********************************************************* - * Copyright (c) 2007-2019, 2023 VMware, Inc. All rights reserved. + * Copyright (c) 2007-2024 Broadcom. All Rights Reserved. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -360,9 +361,9 @@ DnDMsg_Serialize(DnDMsg *msg, // IN/OUT: the message */ DnDMsgErr -DnDMsg_UnserializeHeader(DnDMsg *msg, // IN/OUT: the message - void *buf, // IN: the input buffer - size_t len) // IN: the buffer length +DnDMsg_UnserializeHeader(DnDMsg *msg, // IN/OUT: the message + const void *buf, // IN: the input buffer + size_t len) // IN: the buffer length { BufRead r; @@ -422,7 +423,7 @@ DnDMsg_UnserializeHeader(DnDMsg *msg, // IN/OUT: the message DnDMsgErr DnDMsg_UnserializeArgs(DnDMsg *msg, // IN/OUT: the message - void *buf, // IN: input buffer + const void *buf, // IN: input buffer size_t len) // IN: buffer length { uint32 i; diff --git a/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.h b/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.h index 3b852c46b..58d222cdb 100644 --- a/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.h +++ b/open-vm-tools/services/plugins/dndcp/dnd/dndMsg.h @@ -1,5 +1,6 @@ /********************************************************* - * Copyright (c) 2007-2017, 2023 VMware, Inc. All rights reserved. + * Copyright (c) 2007-2024 Broadcom. All Rights Reserved. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -158,8 +159,8 @@ DynBuf *DnDMsg_GetArg(DnDMsg *msg, uint32 arg); Bool DnDMsg_AppendArg(DnDMsg *msg, void *buf, size_t len); Bool DnDMsg_Serialize(DnDMsg *msg, DynBuf *buf); -DnDMsgErr DnDMsg_UnserializeHeader(DnDMsg *msg, void *buf, size_t len); -DnDMsgErr DnDMsg_UnserializeArgs(DnDMsg *msg, void *buf, size_t len); +DnDMsgErr DnDMsg_UnserializeHeader(DnDMsg *msg, const void *buf, size_t len); +DnDMsgErr DnDMsg_UnserializeArgs(DnDMsg *msg, const void *buf, size_t len); #if defined(__cplusplus) } // extern "C"