string-util: readd string_erase()

This was dropped in 8e27167cc9, but is
actually useful for some usecases still.
This commit is contained in:
Lennart Poettering 2019-07-11 14:50:26 +02:00
parent 282bde1066
commit 53caaffdf4
3 changed files with 36 additions and 0 deletions

View File

@ -1064,3 +1064,13 @@ bool string_is_safe(const char *p) {
return true;
}
char* string_erase(char *x) {
if (!x)
return NULL;
/* A delicious drop of snake-oil! To be called on memory where we stored passphrases or so, after we
* used them. */
explicit_bzero_safe(x, strlen(x));
return x;
}

View File

@ -278,3 +278,5 @@ static inline char* str_realloc(char **p) {
return (*p = t);
}
char* string_erase(char *x);

View File

@ -9,6 +9,29 @@
#include "utf8.h"
#include "util.h"
static void test_string_erase(void) {
char *x;
x = strdupa("");
assert_se(streq(string_erase(x), ""));
x = strdupa("1");
assert_se(streq(string_erase(x), ""));
x = strdupa("123456789");
assert_se(streq(string_erase(x), ""));
assert_se(x[1] == '\0');
assert_se(x[2] == '\0');
assert_se(x[3] == '\0');
assert_se(x[4] == '\0');
assert_se(x[5] == '\0');
assert_se(x[6] == '\0');
assert_se(x[7] == '\0');
assert_se(x[8] == '\0');
assert_se(x[9] == '\0');
}
static void test_free_and_strndup_one(char **t, const char *src, size_t l, const char *expected, bool change) {
int r;
@ -543,6 +566,7 @@ static void test_memory_startswith_no_case(void) {
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_string_erase();
test_free_and_strndup();
test_ascii_strcasecmp_n();
test_ascii_strcasecmp_nn();