diff options
author | Andreas Schneider <asn@cryptomilk.org> | 2013-07-22 10:18:33 +0200 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2013-07-23 10:44:39 +0200 |
commit | 1829e9981bb5b3269c1bc005f30c4fc41b5724e7 (patch) | |
tree | d08b7dfd163bc6b7e959406511ccf3bbdf667aa4 /include/libssh/priv.h | |
parent | 5145daba69e462ddd69d961b0d940db3aebafa11 (diff) | |
download | libssh-1829e9981bb5b3269c1bc005f30c4fc41b5724e7.tar.gz libssh-1829e9981bb5b3269c1bc005f30c4fc41b5724e7.tar.xz libssh-1829e9981bb5b3269c1bc005f30c4fc41b5724e7.zip |
cmake: Check for HAVE_GCC_VOLATILE_MEMORY_PROTECTION.
This ensures that the memset call is not optimized out by the compiler
(works works with gcc and clang).
Diffstat (limited to 'include/libssh/priv.h')
-rw-r--r-- | include/libssh/priv.h | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/include/libssh/priv.h b/include/libssh/priv.h index abf307ad..44806ab6 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -137,6 +137,17 @@ int gettimeofday(struct timeval *__p, void *__t); # define LIBSSH_THREAD #endif +/* + * This makes sure that the compiler doesn't optimize out the code + * + * Use it in a macro where the provided variable is 'x'. + */ +#if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION) +# define LIBSSH_MEM_PROTECTION __asm__ volatile("" : : "r"(&(x)) : "memory") +#else +# define LIBSSH_MEM_PROTECTION +#endif + #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #endif @@ -226,11 +237,33 @@ int match_hostname(const char *host, const char *pattern, unsigned int len); /** Get the size of an array */ #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) +/* + * See http://llvm.org/bugs/show_bug.cgi?id=15495 + */ +#if defined(HAVE_GCC_VOLATILE_MEMORY_PROTECTION) +/** Overwrite a string with '\0' */ +# define BURN_STRING(x) do { \ + if ((x) != NULL) \ + memset((x), '\0', strlen((x))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \ + } while(0) + +/** Overwrite the buffer with '\0' */ +# define BURN_BUFFER(x, size) do { \ + if ((x) != NULL) \ + memset((x), '\0', (size))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \ + } while(0) +#else /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */ /** Overwrite a string with '\0' */ -#define BURN_STRING(x) do { if ((x) != NULL) memset((x), '\0', strlen((x))); __asm__ volatile ("" : : : "memory"); } while(0) +# define BURN_STRING(x) do { \ + if ((x) != NULL) memset((x), '\0', strlen((x))); \ + } while(0) /** Overwrite the buffer with '\0' */ -#define BURN_BUFFER(x, size) do { if ((x) != NULL) memset((x), '\0', (size))); __asm__ volatile ("") : : : "memory"; } while(0) +# define BURN_BUFFER(x, size) do { \ + if ((x) != NULL) \ + memset((x), '\0', (size))); __asm__ volatile("" : : "r"(&(x)) : "memory"); \ + } while(0) +#endif /* HAVE_GCC_VOLATILE_MEMORY_PROTECTION */ /** * This is a hack to fix warnings. The idea is to use this everywhere that we |