Use a single copy of subject string

A full copy can keep both the key and the value instead of allocating
twice the memory.
We are parsing key1=val1,key2=val2,... and in doing that, we currently
store 'key1' in key, and 'val1' in val, and then 'key2', 'val2', and so
on.
After this patch, we store 'key1\0val1\0' in key, which fits and saves
some memory.
Also this removes a warning produced by Coverity that is assuming that
allocating strlen(string_variable) is wrong.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Christophe Fergeau <cfergeau@redhat.com>
This commit is contained in:
Frediano Ziglio 2018-12-12 10:03:38 +00:00
parent 7f4da3ff36
commit 2d32025f77

View File

@ -282,7 +282,7 @@ static X509_NAME* subject_to_x509_name(const char *subject, int *nentries)
{
X509_NAME* in_subject;
const char *p;
char *key, *val, *k, *v = NULL;
char *key, *val = NULL, *k, *v = NULL;
enum {
KEY,
VALUE
@ -291,11 +291,10 @@ static X509_NAME* subject_to_x509_name(const char *subject, int *nentries)
spice_return_val_if_fail(subject != NULL, NULL);
spice_return_val_if_fail(nentries != NULL, NULL);
key = (char*)alloca(strlen(subject));
val = (char*)alloca(strlen(subject));
key = (char*)alloca(strlen(subject)+1);
in_subject = X509_NAME_new();
if (!in_subject || !key || !val) {
if (!in_subject || !key) {
spice_debug("failed to allocate");
return NULL;
}
@ -328,6 +327,7 @@ static X509_NAME* subject_to_x509_name(const char *subject, int *nentries)
} else if (*p == '=' && !escape) {
state = VALUE;
*k = 0;
val = k + 1;
v = val;
} else
*k++ = *p;