Only copy the correct amount of bytes from SecretValue

* When transferring to a null-terminated password
This commit is contained in:
Stef Walter 2012-03-25 12:17:03 +02:00
parent add0a1a55d
commit dd83ac0db3
3 changed files with 31 additions and 4 deletions

View File

@ -1317,6 +1317,28 @@ egg_secure_strdup_full (const char *tag,
return res; return res;
} }
char *
egg_secure_strndup_full (const char *tag,
const char *str,
size_t length,
int options)
{
size_t len;
char *res;
const char *end;
if (!str)
return NULL;
end = memchr (str, '\0', length);
if (end != NULL)
length = (end - str);
len = length + 1;
res = (char *)egg_secure_alloc_full (tag, len, options);
memcpy (res, str, len);
return res;
}
void void
egg_secure_clear (void *p, size_t length) egg_secure_clear (void *p, size_t length)
{ {

View File

@ -81,6 +81,9 @@ extern void* egg_memory_fallback (void *p, size_t length);
} \ } \
static inline void* egg_secure_strdup (const char *str) { \ static inline void* egg_secure_strdup (const char *str) { \
return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \ return egg_secure_strdup_full (G_STRINGIFY (tag), str, EGG_SECURE_USE_FALLBACK); \
} \
static inline void* egg_secure_strndup (const char *str, size_t length) { \
return egg_secure_strndup_full (G_STRINGIFY (tag), str, length, EGG_SECURE_USE_FALLBACK); \
} }
void* egg_secure_alloc_full (const char *tag, size_t length, int options); void* egg_secure_alloc_full (const char *tag, size_t length, int options);
@ -99,6 +102,8 @@ void egg_secure_validate (void);
char* egg_secure_strdup_full (const char *tag, const char *str, int options); char* egg_secure_strdup_full (const char *tag, const char *str, int options);
char* egg_secure_strndup_full (const char *tag, const char *str, size_t length, int options);
void egg_secure_strclear (char *str); void egg_secure_strclear (char *str);
void egg_secure_strfree (char *str); void egg_secure_strfree (char *str);

View File

@ -237,7 +237,7 @@ _secret_value_unref_to_password (SecretValue *value)
result = val->secret; result = val->secret;
} else { } else {
result = egg_secure_strdup (val->secret); result = egg_secure_strndup (val->secret, val->length);
if (val->destroy) if (val->destroy)
(val->destroy) (val->secret); (val->destroy) (val->secret);
} }
@ -245,7 +245,7 @@ _secret_value_unref_to_password (SecretValue *value)
g_slice_free (SecretValue, val); g_slice_free (SecretValue, val);
} else { } else {
result = egg_secure_strdup (val->secret); result = egg_secure_strndup (val->secret, val->length);
} }
return result; return result;
@ -269,7 +269,7 @@ _secret_value_unref_to_string (SecretValue *value)
result = val->secret; result = val->secret;
} else { } else {
result = g_strdup (val->secret); result = g_strndup (val->secret, val->length);
if (val->destroy) if (val->destroy)
(val->destroy) (val->secret); (val->destroy) (val->secret);
} }
@ -277,7 +277,7 @@ _secret_value_unref_to_string (SecretValue *value)
g_slice_free (SecretValue, val); g_slice_free (SecretValue, val);
} else { } else {
result = g_strdup (val->secret); result = g_strndup (val->secret, val->length);
} }
return result; return result;