]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: readd string_erase()
authorLennart Poettering <lennart@poettering.net>
Thu, 11 Jul 2019 12:50:26 +0000 (14:50 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 4 Dec 2019 09:58:12 +0000 (10:58 +0100)
This was dropped in 8e27167cc9b8beda2bf49789b15f0b0301b95d17, but is
actually useful for some usecases still.

src/basic/string-util.c
src/basic/string-util.h
src/test/test-string-util.c

index 8e6aa63806811bc8ab4c89094ab98ed86b813b7d..b477a5153420d8ed11fda8aa43f10fcd39a4af85 100644 (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;
+}
index 04cc82b386088a7eb8b243a48356fa10c3a5a9e0..f10af9ad2f82dd4ab5983a72c37811f3fac7966b 100644 (file)
@@ -278,3 +278,5 @@ static inline char* str_realloc(char **p) {
 
         return (*p = t);
 }
+
+char* string_erase(char *x);
index 8ea399436621939d8ad8c646cc245e100c713522..7a05afb4acaadb3313c1414444c13418a8eca815 100644 (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();