From: Oliver Kurth Date: Tue, 5 Jun 2018 22:47:38 +0000 (-0700) Subject: Add Safe version of DynBuf_Enlarge X-Git-Tag: stable-11.0.0~550 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=411d143eee043afe7f649573305096453608d77a;p=thirdparty%2Fopen-vm-tools.git Add Safe version of DynBuf_Enlarge This change adds the "safe" variant of the enlarge interface. Similar to DynBuf_SafeAppend, this panics when the buffer enlargement fails due to memory allocation failures. --- diff --git a/open-vm-tools/lib/include/dynbuf.h b/open-vm-tools/lib/include/dynbuf.h index b2541ee1b..f9978cc24 100644 --- a/open-vm-tools/lib/include/dynbuf.h +++ b/open-vm-tools/lib/include/dynbuf.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2017 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2018 VMware, Inc. All rights reserved. * * 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 @@ -88,6 +88,15 @@ DynBuf_SafeInternalAppend(DynBuf *b, // IN/OUT #define DynBuf_SafeAppend(_buf, _data, _size) \ DynBuf_SafeInternalAppend(_buf, _data, _size, __FILE__, __LINE__) +void +DynBuf_SafeInternalEnlarge(DynBuf *b, // IN/OUT + size_t min_size, // IN + char const *file, // IN + unsigned int lineno); // IN + +#define DynBuf_SafeEnlarge(_buf, _min_size) \ + DynBuf_SafeInternalEnlarge(_buf, _min_size, __FILE__, __LINE__) + /* *----------------------------------------------------------------------------- diff --git a/open-vm-tools/lib/misc/dynbuf.c b/open-vm-tools/lib/misc/dynbuf.c index 76b250a5d..dc36d0708 100644 --- a/open-vm-tools/lib/misc/dynbuf.c +++ b/open-vm-tools/lib/misc/dynbuf.c @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2017 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2018 VMware, Inc. All rights reserved. * * 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 @@ -313,6 +313,36 @@ DynBuf_Enlarge(DynBuf *b, // IN/OUT: } +/* + *----------------------------------------------------------------------------- + * + * DynBuf_SafeInternalEnlarge -- + * + * Enlarge 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_SafeInternalEnlarge(DynBuf *b, // IN/OUT: + size_t minSize, // IN: + char const *file, // IN: + unsigned int lineno) // IN: +{ + if (!DynBuf_Enlarge(b, minSize)) { + Panic("Unrecoverable memory allocation failure at %s:%u\n", + file, lineno); + } +} + + /* *----------------------------------------------------------------------------- *