Commit 8a2bee5b authored by Daiki Ueno's avatar Daiki Ueno

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().
parent 63b0c09c
......@@ -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;
}
......
......@@ -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 ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment