1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-06-03 22:48:03 +02:00

common: Minor fixes for the new private-keys.c.

* common/private-keys.c (my_error_from_syserror): New.  Use it in
place of gpg_error_from_syserror.
(_pkc_add, pkc_lookup, pke_next_value): Use ascii_strcasecmp.
(pkc_parse): Use xtrystrdup and append_to_strlist_try as intended.

(_pkc_add): Add braces around if-statement.
--

We should have a macro so that we do not need to define a wrapper
function like my_error_from_syserror in files where it is needed.  I
am not sure about a proper name, "my_" seems to be the easiest
replacement.  Note that the global DEFAULT_ERRSOURCE is relatively new
to replace the need to convey the error source in function calls; we
want that function from common/ return the error source of the main
binary.

We require that a key is ASCII and thus we better use ascii_strcasecmp
to avoid problems with strange locales.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-04-25 18:14:12 +02:00
parent 2502a76d8e
commit b7fa4960c2
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -61,6 +61,15 @@ struct private_key_entry
char *value; char *value;
}; };
/* Helper */
static inline gpg_error_t
my_error_from_syserror (void)
{
return gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
}
/* Allocation and deallocation. */ /* Allocation and deallocation. */
@ -179,7 +188,7 @@ assert_raw_value (pke_t entry)
&entry->value[offset]); &entry->value[offset]);
if (append_to_strlist_try (&entry->raw_value, buf) == NULL) if (append_to_strlist_try (&entry->raw_value, buf) == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
@ -267,7 +276,7 @@ assert_value (pke_t entry)
entry->value = p = xtrymalloc (len); entry->value = p = xtrymalloc (len);
if (entry->value == NULL) if (entry->value == NULL)
return gpg_error_from_syserror (); return my_error_from_syserror ();
swallow_ws = 0; swallow_ws = 0;
for (s = entry->raw_value; s; s = s->next) for (s = entry->raw_value; s; s = s->next)
@ -326,7 +335,7 @@ _pkc_add (pkc_t pk, char *name, char *value, strlist_t raw_value,
goto leave; goto leave;
} }
if (name && strcasecmp (name, "Key:") == 0 && pkc_lookup (pk, "Key:")) if (name && ascii_strcasecmp (name, "Key:") == 0 && pkc_lookup (pk, "Key:"))
{ {
err = gpg_error (GPG_ERR_INV_NAME); err = gpg_error (GPG_ERR_INV_NAME);
goto leave; goto leave;
@ -335,7 +344,7 @@ _pkc_add (pkc_t pk, char *name, char *value, strlist_t raw_value,
e = xtrycalloc (1, sizeof *e); e = xtrycalloc (1, sizeof *e);
if (e == NULL) if (e == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
@ -356,17 +365,18 @@ _pkc_add (pkc_t pk, char *name, char *value, strlist_t raw_value,
/* If so, find the last in that block. */ /* If so, find the last in that block. */
if (last) if (last)
while (last->next) {
{ while (last->next)
pke_t next = last->next; {
pke_t next = last->next;
if (next->name && strcasecmp (next->name, name) == 0) if (next->name && ascii_strcasecmp (next->name, name) == 0)
last = next; last = next;
else else
break; break;
} }
/* Otherwise, just find the last entry. */ }
else else /* Otherwise, just find the last entry. */
last = pk->last; last = pk->last;
} }
@ -410,13 +420,13 @@ pkc_add (pkc_t pk, const char *name, const char *value)
k = xtrystrdup (name); k = xtrystrdup (name);
if (k == NULL) if (k == NULL)
return gpg_error_from_syserror (); return my_error_from_syserror ();
v = xtrystrdup (value); v = xtrystrdup (value);
if (v == NULL) if (v == NULL)
{ {
xfree (k); xfree (k);
return gpg_error_from_syserror (); return my_error_from_syserror ();
} }
return _pkc_add (pk, k, v, NULL, 0); return _pkc_add (pk, k, v, NULL, 0);
@ -441,7 +451,7 @@ pkc_set (pkc_t pk, const char *name, const char *value)
v = xtrystrdup (value); v = xtrystrdup (value);
if (v == NULL) if (v == NULL)
return gpg_error_from_syserror (); return my_error_from_syserror ();
free_strlist_wipe (e->raw_value); free_strlist_wipe (e->raw_value);
e->raw_value = NULL; e->raw_value = NULL;
@ -496,7 +506,7 @@ pkc_lookup (pkc_t pk, const char *name)
{ {
pke_t entry; pke_t entry;
for (entry = pk->first; entry; entry = entry->next) for (entry = pk->first; entry; entry = entry->next)
if (entry->name && strcasecmp (entry->name, name) == 0) if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
return entry; return entry;
return NULL; return NULL;
} }
@ -518,7 +528,7 @@ pke_t
pke_next_value (pke_t entry, const char *name) pke_next_value (pke_t entry, const char *name)
{ {
for (entry = entry->next; entry; entry = entry->next) for (entry = entry->next; entry; entry = entry->next)
if (entry->name && strcasecmp (entry->name, name) == 0) if (entry->name && ascii_strcasecmp (entry->name, name) == 0)
return entry; return entry;
return NULL; return NULL;
} }
@ -558,13 +568,13 @@ pkc_set_private_key (pkc_t pk, gcry_sexp_t sexp)
raw = xtrymalloc (len); raw = xtrymalloc (len);
if (raw == NULL) if (raw == NULL)
return gpg_error_from_syserror (); return my_error_from_syserror ();
clean = xtrymalloc (len); clean = xtrymalloc (len);
if (clean == NULL) if (clean == NULL)
{ {
xfree (raw); xfree (raw);
return gpg_error_from_syserror (); return my_error_from_syserror ();
} }
gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, raw, len); gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, raw, len);
@ -621,7 +631,7 @@ pkc_parse (pkc_t *result, int *errlinep, estream_t stream)
*result = pkc_new (); *result = pkc_new ();
if (*result == NULL) if (*result == NULL)
return gpg_error_from_syserror (); return my_error_from_syserror ();
if (errlinep) if (errlinep)
*errlinep = 0; *errlinep = 0;
@ -640,7 +650,7 @@ pkc_parse (pkc_t *result, int *errlinep, estream_t stream)
/* A continuation. */ /* A continuation. */
if (append_to_strlist_try (&raw_value, buf) == NULL) if (append_to_strlist_try (&raw_value, buf) == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
continue; continue;
@ -672,26 +682,26 @@ pkc_parse (pkc_t *result, int *errlinep, estream_t stream)
value = colon + 1; value = colon + 1;
tmp = *value; tmp = *value;
*value = 0; *value = 0;
name = xstrdup (p); name = xtrystrdup (p);
*value = tmp; *value = tmp;
if (name == NULL) if (name == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
if (append_to_strlist (&raw_value, value) == NULL) if (append_to_strlist_try (&raw_value, value) == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
continue; continue;
} }
if (append_to_strlist (&raw_value, buf) == NULL) if (append_to_strlist_try (&raw_value, buf) == NULL)
{ {
err = gpg_error_from_syserror (); err = my_error_from_syserror ();
goto leave; goto leave;
} }
} }
@ -733,7 +743,7 @@ pkc_write (pkc_t pk, estream_t stream)
es_fputs (s->d, stream); es_fputs (s->d, stream);
if (es_ferror (stream)) if (es_ferror (stream))
return gpg_error_from_syserror (); return my_error_from_syserror ();
} }
return 0; return 0;