From: John Wolfe Date: Mon, 4 Apr 2022 19:58:41 +0000 (-0700) Subject: DynBuf: Implement new function DynBuf_SafeInternalInsert(). X-Git-Tag: stable-12.1.0~104 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd8be34a9a4e897d78297eb8ae970300e80253a0;p=thirdparty%2Fopen-vm-tools.git DynBuf: Implement new function DynBuf_SafeInternalInsert(). --- diff --git a/open-vm-tools/lib/include/dynbuf.h b/open-vm-tools/lib/include/dynbuf.h index 0d87d87cb..9ffd2b8af 100644 --- a/open-vm-tools/lib/include/dynbuf.h +++ b/open-vm-tools/lib/include/dynbuf.h @@ -75,7 +75,7 @@ DynBuf_Append(DynBuf *b, // IN/OUT void const *data, // IN size_t size); // IN -Bool +MUST_CHECK_RETURN Bool DynBuf_Insert(DynBuf *b, // IN/OUT size_t offset, // IN void const *data, // IN @@ -98,6 +98,17 @@ DynBuf_SafeInternalAppend(DynBuf *b, // IN/OUT #define DynBuf_SafeAppend(_buf, _data, _size) \ DynBuf_SafeInternalAppend(_buf, _data, _size, __FILE__, __LINE__) +void +DynBuf_SafeInternalInsert(DynBuf *b, // IN/OUT + size_t offset, // IN + void const *data, // IN + size_t size, // IN + char const *file, // IN + unsigned int lineno); // IN + +#define DynBuf_SafeInsert(_buf, _offset, _data, _size) \ + DynBuf_SafeInternalInsert(_buf, _offset, _data, _size, __FILE__, __LINE__) + void DynBuf_SafeInternalEnlarge(DynBuf *b, // IN/OUT size_t min_size, // IN diff --git a/open-vm-tools/lib/misc/dynbuf.c b/open-vm-tools/lib/misc/dynbuf.c index ef7663e1a..91b87f453 100644 --- a/open-vm-tools/lib/misc/dynbuf.c +++ b/open-vm-tools/lib/misc/dynbuf.c @@ -429,6 +429,39 @@ DynBuf_Insert(DynBuf *b, // IN/OUT: } +/* + *----------------------------------------------------------------------------- + * + * DynBuf_SafeInternalInsert -- + * + * Insert data at a given offset within a dynamic buffer. Memory + * allocation failure is handled the same way as Util_SafeMalloc, that is + * to say, with a Panic. + * + * Results: + * None + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +void +DynBuf_SafeInternalInsert(DynBuf *b, // IN/OUT: + size_t offset, // IN: + void const *data, // IN: + size_t size, // IN: + char const *file, // IN: + unsigned int lineno) // IN: +{ + if (!DynBuf_Insert(b, offset, data, size)) { + Panic("Unrecoverable memory allocation failure at %s:%u\n", + file, lineno); + } +} + + /* *----------------------------------------------------------------------------- * @@ -462,7 +495,7 @@ DynBuf_Append(DynBuf *b, // IN/OUT: * DynBuf_SafeInternalAppend -- * * Append data at the end of a dynamic buffer. Memory allocation failure - * are handled the same way as Util_SafeMalloc, that is to say, with a + * is handled the same way as Util_SafeMalloc, that is to say, with a * Panic. * * Results: