avoid memory leaks by calling free() on the string that is added to a stringlist. defer variable declarations until the point it is needed & can be initialized roker-linux
authorRoker <roker@pep-project.org>
Wed, 13 Jul 2016 23:49:15 +0200
branchroker-linux
changeset 868e2bee94816ac
parent 867 28e65ff06c40
child 872 90970c754308
avoid memory leaks by calling free() on the string that is added to a stringlist. defer variable declarations until the point it is needed & can be initialized
src/pgp_netpgp.c
     1.1 --- a/src/pgp_netpgp.c	Wed Jul 13 23:25:35 2016 +0200
     1.2 +++ b/src/pgp_netpgp.c	Wed Jul 13 23:49:15 2016 +0200
     1.3 @@ -1170,8 +1170,8 @@
     1.4      } else { 
     1.5  
     1.6          *keylist = stringlist_add(*keylist, newfprstr);
     1.7 +        free(newfprstr);
     1.8          if (*keylist == NULL) {
     1.9 -            free(newfprstr);
    1.10              return PEP_OUT_OF_MEMORY;
    1.11          }
    1.12      }
    1.13 @@ -1239,18 +1239,14 @@
    1.14      result = _export_keydata(key, &buffer, &buflen);
    1.15      
    1.16      if(result == PEP_STATUS_OK){
    1.17 -        char *encoded_key;
    1.18 -        char *request;
    1.19 -        size_t encoded_key_len;
    1.20 -
    1.21 -        encoded_key = curl_escape(buffer, (int)buflen);
    1.22 +        char *encoded_key = curl_escape(buffer, (int)buflen);
    1.23          if(!encoded_key){
    1.24              result = PEP_OUT_OF_MEMORY;
    1.25              goto free_buffer;
    1.26          }
    1.27 -        encoded_key_len = strlen(encoded_key);
    1.28 +        size_t encoded_key_len = strlen(encoded_key);
    1.29  
    1.30 -        request = calloc(1, HKP_REQ_PREFIX_LEN + encoded_key_len + 1);
    1.31 +        char *request = calloc(1, HKP_REQ_PREFIX_LEN + encoded_key_len + 1);
    1.32          if(!request){
    1.33              result = PEP_OUT_OF_MEMORY;
    1.34              goto free_encoded_key;
    1.35 @@ -1261,6 +1257,7 @@
    1.36          request[HKP_REQ_PREFIX_LEN + encoded_key_len] = '\0';
    1.37  
    1.38          if(!stringlist_add(encoded_keys, request)){
    1.39 +            free(request);
    1.40              result = PEP_OUT_OF_MEMORY;
    1.41          }
    1.42  
    1.43 @@ -1280,12 +1277,8 @@
    1.44  {
    1.45      static const char *ks_cmd = HKP_SERVER "/pks/add";
    1.46  
    1.47 -    stringlist_t *encoded_keys;
    1.48 -    const stringlist_t *post;
    1.49 -
    1.50      PEP_STATUS result;
    1.51 -
    1.52 -    CURL *curl;
    1.53 +    CURL *curl = NULL;
    1.54  
    1.55      assert(session);
    1.56      assert(pattern);
    1.57 @@ -1293,7 +1286,7 @@
    1.58      if (!session || !pattern )
    1.59          return PEP_ILLEGAL_VALUE;
    1.60  
    1.61 -    encoded_keys = new_stringlist(NULL);
    1.62 +    stringlist_t *encoded_keys = new_stringlist(NULL);
    1.63      assert(encoded_keys);
    1.64      if (encoded_keys == NULL) {
    1.65          return PEP_OUT_OF_MEMORY;
    1.66 @@ -1325,7 +1318,7 @@
    1.67      if(result == PEP_STATUS_OK){
    1.68          CURLcode curlres;
    1.69  
    1.70 -        for (post = encoded_keys; post != NULL; post = post->next) {
    1.71 +        for (const stringlist_t *post = encoded_keys; post != NULL; post = post->next) {
    1.72              assert(post->value);
    1.73  
    1.74              curl_easy_setopt(curl, CURLOPT_URL, ks_cmd);
    1.75 @@ -1535,8 +1528,6 @@
    1.76          const char *reason
    1.77      )
    1.78  {
    1.79 -    pgp_key_t *pkey;
    1.80 -    pgp_key_t *skey;
    1.81      uint8_t fpr[PGP_FINGERPRINT_SIZE];
    1.82      size_t length;
    1.83      unsigned from = 0;
    1.84 @@ -1559,7 +1550,7 @@
    1.85          goto unlock_netpgp;
    1.86      }
    1.87      
    1.88 -    pkey = pgp_getkeybyfpr(
    1.89 +    pgp_key_t *pkey = pgp_getkeybyfpr(
    1.90                             netpgp.io,
    1.91                             netpgp.pubring,
    1.92                             fpr, length, &from, NULL,
    1.93 @@ -1572,7 +1563,7 @@
    1.94      }
    1.95      
    1.96      from = 0;
    1.97 -    skey = pgp_getkeybyfpr(
    1.98 +    pgp_key_t *skey = pgp_getkeybyfpr(
    1.99                             netpgp.io,
   1.100                             netpgp.secring,
   1.101                             fpr, length, &from, NULL,