summaryrefslogtreecommitdiff
path: root/egg
diff options
context:
space:
mode:
authorDaiki Ueno <dueno@src.gnome.org>2018-02-21 16:42:16 +0100
committerDaiki Ueno <dueno@src.gnome.org>2018-02-21 16:46:37 +0100
commit8a2bee5b2ddb424a4b374f6baca4492c0679fed1 (patch)
tree73608e8ee6877fc99700775732750d38db3e55d8 /egg
parent63b0c09c8045f59a9e14fd4362ae3567316c64e3 (diff)
downloadgnome-keyring-8a2bee5b2ddb424a4b374f6baca4492c0679fed1.tar.gz
egg: Fix null termination behavior of egg_secure_strndup()
Even if the given string is shorter than n, the result should be null terminated. This matches the behavior of strndup().
Diffstat (limited to 'egg')
-rw-r--r--egg/egg-secure-memory.c1
-rw-r--r--egg/test-secmem.c25
2 files changed, 26 insertions, 0 deletions
diff --git a/egg/egg-secure-memory.c b/egg/egg-secure-memory.c
index ac4ce06a..4d8b3cc8 100644
--- a/egg/egg-secure-memory.c
+++ b/egg/egg-secure-memory.c
@@ -1344,6 +1344,7 @@ egg_secure_strndup_full (const char *tag,
len = length + 1;
res = (char *)egg_secure_alloc_full (tag, len, options);
memcpy (res, str, len);
+ res[length] = '\0';
return res;
}
diff --git a/egg/test-secmem.c b/egg/test-secmem.c
index 58b0d35f..54d81368 100644
--- a/egg/test-secmem.c
+++ b/egg/test-secmem.c
@@ -252,6 +252,30 @@ test_strclear (void)
egg_secure_free_full (str, 0);
}
+static void
+test_strndup (void)
+{
+ gchar *str;
+
+ str = egg_secure_strndup ("secret", 6);
+ g_assert (str != NULL);
+ g_assert_cmpuint (strlen (str), ==, 6);
+
+ egg_secure_free_full (str, 0);
+
+ str = egg_secure_strndup ("secret", 10);
+ g_assert (str != NULL);
+ g_assert_cmpuint (strlen (str), ==, 6);
+
+ egg_secure_free_full (str, 0);
+
+ str = egg_secure_strndup ("long secret", 6);
+ g_assert (str != NULL);
+ g_assert_cmpuint (strlen (str), ==, 6);
+
+ egg_secure_free_full (str, 0);
+}
+
int
main (int argc, char **argv)
{
@@ -264,6 +288,7 @@ main (int argc, char **argv)
g_test_add_func ("/secmem/multialloc", test_multialloc);
g_test_add_func ("/secmem/clear", test_clear);
g_test_add_func ("/secmem/strclear", test_strclear);
+ g_test_add_func ("/secmem/strndup", test_strndup);
return g_test_run ();
}