1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-18 14:17:03 +01:00

gpg: Use usual free semantics for packet structure free functions.

* g10/free-packet.c (free_attributes): Turn function into a nop for a
NULL arg.
(free_user_id): Ditto.
(free_compressed): Ditto.
(free_encrypted): Ditto.
(free_plaintext): Ditto.
(release_public_key_parts): Avoid extra check for NULL.
* g10/getkey.c (get_best_pubkey_byname): Ditto.
--

This change avoid surprises because it is common that function named
like free and taking a pointer also have similar semantics.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-11-15 16:23:41 +01:00
parent 12834e84ac
commit 8ea3b4c410
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 75 additions and 56 deletions

View File

@ -114,11 +114,8 @@ release_public_key_parts (PKT_public_key *pk)
xfree (pk->prefs); xfree (pk->prefs);
pk->prefs = NULL; pk->prefs = NULL;
} }
if (pk->user_id)
{
free_user_id (pk->user_id); free_user_id (pk->user_id);
pk->user_id = NULL; pk->user_id = NULL;
}
if (pk->revkey) if (pk->revkey)
{ {
xfree(pk->revkey); xfree(pk->revkey);
@ -293,6 +290,9 @@ free_comment( PKT_comment *rem )
void void
free_attributes(PKT_user_id *uid) free_attributes(PKT_user_id *uid)
{ {
if (!uid)
return;
xfree(uid->attribs); xfree(uid->attribs);
xfree(uid->attrib_data); xfree(uid->attrib_data);
@ -304,6 +304,9 @@ free_attributes(PKT_user_id *uid)
void void
free_user_id (PKT_user_id *uid) free_user_id (PKT_user_id *uid)
{ {
if (!uid)
return;
log_assert (uid->ref > 0); log_assert (uid->ref > 0);
if (--uid->ref) if (--uid->ref)
return; return;
@ -318,9 +321,14 @@ free_user_id (PKT_user_id *uid)
void void
free_compressed( PKT_compressed *zd ) free_compressed( PKT_compressed *zd )
{ {
if( zd->buf ) { /* have to skip some bytes */ if (!zd)
/* don't have any information about the length, so return;
* we assume this is the last packet */
if (zd->buf)
{
/* We need to skip some bytes. Because don't have any
* information about the length, so we assume this is the last
* packet */
while (iobuf_read( zd->buf, NULL, 1<<30 ) != -1) while (iobuf_read( zd->buf, NULL, 1<<30 ) != -1)
; ;
} }
@ -330,13 +338,22 @@ free_compressed( PKT_compressed *zd )
void void
free_encrypted( PKT_encrypted *ed ) free_encrypted( PKT_encrypted *ed )
{ {
if( ed->buf ) { /* have to skip some bytes */ if (!ed)
if( ed->is_partial ) { return;
if (ed->buf)
{
/* We need to skip some bytes. */
if (ed->is_partial)
{
while (iobuf_read( ed->buf, NULL, 1<<30 ) != -1) while (iobuf_read( ed->buf, NULL, 1<<30 ) != -1)
; ;
} }
else { else
while( ed->len ) { /* skip the packet */ {
while (ed->len)
{
/* Skip the packet. */
int n = iobuf_read( ed->buf, NULL, ed->len ); int n = iobuf_read( ed->buf, NULL, ed->len );
if (n == -1) if (n == -1)
ed->len = 0; ed->len = 0;
@ -352,13 +369,20 @@ free_encrypted( PKT_encrypted *ed )
void void
free_plaintext( PKT_plaintext *pt ) free_plaintext( PKT_plaintext *pt )
{ {
if( pt->buf ) { /* have to skip some bytes */ if (!pt)
if( pt->is_partial ) { return;
if (pt->buf)
{ /* We need to skip some bytes. */
if (pt->is_partial)
{
while (iobuf_read( pt->buf, NULL, 1<<30 ) != -1) while (iobuf_read( pt->buf, NULL, 1<<30 ) != -1)
; ;
} }
else { else
while( pt->len ) { /* skip the packet */ {
while( pt->len )
{ /* Skip the packet. */
int n = iobuf_read( pt->buf, NULL, pt->len ); int n = iobuf_read( pt->buf, NULL, pt->len );
if (n == -1) if (n == -1)
pt->len = 0; pt->len = 0;

View File

@ -1602,7 +1602,6 @@ get_best_pubkey_byname (ctrl_t ctrl, GETKEY_CTX *retctx, PKT_public_key *pk,
{ {
/* New key is better. */ /* New key is better. */
release_public_key_parts (&best.key); release_public_key_parts (&best.key);
if (best.uid)
free_user_id (best.uid); free_user_id (best.uid);
best = new; best = new;
} }
@ -1610,20 +1609,17 @@ get_best_pubkey_byname (ctrl_t ctrl, GETKEY_CTX *retctx, PKT_public_key *pk,
{ {
/* Old key is better. */ /* Old key is better. */
release_public_key_parts (&new.key); release_public_key_parts (&new.key);
if (new.uid)
free_user_id (new.uid); free_user_id (new.uid);
} }
else else
{ {
/* A tie. Keep the old key. */ /* A tie. Keep the old key. */
release_public_key_parts (&new.key); release_public_key_parts (&new.key);
if (new.uid)
free_user_id (new.uid); free_user_id (new.uid);
} }
} }
getkey_end (ctx); getkey_end (ctx);
ctx = NULL; ctx = NULL;
if (best.uid)
free_user_id (best.uid); free_user_id (best.uid);
if (best.valid) if (best.valid)
@ -3604,7 +3600,6 @@ finish_lookup (kbnode_t keyblock, unsigned int req_usage, int want_exact,
if (latest_key) if (latest_key)
{ {
pk = latest_key->pkt->pkt.public_key; pk = latest_key->pkt->pkt.public_key;
if (pk->user_id)
free_user_id (pk->user_id); free_user_id (pk->user_id);
pk->user_id = scopy_user_id (foundu); pk->user_id = scopy_user_id (foundu);
} }