EMPTYSTR fixes
authorKrista Bennett <krista@pep-project.org>
Thu, 18 Jan 2018 16:45:01 +0100
changeset 241471de06af5121
parent 2413 3fc3a94d0820
child 2415 7410f7666976
EMPTYSTR fixes
src/keymanagement.c
test/revoke_regen_attach_test.cc
     1.1 --- a/src/keymanagement.c	Thu Jan 18 15:21:40 2018 +0100
     1.2 +++ b/src/keymanagement.c	Thu Jan 18 16:45:01 2018 +0100
     1.3 @@ -205,7 +205,7 @@
     1.4                                            fpr, 
     1.5                                            ct);
     1.6              free(ident->fpr);
     1.7 -            ident->fpr = strdup("");
     1.8 +            ident->fpr = NULL;
     1.9              ident->comm_type = ct;            
    1.10              status = PEP_KEY_UNSUITABLE;
    1.11          default:
    1.12 @@ -259,7 +259,7 @@
    1.13      
    1.14      PEP_STATUS status = PEP_STATUS_OK;
    1.15  
    1.16 -    if (!stored_identity || !stored_identity->user_id
    1.17 +    if (!stored_identity || EMPTYSTR(stored_identity->user_id)
    1.18          || !is_identity_default || !is_user_default || !is_address_default)
    1.19          return PEP_ILLEGAL_VALUE;
    1.20          
    1.21 @@ -271,9 +271,9 @@
    1.22      char* stored_fpr = stored_identity->fpr;
    1.23      // Input: stored identity retrieved from database
    1.24      // if stored identity contains a default key
    1.25 -    if (stored_fpr) {
    1.26 +    if (!EMPTYSTR(stored_fpr)) {
    1.27          status = validate_fpr(session, stored_identity);    
    1.28 -        if (status == PEP_STATUS_OK && stored_identity->fpr) {
    1.29 +        if (status == PEP_STATUS_OK && !EMPTYSTR(stored_identity->fpr)) {
    1.30              *is_identity_default = *is_address_default = true;
    1.31              return status;
    1.32          }
    1.33 @@ -289,7 +289,7 @@
    1.34      char* user_fpr = NULL;
    1.35      status = get_user_default_key(session, stored_identity->user_id, &user_fpr);
    1.36      
    1.37 -    if (user_fpr) {             
    1.38 +    if (!EMPTYSTR(user_fpr)) {             
    1.39          // There exists a default key for user, so validate
    1.40          stored_identity->fpr = user_fpr;
    1.41          status = validate_fpr(session, stored_identity);
    1.42 @@ -308,7 +308,7 @@
    1.43      
    1.44      status = elect_pubkey(session, stored_identity);
    1.45      if (status == PEP_STATUS_OK) {
    1.46 -        if (stored_identity->fpr)
    1.47 +        if (!EMPTYSTR(stored_identity->fpr))
    1.48              validate_fpr(session, stored_identity);
    1.49      }    
    1.50      else if (status != PEP_KEY_NOT_FOUND && first_reject_status != PEP_KEY_NOT_FOUND) {
    1.51 @@ -376,7 +376,7 @@
    1.52      }
    1.53      free(return_id->fpr);
    1.54      return_id->fpr = NULL;
    1.55 -    if (status == PEP_STATUS_OK && stored_ident->fpr)
    1.56 +    if (status == PEP_STATUS_OK && !EMPTYSTR(stored_ident->fpr))
    1.57          return_id->fpr = strdup(stored_ident->fpr);
    1.58          
    1.59      return_id->comm_type = stored_ident->comm_type;
    1.60 @@ -385,22 +385,25 @@
    1.61      // one, we pull it out of storage if available.
    1.62      // (also, if the input username is "anonymous" and there exists
    1.63      //  a DB username, we replace)
    1.64 -    if (stored_ident->username) {
    1.65 -        if (return_id->username && 
    1.66 +    if (!EMPTYSTR(stored_ident->username)) {
    1.67 +        if (!EMPTYSTR(return_id->username) && 
    1.68              (strcasecmp(return_id->username, "anonymous") == 0)) {
    1.69              free(return_id->username);
    1.70              return_id->username = NULL;
    1.71          }
    1.72 -        if (!return_id->username)
    1.73 +        if (EMPTYSTR(return_id->username)) {
    1.74 +            free(return_id->username);
    1.75              return_id->username = strdup(stored_ident->username);
    1.76 +        }
    1.77      }
    1.78      
    1.79      return_id->me = stored_ident->me;
    1.80      
    1.81      // FIXME: Do we ALWAYS do this? We probably should...
    1.82 -    if (!return_id->user_id)
    1.83 +    if (EMPTYSTR(return_id->user_id)) {
    1.84 +        free(return_id->user_id);
    1.85          return_id->user_id = strdup(stored_ident->user_id);
    1.86 -        
    1.87 +    }    
    1.88      // Call set_identity() to store
    1.89      if ((is_identity_default || is_user_default) &&
    1.90           is_address_default) {                 
    1.91 @@ -457,7 +460,7 @@
    1.92      // Retrieve stored identity information!    
    1.93      pEp_identity* stored_ident = NULL;
    1.94  
    1.95 -    if (identity->user_id) {            
    1.96 +    if (!EMPTYSTR(identity->user_id)) {            
    1.97          // (we're gonna update the trust/fpr anyway, so we use the no-fpr-from-trust-db variant)
    1.98          //      * do get_identity() to retrieve stored identity information
    1.99          status = get_identity_without_trust_check(session, identity->address, identity->user_id, &stored_ident);
   1.100 @@ -481,7 +484,7 @@
   1.101                              
   1.102                              // if usernames match, we replace the userid. Or if the temp username
   1.103                              // is anonymous.
   1.104 -                            if (!this_id->username ||
   1.105 +                            if (EMPTYSTR(this_id->username) ||
   1.106                                  strcasecmp(this_id->username, "anonymous") == 0 ||
   1.107                                  (identity->username && 
   1.108                                   strcasecmp(identity->username, 
   1.109 @@ -531,7 +534,7 @@
   1.110              
   1.111              //  if we only have user_id and address and identity not available
   1.112              //      * return error status (identity not found)
   1.113 -            if (!(identity->username))
   1.114 +            if (EMPTYSTR(identity->username))
   1.115                  status = PEP_CANNOT_FIND_IDENTITY;
   1.116              
   1.117              // Otherwise, if we had user_id, address, and username:
   1.118 @@ -554,7 +557,7 @@
   1.119              //  * Return: created identity
   1.120          }        
   1.121      }
   1.122 -    else if (identity->username) {
   1.123 +    else if (!EMPTYSTR(identity->username)) {
   1.124          /*
   1.125           * Temporary identity information with username supplied
   1.126              * Input: address, username (no others)
     2.1 --- a/test/revoke_regen_attach_test.cc	Thu Jan 18 15:21:40 2018 +0100
     2.2 +++ b/test/revoke_regen_attach_test.cc	Thu Jan 18 16:45:01 2018 +0100
     2.3 @@ -45,9 +45,13 @@
     2.4      key_mistrusted(session, me);
     2.5  
     2.6      cout << "re-generated fingerprint \n";
     2.7 +    free(me->fpr);
     2.8 +    status = myself(session, me);
     2.9 +    assert(status == PEP_STATUS_OK);
    2.10      cout << me->fpr << "\n";
    2.11      
    2.12 -    assert(strcmp(me->fpr, prev_fpr));
    2.13 +    assert(me->fpr);
    2.14 +    assert(strcmp(me->fpr, prev_fpr) != 0);
    2.15      cout << "New fpr is: " << me->fpr;
    2.16      
    2.17      me->fpr = NULL;