avoid unitialized local pointer variables to reduce hard-to-find bugs
authorRoker <roker@pep-project.org>
Thu, 26 Jan 2017 19:07:45 +0100
changeset 1559c6506bc6a0df
parent 1558 02b4593103fd
child 1560 6024c4e5434c
avoid unitialized local pointer variables to reduce hard-to-find bugs
src/bloblist.c
src/identity_list.c
src/keymanagement.c
src/message_api.c
src/pEpEngine.c
src/pgp_gpg.c
src/pgp_netpgp.c
src/platform_unix.c
src/stringlist.c
src/stringpair.c
src/sync_actions.c
     1.1 --- a/src/bloblist.c	Thu Jan 26 18:19:09 2017 +0100
     1.2 +++ b/src/bloblist.c	Thu Jan 26 19:07:45 2017 +0100
     1.3 @@ -44,13 +44,10 @@
     1.4  
     1.5  DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
     1.6  {
     1.7 -    bloblist_t *curr;
     1.8 -    bloblist_t *next;
     1.9 -
    1.10 -    curr = bloblist;
    1.11 +    bloblist_t *curr = bloblist;
    1.12  
    1.13      while (curr) {
    1.14 -        next = curr->next;
    1.15 +        bloblist_t *next = curr->next;
    1.16          free(curr->value);
    1.17          free(curr->mime_type);
    1.18          free(curr->filename);
    1.19 @@ -61,8 +58,6 @@
    1.20  
    1.21  DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
    1.22  {
    1.23 -    bloblist_t *bloblist = NULL;
    1.24 -
    1.25      assert(src);
    1.26      if (src == NULL)
    1.27          return NULL;
    1.28 @@ -75,7 +70,7 @@
    1.29  
    1.30      memcpy(blob2, src->value, src->size);
    1.31  
    1.32 -    bloblist = new_bloblist(blob2, src->size, src->mime_type, src->filename);
    1.33 +    bloblist_t *bloblist = new_bloblist(blob2, src->size, src->mime_type, src->filename);
    1.34      if (bloblist == NULL)
    1.35          goto enomem;
    1.36      blob2 = NULL;
     2.1 --- a/src/identity_list.c	Thu Jan 26 18:19:09 2017 +0100
     2.2 +++ b/src/identity_list.c	Thu Jan 26 19:07:45 2017 +0100
     2.3 @@ -64,13 +64,10 @@
     2.4  
     2.5  DYNAMIC_API void free_identity_list(identity_list *id_list)
     2.6  {
     2.7 -    identity_list *curr;
     2.8 -    identity_list *next;
     2.9 -    
    2.10 -    curr = id_list;
    2.11 +    identity_list *curr = id_list;
    2.12      
    2.13      while (curr) {
    2.14 -        next = curr->next;
    2.15 +        identity_list *next = curr->next;
    2.16          free_identity(curr->ident);
    2.17          free(curr);
    2.18          curr = next;
     3.1 --- a/src/keymanagement.c	Thu Jan 26 18:19:09 2017 +0100
     3.2 +++ b/src/keymanagement.c	Thu Jan 26 19:07:45 2017 +0100
     3.3 @@ -26,7 +26,7 @@
     3.4      )
     3.5  {
     3.6      PEP_STATUS status;
     3.7 -    stringlist_t *keylist;
     3.8 +    stringlist_t *keylist = NULL;
     3.9      char *_fpr = "";
    3.10      identity->comm_type = PEP_ct_unknown;
    3.11  
    3.12 @@ -82,8 +82,8 @@
    3.13          PEP_SESSION session, pEp_identity * identity
    3.14      )
    3.15  {
    3.16 -    pEp_identity *stored_identity;
    3.17 -    pEp_identity* temp_id = NULL;
    3.18 +    pEp_identity *stored_identity = NULL;
    3.19 +    pEp_identity *temp_id = NULL;
    3.20      PEP_STATUS status;
    3.21  
    3.22      assert(session);
    3.23 @@ -201,7 +201,7 @@
    3.24          
    3.25          
    3.26          /* At this point, we either have a non-blacklisted fpr we can work */
    3.27 -        /* with, or we've got nada.                                        */        
    3.28 +        /* with, or we've got nada.                                        */
    3.29  
    3.30          if (EMPTYSTR(temp_id->fpr)) {
    3.31              /* nada : set comm_type accordingly */
    3.32 @@ -308,13 +308,8 @@
    3.33      identity->flags = temp_id->flags;
    3.34  
    3.35  exit_free :
    3.36 -    
    3.37 -    if (stored_identity){
    3.38 -        free_identity(stored_identity);
    3.39 -    }
    3.40 -
    3.41 -    if (temp_id)
    3.42 -        free_identity(temp_id);
    3.43 +    free_identity(stored_identity);
    3.44 +    free_identity(temp_id);
    3.45      
    3.46      return status;
    3.47  }
    3.48 @@ -416,7 +411,7 @@
    3.49  
    3.50  PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
    3.51  {
    3.52 -    pEp_identity *stored_identity;
    3.53 +    pEp_identity *stored_identity = NULL;
    3.54      PEP_STATUS status;
    3.55  
    3.56      assert(session);
    3.57 @@ -489,7 +484,6 @@
    3.58          }
    3.59          
    3.60          identity->flags = (identity->flags & 255) | stored_identity->flags;
    3.61 -
    3.62          free_identity(stored_identity);
    3.63      }
    3.64      
    3.65 @@ -639,7 +633,6 @@
    3.66      }
    3.67  
    3.68      return PEP_STATUS_OK;
    3.69 -
    3.70  }
    3.71  
    3.72  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
     4.1 --- a/src/message_api.c	Thu Jan 26 18:19:09 2017 +0100
     4.2 +++ b/src/message_api.c	Thu Jan 26 19:07:45 2017 +0100
     4.3 @@ -87,8 +87,6 @@
     4.4  
     4.5  static char * combine_short_and_long(const char *shortmsg, const char *longmsg)
     4.6  {
     4.7 -    char * ptext;
     4.8 -
     4.9      assert(shortmsg);
    4.10      assert(strcmp(shortmsg, "pEp") != 0);
    4.11  
    4.12 @@ -111,8 +109,8 @@
    4.13      const char * const newlines = "\n\n";
    4.14      const size_t NL_LEN = 2;
    4.15  
    4.16 -    size_t bufsize = SUBJ_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
    4.17 -    ptext = calloc(1, bufsize);
    4.18 +    const size_t bufsize = SUBJ_LEN + strlen(shortmsg) + NL_LEN + strlen(longmsg) + 1;
    4.19 +    char * ptext = calloc(1, bufsize);
    4.20      assert(ptext);
    4.21      if (ptext == NULL)
    4.22          return NULL;
    4.23 @@ -709,14 +707,12 @@
    4.24  
    4.25  static bool is_encrypted_attachment(const bloblist_t *blob)
    4.26  {
    4.27 -    char *ext;
    4.28 -
    4.29      assert(blob);
    4.30  
    4.31      if (blob == NULL || blob->filename == NULL)
    4.32          return false;
    4.33  
    4.34 -    ext = strrchr(blob->filename, '.');
    4.35 +    char *ext = strrchr(blob->filename, '.');
    4.36      if (ext == NULL)
    4.37          return false;
    4.38  
    4.39 @@ -944,9 +940,8 @@
    4.40  
    4.41      bool remove = false;
    4.42  
    4.43 -    bloblist_t *bl;
    4.44      int i = 0;
    4.45 -    for (bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
    4.46 +    for (bloblist_t *bl = msg->attachments; i < MAX_KEYS_TO_IMPORT && bl && bl->value;
    4.47              bl = bl->next, i++)
    4.48      {
    4.49          if (bl && bl->value && bl->size && bl->size < MAX_KEY_SIZE
    4.50 @@ -962,9 +957,8 @@
    4.51  
    4.52  PEP_STATUS _attach_key(PEP_SESSION session, const char* fpr, message *msg)
    4.53  {
    4.54 -    char *keydata;
    4.55 +    char *keydata = NULL;
    4.56      size_t size;
    4.57 -    bloblist_t *bl;
    4.58  
    4.59      PEP_STATUS status = export_key(session, fpr, &keydata, &size);
    4.60      assert(status == PEP_STATUS_OK);
    4.61 @@ -972,7 +966,7 @@
    4.62          return status;
    4.63      assert(size);
    4.64  
    4.65 -    bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
    4.66 +     bloblist_t *bl = bloblist_add(msg->attachments, keydata, size, "application/pgp-keys",
    4.67                        "pEpkey.asc");
    4.68  
    4.69      if (msg->attachments == NULL && bl)
    4.70 @@ -1467,8 +1461,8 @@
    4.71      
    4.72      stringlist_t* orig_verify = *verify_in;
    4.73      
    4.74 -    stringlist_t* verify_curr;
    4.75 -    stringlist_t* from_keys;
    4.76 +    stringlist_t* verify_curr = NULL;
    4.77 +    stringlist_t* from_keys = NULL;
    4.78      
    4.79      /* FIXME: what to do if head needs to be null */
    4.80      PEP_STATUS status = find_keys(session, from->address, &from_keys);
    4.81 @@ -1516,7 +1510,7 @@
    4.82      
    4.83  free:
    4.84      free_stringlist(from_keys);
    4.85 -    return status;    
    4.86 +    return status;
    4.87  }
    4.88  
    4.89  
     5.1 --- a/src/pEpEngine.c	Thu Jan 26 18:19:09 2017 +0100
     5.2 +++ b/src/pEpEngine.c	Thu Jan 26 19:07:45 2017 +0100
     5.3 @@ -844,9 +844,6 @@
     5.4      )
     5.5  {
     5.6      const char *source = fingerprint;
     5.7 -    char *buffer;
     5.8 -    char *dest;
     5.9 -    size_t fsize;
    5.10  
    5.11      assert(session);
    5.12      assert(fingerprint);
    5.13 @@ -860,13 +857,13 @@
    5.14      *words = NULL;
    5.15      *wsize = 0;
    5.16  
    5.17 -    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
    5.18 +    char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
    5.19      assert(buffer);
    5.20      if (buffer == NULL)
    5.21          return PEP_OUT_OF_MEMORY;
    5.22 -    dest = buffer;
    5.23 +    char *dest = buffer;
    5.24  
    5.25 -    fsize = strlen(fingerprint);
    5.26 +    const size_t fsize = strlen(fingerprint);
    5.27  
    5.28      if (!lang || !lang[0])
    5.29          lang = "en";
    5.30 @@ -881,8 +878,8 @@
    5.31      while (source < fingerprint + fsize) {
    5.32          PEP_STATUS _status;
    5.33          uint16_t value;
    5.34 -        char *word;
    5.35 -        size_t _wsize;
    5.36 +        char *word = NULL;
    5.37 +        size_t _wsize = 0;
    5.38          int j;
    5.39  
    5.40          for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
     6.1 --- a/src/pgp_gpg.c	Thu Jan 26 18:19:09 2017 +0100
     6.2 +++ b/src/pgp_gpg.c	Thu Jan 26 19:07:45 2017 +0100
     6.3 @@ -18,13 +18,12 @@
     6.4  {
     6.5      static char buf[MAX_LINELENGTH];
     6.6      int r;
     6.7 -    FILE *f;
     6.8      stringlist_t *_k;
     6.9      stringlist_t *_v;
    6.10      unsigned int i;
    6.11      unsigned int found = 0;
    6.12  
    6.13 -    f = Fopen(config_file_path, "r");
    6.14 +    FILE *f = Fopen(config_file_path, "r");
    6.15      if (f == NULL && errno == ENOMEM)
    6.16          return false;
    6.17  
    6.18 @@ -47,9 +46,7 @@
    6.19          }
    6.20  
    6.21          do {
    6.22 -            char * s;
    6.23 -
    6.24 -            s = Fgets(buf, MAX_LINELENGTH, f);
    6.25 +            char * s = Fgets(buf, MAX_LINELENGTH, f);
    6.26              if (!feof(f)) {
    6.27                  assert(s);
    6.28                  if (s == NULL)
     7.1 --- a/src/pgp_netpgp.c	Thu Jan 26 18:19:09 2017 +0100
     7.2 +++ b/src/pgp_netpgp.c	Thu Jan 26 19:07:45 2017 +0100
     7.3 @@ -291,16 +291,16 @@
     7.4      }
     7.5      if (vresult->validc && vresult->valid_sigs &&
     7.6          !vresult->invalidc && !vresult->unknownc ) {
     7.7 -        unsigned    n;
     7.8 -        stringlist_t *k;
     7.9 +        
    7.10          // caller responsible to free
    7.11          *_keylist = new_stringlist(NULL);
    7.12          assert(*_keylist);
    7.13          if (*_keylist == NULL) {
    7.14              return PEP_OUT_OF_MEMORY;
    7.15          }
    7.16 -        k = *_keylist;
    7.17 -        for (n = 0; n < vresult->validc; ++n) {
    7.18 +        
    7.19 +        stringlist_t *k = *_keylist;
    7.20 +        for (unsigned n = 0; n < vresult->validc; ++n) {
    7.21              unsigned from = 0;
    7.22              const pgp_key_t	 *signer;
    7.23              char *fprstr = NULL;
    7.24 @@ -353,10 +353,7 @@
    7.25      char **ptext, size_t *psize, stringlist_t **keylist
    7.26      )
    7.27  {
    7.28 -    pgp_memory_t *mem;
    7.29 -    pgp_validation_t *vresult;
    7.30      char *_ptext = NULL;
    7.31 -    size_t _psize = 0;
    7.32  
    7.33      PEP_STATUS result;
    7.34      stringlist_t *_keylist = NULL;
    7.35 @@ -379,10 +376,10 @@
    7.36      *psize = 0;
    7.37      *keylist = NULL;
    7.38  
    7.39 -    vresult = malloc(sizeof(pgp_validation_t));
    7.40 +    pgp_validation_t *vresult = malloc(sizeof(pgp_validation_t));
    7.41      memset(vresult, 0x0, sizeof(pgp_validation_t));
    7.42  
    7.43 -    mem = pgp_decrypt_and_validate_buf(netpgp.io, vresult, ctext, csize,
    7.44 +    pgp_memory_t *mem = pgp_decrypt_and_validate_buf(netpgp.io, vresult, ctext, csize,
    7.45                  netpgp.secring, netpgp.pubring,
    7.46                  _armoured(ctext, csize, ARMOR_HEAD),
    7.47                  0 /* sshkeys */,
    7.48 @@ -392,7 +389,7 @@
    7.49          goto unlock_netpgp;
    7.50      }
    7.51  
    7.52 -    _psize = pgp_mem_len(mem);
    7.53 +    const size_t _psize = pgp_mem_len(mem);
    7.54      if (_psize){
    7.55          if ((_ptext = malloc(_psize + 1)) == NULL) {
    7.56              result = PEP_OUT_OF_MEMORY;
     8.1 --- a/src/platform_unix.c	Thu Jan 26 18:19:09 2017 +0100
     8.2 +++ b/src/platform_unix.c	Thu Jan 26 19:07:45 2017 +0100
     8.3 @@ -321,8 +321,7 @@
     8.4          if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
     8.5              return false;
     8.6  
     8.7 -        char *p;
     8.8 -        p = stpncpy(agent_path, dirname, MAX_PATH);
     8.9 +        char *p = stpncpy(agent_path, dirname, MAX_PATH);
    8.10          
    8.11          ssize_t len = MAX_PATH - (p - agent_path) - 2;
    8.12  
     9.1 --- a/src/stringlist.c	Thu Jan 26 18:19:09 2017 +0100
     9.2 +++ b/src/stringlist.c	Thu Jan 26 19:07:45 2017 +0100
     9.3 @@ -163,13 +163,10 @@
     9.4  
     9.5  DYNAMIC_API void free_stringlist(stringlist_t *stringlist)
     9.6  {
     9.7 -    stringlist_t *curr;
     9.8 -    stringlist_t *next;
     9.9 -    
    9.10 -    curr = stringlist;
    9.11 +    stringlist_t *curr = stringlist;;
    9.12      
    9.13      while (curr) {
    9.14 -        next = curr->next;
    9.15 +        stringlist_t *next = curr->next;
    9.16          free(curr->value);
    9.17          free(curr);
    9.18          curr = next;
    10.1 --- a/src/stringpair.c	Thu Jan 26 18:19:09 2017 +0100
    10.2 +++ b/src/stringpair.c	Thu Jan 26 19:07:45 2017 +0100
    10.3 @@ -160,8 +160,7 @@
    10.4          return stringpair_list;
    10.5  
    10.6      stringpair_list_t *_s = stringpair_list;
    10.7 -    stringpair_list_t *_s2;
    10.8 -    for (_s2 = second; _s2 != NULL; _s2 = _s2->next) {
    10.9 +    for (stringpair_list_t *_s2 = second; _s2 != NULL; _s2 = _s2->next) {
   10.10          stringpair_t *_sp = stringpair_dup(_s2->value);
   10.11          if (_sp == NULL)
   10.12              return NULL;
   10.13 @@ -180,8 +179,7 @@
   10.14  {
   10.15      int len = 0;
   10.16  
   10.17 -    const stringpair_list_t *_sl;
   10.18 -    for (_sl = stringpair_list; _sl && _sl->value; _sl = _sl->next)
   10.19 +    for (const stringpair_list_t *_sl = stringpair_list; _sl && _sl->value; _sl = _sl->next)
   10.20          len++;
   10.21  
   10.22      return len;
   10.23 @@ -203,8 +201,7 @@
   10.24  {
   10.25      assert(key);
   10.26  
   10.27 -    stringpair_list_t *_l;
   10.28 -    for (_l = stringpair_list; _l; _l = _l->next) {
   10.29 +    for (stringpair_list_t *_l = stringpair_list; _l; _l = _l->next) {
   10.30          if (strcoll(key, _l->value->key) == 0)
   10.31              return _l;
   10.32      }
    11.1 --- a/src/sync_actions.c	Thu Jan 26 18:19:09 2017 +0100
    11.2 +++ b/src/sync_actions.c	Thu Jan 26 19:07:45 2017 +0100
    11.3 @@ -259,7 +259,7 @@
    11.4      for (identity_list *il = group_keys; il && il->ident; il = il->next) {
    11.5  
    11.6          // Check that identity isn't excluded from sync.
    11.7 -        pEp_identity *stored_identity;
    11.8 +        pEp_identity *stored_identity = NULL;
    11.9          status = get_identity(session, il->ident->address, PEP_OWN_USERID,
   11.10                  &stored_identity);
   11.11          if (status == PEP_STATUS_OK) {