diff --git a/libsecret/secret-value.c b/libsecret/secret-value.c index a6fc5b8..cbfdf6b 100644 --- a/libsecret/secret-value.c +++ b/libsecret/secret-value.c @@ -97,14 +97,15 @@ secret_value_new (const gchar *secret, { gchar *copy; - g_return_val_if_fail (secret == NULL || length != 0, NULL); + g_return_val_if_fail (length == 0 || secret != NULL, NULL); g_return_val_if_fail (content_type, NULL); if (length < 0) length = strlen (secret); copy = egg_secure_alloc (length + 1); - memcpy (copy, secret, length); + if (secret) + memcpy (copy, secret, length); copy[length] = 0; return secret_value_new_full (copy, length, content_type, egg_secure_free); } @@ -132,7 +133,6 @@ secret_value_new_full (gchar *secret, { SecretValue *value; - g_return_val_if_fail (secret == NULL || length != 0, NULL); g_return_val_if_fail (content_type, NULL); if (length < 0) diff --git a/libsecret/tests/test-value.c b/libsecret/tests/test-value.c index ceb2448..5578aef 100644 --- a/libsecret/tests/test-value.c +++ b/libsecret/tests/test-value.c @@ -94,6 +94,28 @@ test_new_full_terminated (void) secret_value_unref (value); } +static void +test_new_empty (void) +{ + SecretValue *value; + const gchar *password; + gsize length; + + value = secret_value_new (NULL, 0, "text/plain"); + g_assert (value != NULL); + password = secret_value_get (value, &length); + g_assert_cmpuint (length, ==, 0); + g_assert_cmpstr (password, ==, ""); + secret_value_unref (value); + + value = secret_value_new ("", 0, "text/plain"); + g_assert (value != NULL); + password = secret_value_get (value, &length); + g_assert_cmpuint (length, ==, 0); + g_assert_cmpstr (password, ==, ""); + secret_value_unref (value); +} + static void test_ref_unref (void) { @@ -202,6 +224,7 @@ main (int argc, char **argv) g_test_add_func ("/value/new-terminated", test_new_terminated); g_test_add_func ("/value/new-full", test_new_full); g_test_add_func ("/value/new-full-terminated", test_new_full_terminated); + g_test_add_func ("/value/new-empty", test_new_empty); g_test_add_func ("/value/ref-unref", test_ref_unref); g_test_add_func ("/value/boxed", test_boxed); g_test_add_func ("/value/to-password", test_to_password);