Fixed off-by-one errors. Made test a little less fragile. netpgp functions work as desired. ENGINE-73
authorKrista Grothoff <krista@pep-project.org>
Tue, 16 Aug 2016 15:37:19 +0200
branchENGINE-73
changeset 102233885fd68114
parent 1021 90d21813bfce
child 1023 fbaab7b6d87b
Fixed off-by-one errors. Made test a little less fragile. netpgp functions work as desired.
src/pgp_netpgp.c
src/pgp_netpgp.h
test/pgp_list_keys_test.cc
     1.1 --- a/src/pgp_netpgp.c	Tue Aug 16 13:03:51 2016 +0200
     1.2 +++ b/src/pgp_netpgp.c	Tue Aug 16 15:37:19 2016 +0200
     1.3 @@ -203,9 +203,15 @@
     1.4      unsigned armoured = 0;
     1.5      regex_t r;
     1.6      regcomp(&r, pattern, REG_EXTENDED|REG_NOSUB);
     1.7 +#ifdef __APPLE__
     1.8      if (regnexec(&r, buf, size, 0, NULL, 0) == 0) {
     1.9          armoured = 1;
    1.10      }
    1.11 +#else
    1.12 +    if (regexec(&r, buf, 0, NULL, 0) == 0) {
    1.13 +        armoured = 1;
    1.14 +    }
    1.15 +#endif
    1.16      regfree(&r);
    1.17      return armoured;
    1.18  }
    1.19 @@ -1669,7 +1675,10 @@
    1.20      
    1.21      // Primitive email extraction
    1.22      at = strrchr(src,'@');
    1.23 -        
    1.24 +    
    1.25 +    char* name_str = NULL;
    1.26 +    char* email_str = NULL;
    1.27 +    
    1.28      if (at) {
    1.29          // Go back until we hit a space, a '<', or the start of the string
    1.30          for (begin = at; begin >= src && *begin != ' ' && *begin != '<'; begin--) {
    1.31 @@ -1678,18 +1687,19 @@
    1.32          if (begin != at)
    1.33              begin++; // Ugly
    1.34          else {
    1.35 -            for (end = at; end <= last_char && *end != ' ' && *end != '>'; end++) {
    1.36 +            for (end = at; end < last_char && *end != ' ' && *end != '>'; end++) {
    1.37                  continue;
    1.38              }
    1.39              // Points one char past.
    1.40          }
    1.41          if (begin < at && end > at) {
    1.42              // yay, it's an email address!
    1.43 -            copy_len = end - begin;
    1.44 -            *email = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.45 -            strncpy(email, begin, copy_len);
    1.46 -            *(email + copy_len) = '\0';
    1.47 -            end = (*begin == '<' ? begin - 1 : begin); // if this precedes src, it is checked below
    1.48 +            copy_len = end - begin - 1;
    1.49 +            email_str = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.50 +            strncpy(email_str, begin, copy_len);
    1.51 +            email_str[copy_len] = '\0';
    1.52 +            begin--; // put the beginning back where it was.
    1.53 +            end = (*begin == '<' ? begin : begin + 1); // if this precedes src, it is checked below
    1.54              begin = src;
    1.55          }
    1.56          else {
    1.57 @@ -1700,10 +1710,12 @@
    1.58      }
    1.59      if (begin < end) {
    1.60          copy_len = end - begin;
    1.61 -        *name = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.62 -        strncpy(name, begin, copy_len);
    1.63 -        *(name + copy_len) = '\0';
    1.64 +        name_str = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.65 +        strncpy(name_str, begin, copy_len);
    1.66 +        name_str[copy_len] = '\0';
    1.67      }
    1.68 +    *email = email_str;
    1.69 +    *name = name_str;
    1.70  }
    1.71  
    1.72  PEP_STATUS pgp_list_keys(
     2.1 --- a/src/pgp_netpgp.h	Tue Aug 16 13:03:51 2016 +0200
     2.2 +++ b/src/pgp_netpgp.h	Tue Aug 16 15:37:19 2016 +0200
     2.3 @@ -30,6 +30,10 @@
     2.4          PEP_SESSION session, const char *pattern, stringlist_t **keylist
     2.5      );
     2.6  
     2.7 +PEP_STATUS pgp_list_keys(
     2.8 +    PEP_SESSION session, identity_list** id_list
     2.9 +    );
    2.10 +
    2.11  PEP_STATUS pgp_generate_keypair(
    2.12          PEP_SESSION session, pEp_identity *identity
    2.13      );
     3.1 --- a/test/pgp_list_keys_test.cc	Tue Aug 16 13:03:51 2016 +0200
     3.2 +++ b/test/pgp_list_keys_test.cc	Tue Aug 16 15:37:19 2016 +0200
     3.3 @@ -10,10 +10,14 @@
     3.4      for ( ; idl != NULL; idl = idl->next) {
     3.5          if (idl->ident) {
     3.6              cout << "Identity:" << endl;
     3.7 -            cout << "\tFPR: " << idl->ident->address << endl;
     3.8 -            cout << "\tAddress: " << idl->ident->fpr << endl; 
     3.9 -            cout << "\tUID: " << idl->ident->user_id << endl;
    3.10 -            cout << "\tName: " << idl->ident->username << endl << endl;
    3.11 +            if (idl->ident->fpr)
    3.12 +                cout << "\tFPR: " << idl->ident->fpr << endl;
    3.13 +            if (idl->ident->address)
    3.14 +                cout << "\tAddress: " << idl->ident->address << endl; 
    3.15 +            if (idl->ident->user_id)
    3.16 +                cout << "\tUID: " << idl->ident->user_id << endl;
    3.17 +            if (idl->ident->username)
    3.18 +                cout << "\tName: " << idl->ident->username << endl << endl;
    3.19          }
    3.20      }
    3.21  }
    3.22 @@ -34,7 +38,6 @@
    3.23      print_id_list(all_the_ids);
    3.24      free_identity_list(all_the_ids);
    3.25  
    3.26 -
    3.27      cout << "calling release()\n";
    3.28      release(session);
    3.29      return 0;