From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:43 +0000 (-0700) Subject: Remove memory zero related functions from util.h X-Git-Tag: stable-10.2.0~173 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7bffaf26f46a28893e07df2a46bfdb612b8a15c2;p=thirdparty%2Fopen-vm-tools.git Remove memory zero related functions from util.h Add a new header utilZero.h --- diff --git a/open-vm-tools/lib/include/util.h b/open-vm-tools/lib/include/util.h index 6d3d75d1e..48bacaa78 100644 --- a/open-vm-tools/lib/include/util.h +++ b/open-vm-tools/lib/include/util.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2016 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2017 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 @@ -49,6 +49,7 @@ #include "vm_assert.h" #include "vm_basic_defs.h" #include "unicodeTypes.h" +#include "utilZero.h" #if defined(__cplusplus) extern "C" { @@ -173,107 +174,6 @@ uint32 Util_FastRand(uint32 seed); // Not thread safe! void Util_OverrideHomeDir(const char *path); - -/* - *----------------------------------------------------------------------------- - * - * Util_ValidateBytes -- - * - * Check that memory is filled with the specified value. - * - * Results: - * NULL No error - * !NULL First address that doesn't have the proper value - * - * Side effects: - * None. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void * -Util_ValidateBytes(const void *ptr, // IN: ptr to check - size_t size, // IN: size of ptr - uint8 byteValue) // IN: memory must be filled with this -{ - uint8 *p; - uint8 *end; - uint64 bigValue; - - ASSERT(ptr); - - if (size == 0) { - return NULL; - } - - p = (uint8 *) ptr; - end = p + size; - - /* Compare bytes until a "nice" boundary is achieved. */ - while ((uintptr_t) p % sizeof bigValue) { - if (*p != byteValue) { - return p; - } - - p++; - - if (p == end) { - return NULL; - } - } - - /* Compare using a "nice sized" chunk for a long as possible. */ - memset(&bigValue, (int) byteValue, sizeof bigValue); - - while (p + sizeof bigValue <= end) { - if (*((uint64 *) p) != bigValue) { - /* That's not right... let the loop below report the exact address. */ - break; - } - - size -= sizeof bigValue; - p += sizeof bigValue; - } - - /* Handle any trailing bytes. */ - while (p < end) { - if (*p != byteValue) { - return p; - } - - p++; - } - - return NULL; -} - - -/* - *---------------------------------------------------------------------- - * - * Util_BufferIsEmpty -- - * - * Determine if the specified buffer of 'len' bytes starting at 'base' - * is empty (i.e. full of zeroes). - * - * Results: - * TRUE Yes - * FALSE No - * - * Side effects: - * None - * - *---------------------------------------------------------------------- - */ - -static INLINE Bool -Util_BufferIsEmpty(void const *base, // IN: - size_t len) // IN: -{ - return Util_ValidateBytes(base, len, '\0') == NULL; -} - - Bool Util_MakeSureDirExistsAndAccessible(char const *path, unsigned int mode); @@ -425,165 +325,7 @@ void *Util_Memdup(const void *src, size_t size); void *Util_Memcpy(void *dest, const void *src, size_t count); -/* - *----------------------------------------------------------------------------- - * - * Util_Zero -- - * - * Zeros out bufSize bytes of buf. NULL is legal. - * - * Results: - * None. - * - * Side effects: - * See above. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void -Util_Zero(void *buf, // OUT - size_t bufSize) // IN -{ - if (buf != NULL) { -#if defined _WIN32 && defined USERLEVEL - /* - * Simple memset calls might be optimized out. See CERT advisory - * MSC06-C. - */ - SecureZeroMemory(buf, bufSize); -#else - memset(buf, 0, bufSize); -#if !defined _WIN32 - /* - * Memset calls before free might be optimized out. See PR1248269. - */ - __asm__ __volatile__("" : : "r"(&buf) : "memory"); -#endif -#endif - } -} - - -/* - *----------------------------------------------------------------------------- - * - * Util_ZeroString -- - * - * Zeros out a NULL-terminated string. NULL is legal. - * - * Results: - * None. - * - * Side effects: - * See above. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void -Util_ZeroString(char *str) // IN/OUT/OPT -{ - if (str != NULL) { - Util_Zero(str, strlen(str)); - } -} - - #ifndef VMKBOOT -/* - *----------------------------------------------------------------------------- - * - * Util_ZeroFree -- - * - * Zeros out bufSize bytes of buf, and then frees it. NULL is - * legal. - * - * Results: - * None. - * - * Side effects: - * buf is zeroed, and then free() is called on it. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void -Util_ZeroFree(void *buf, // OUT/OPT - size_t bufSize) // IN -{ - if (buf != NULL) { - // See Posix_Free. - int err = errno; - Util_Zero(buf, bufSize); - free(buf); - errno = err; - } -} - - -/* - *----------------------------------------------------------------------------- - * - * Util_ZeroFreeString -- - * - * Zeros out a NULL-terminated string, and then frees it. NULL is - * legal. - * - * Results: - * None. - * - * Side effects: - * str is zeroed, and then free() is called on it. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void -Util_ZeroFreeString(char *str) // IN/OUT/OPT -{ - if (str != NULL) { - // See Posix_Free. - int err = errno; - Util_ZeroString(str); - free(str); - errno = err; - } -} - - -#ifdef _WIN32 -/* - *----------------------------------------------------------------------------- - * - * Util_ZeroFreeStringW -- - * - * Zeros out a NUL-terminated wide-character string, and then frees it. - * NULL is legal. - * - * Results: - * None. - * - * Side effects: - * str is zeroed, and then free() is called on it. - * - *----------------------------------------------------------------------------- - */ - -static INLINE void -Util_ZeroFreeStringW(wchar_t *str) // IN/OUT/OPT -{ - if (str != NULL) { - // See Posix_Free. - int err = errno; - Util_Zero(str, wcslen(str) * sizeof *str); - free(str); - errno = err; - } -} -#endif // _WIN32 - - /* *----------------------------------------------------------------------------- * @@ -646,7 +388,7 @@ Util_FreeStringList(char **list, // IN/OUT/OPT: the list to free { Util_FreeList((void **) list, length); } -#endif +#endif /* VMKBOOT */ /*