string-util: introduce explicit_zero_safe()

The only real difference is that this wrapper can deal with NULL
pointer arguments, but only if the length is also zero.

CID 1396277
This commit is contained in:
Lennart Poettering
2018-10-24 21:00:15 +02:00
parent 7388cea30d
commit 87f5446311
5 changed files with 25 additions and 15 deletions
+6 -3
View File
@@ -1059,8 +1059,11 @@ typedef void *(*memset_t)(void *,int,size_t);
static volatile memset_t memset_func = memset;
void explicit_bzero(void *p, size_t l) {
memset_func(p, '\0', l);
void* explicit_bzero_safe(void *p, size_t l) {
if (l > 0)
memset_func(p, '\0', l);
return p;
}
#endif
@@ -1070,7 +1073,7 @@ char* string_erase(char *x) {
/* A delicious drop of snake-oil! To be called on memory where
* we stored passphrases or so, after we used them. */
explicit_bzero(x, strlen(x));
explicit_bzero_safe(x, strlen(x));
return x;
}