fix #30 : memleak in myself(): stored_identity is never freed in keymanagement.c
authorEdouard Tisserant
Mon, 11 Jul 2016 11:34:23 +0200
changeset 8297bb16947031a
parent 828 ea7e5920eda3
child 830 4d7551272344
fix #30 : memleak in myself(): stored_identity is never freed in keymanagement.c
src/keymanagement.c
     1.1 --- a/src/keymanagement.c	Mon Jul 11 09:21:23 2016 +0200
     1.2 +++ b/src/keymanagement.c	Mon Jul 11 11:34:23 2016 +0200
     1.3 @@ -142,21 +142,23 @@
     1.4      
     1.5      assert(status != PEP_OUT_OF_MEMORY);
     1.6      if (status == PEP_OUT_OF_MEMORY)
     1.7 -        return PEP_OUT_OF_MEMORY;
     1.8 +        goto exit_free;
     1.9  
    1.10      if (stored_identity) {
    1.11          PEP_comm_type _comm_type_key;
    1.12          status = get_key_rating(session, stored_identity->fpr, &_comm_type_key);
    1.13          assert(status != PEP_OUT_OF_MEMORY);
    1.14          if (status == PEP_OUT_OF_MEMORY)
    1.15 -            return PEP_OUT_OF_MEMORY;
    1.16 +            goto exit_free;
    1.17  
    1.18          if (EMPTYSTR(identity->username)) {
    1.19              free(identity->username);
    1.20              identity->username = strdup(stored_identity->username);
    1.21              assert(identity->username);
    1.22 -            if (identity->username == NULL)
    1.23 -                return PEP_OUT_OF_MEMORY;
    1.24 +            if (identity->username == NULL){
    1.25 +                status = PEP_OUT_OF_MEMORY;
    1.26 +                goto exit_free;
    1.27 +            }
    1.28          }
    1.29  
    1.30          if (EMPTYSTR(identity->fpr)) {
    1.31 @@ -167,7 +169,7 @@
    1.32              if (_comm_type_key < PEP_ct_unconfirmed_encryption) {
    1.33                  PEP_STATUS status = elect_pubkey(session, identity);
    1.34                  if (status != PEP_STATUS_OK)
    1.35 -                    return status;
    1.36 +                    goto exit_free;
    1.37              }
    1.38              else {
    1.39                  identity->comm_type = stored_identity->comm_type;
    1.40 @@ -190,7 +192,7 @@
    1.41                  status = get_trust(session, identity);
    1.42                  assert(status != PEP_OUT_OF_MEMORY);
    1.43                  if (status == PEP_OUT_OF_MEMORY)
    1.44 -                    return PEP_OUT_OF_MEMORY;
    1.45 +                    goto exit_free;
    1.46                  if (identity->comm_type < stored_identity->comm_type)
    1.47                      identity->comm_type = PEP_ct_unknown;
    1.48              }
    1.49 @@ -209,14 +211,14 @@
    1.50              status = get_key_rating(session, identity->fpr, &_comm_type_key);
    1.51              assert(status != PEP_OUT_OF_MEMORY);
    1.52              if (status == PEP_OUT_OF_MEMORY)
    1.53 -                return PEP_OUT_OF_MEMORY;
    1.54 +                goto exit_free;
    1.55  
    1.56              identity->comm_type = _comm_type_key;
    1.57          }
    1.58          else /* EMPTYSTR(identity->fpr) */ {
    1.59              PEP_STATUS status = elect_pubkey(session, identity);
    1.60              if (status != PEP_STATUS_OK)
    1.61 -                return status;
    1.62 +                goto exit_free;
    1.63          }
    1.64      }
    1.65  
    1.66 @@ -228,8 +230,10 @@
    1.67          if (EMPTYSTR(identity->username)) { // mitigate
    1.68              free(identity->username);
    1.69              identity->username = strdup("anonymous");
    1.70 -            if (identity->username == NULL)
    1.71 -                return PEP_OUT_OF_MEMORY;
    1.72 +            if (identity->username == NULL){
    1.73 +                status = PEP_OUT_OF_MEMORY;
    1.74 +                goto exit_free;
    1.75 +            }
    1.76          }
    1.77  
    1.78          // Identity doesn't get stored if call was just about checking existing
    1.79 @@ -239,7 +243,7 @@
    1.80              status = set_identity(session, identity);
    1.81              assert(status == PEP_STATUS_OK);
    1.82              if (status != PEP_STATUS_OK) {
    1.83 -                return status;
    1.84 +                goto exit_free;
    1.85              }
    1.86          }
    1.87      }
    1.88 @@ -249,6 +253,12 @@
    1.89          if (session->examine_identity)
    1.90              session->examine_identity(identity, session->examine_management);
    1.91  
    1.92 +exit_free :
    1.93 +    
    1.94 +    if (stored_identity){
    1.95 +        free_identity(stored_identity);
    1.96 +    }
    1.97 +
    1.98      return status;
    1.99  }
   1.100