ENGINE-451: clearing up some runtime errors. ENGINE-451
authorKrista Bennett <krista@pep-project.org>
Tue, 14 Aug 2018 13:44:12 +0200
branchENGINE-451
changeset 2825aa81abad2215
parent 2824 34863e77a260
child 2826 07c080d73a63
ENGINE-451: clearing up some runtime errors.
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Tue Aug 14 10:01:15 2018 +0200
     1.2 +++ b/src/pgp_gpg.c	Tue Aug 14 13:44:12 2018 +0200
     1.3 @@ -27,6 +27,10 @@
     1.4  
     1.5  // This is in response to ENGINE-427. We should NOT be aiming to keep this here.
     1.6  bool quickfix_config(stringlist_t* keys, const char* config_file_path) {
     1.7 +    
     1.8 +    if (!keys || !config_file_path)
     1.9 +        return false;
    1.10 +        
    1.11      static char buf[MAX_LINELENGTH];
    1.12      size_t num_keys = stringlist_length(keys);
    1.13  
    1.14 @@ -41,18 +45,22 @@
    1.15      char* backup_file_path = NULL;
    1.16      size_t backup_file_path_baselen = strlen(config_file_path);
    1.17      FILE *backup_file = 0;
    1.18 +    FILE *f;
    1.19 +    FILE *temp_config_file;
    1.20 +
    1.21      int ret;
    1.22      int found = 0;
    1.23      int i;
    1.24      char* temp_config_file_path = NULL;
    1.25      char* s = NULL;
    1.26 +
    1.27      stringlist_t* _k;
    1.28      stringlist_t* lines = new_stringlist(NULL);
    1.29 -    FILE *f;
    1.30 -    FILE *temp_config_file;
    1.31      stringlist_t* cur_string;
    1.32 +
    1.33      bool status = false;
    1.34      str_ptr_and_bit* found_keys = NULL;
    1.35 +
    1.36  #ifdef WIN32
    1.37      WIN32_FIND_DATA FindFileData;
    1.38      HANDLE handle;
    1.39 @@ -63,13 +71,13 @@
    1.40  #endif
    1.41  
    1.42      // If we bork it up somehow, we don't go beyond 100 tries...
    1.43 -    for (int nr = 0; nr < 99; nr++) {
    1.44 +    for (int nr = 0; nr < 100; nr++) {
    1.45          backup_file_path = (char*)calloc(backup_file_path_baselen + 12, 1);  // .99.pep.old\0
    1.46          ret = snprintf(backup_file_path, backup_file_path_baselen + 12,
    1.47                                          "%s.%d.pep.bkp", config_file_path, nr);
    1.48          assert(ret >= 0);  // snprintf(2)
    1.49          if (ret < 0) {
    1.50 -            goto quickfix_error;  // frees backup_file_path
    1.51 +            goto pep_free;  // frees backup_file_path
    1.52          }
    1.53  
    1.54  #ifdef WIN32
    1.55 @@ -98,17 +106,17 @@
    1.56      }
    1.57  
    1.58      if (!backup_file_path)
    1.59 -        goto quickfix_error;
    1.60 +        goto pep_free;
    1.61  
    1.62      if (!backup_file)
    1.63 -        goto quickfix_error;
    1.64 +        goto pep_free;
    1.65  
    1.66      // Open original file, parse it, and meanwhile write a backup copy
    1.67  
    1.68      f = Fopen(config_file_path, "rb");
    1.69  
    1.70      if (f == NULL)
    1.71 -        goto quickfix_error;
    1.72 +        goto pep_free;
    1.73  
    1.74      ret = Fprintf(backup_file, "# Backup created by pEp.%s"
    1.75                                 "# If GnuPG and pEp work smoothly this file may safely be removed.%s%s",
    1.76 @@ -125,7 +133,7 @@
    1.77          if (ret < 0) {
    1.78              free(found_keys);
    1.79              found_keys = NULL;
    1.80 -            goto quickfix_error;
    1.81 +            goto pep_free;
    1.82          }
    1.83  
    1.84          char* rest;
    1.85 @@ -139,6 +147,8 @@
    1.86  
    1.87          if (*line_token == '#' || *line_token == '\0') {
    1.88              stringlist_add(lines, strdup(line_token));
    1.89 +            free(found_keys);
    1.90 +            found_keys = NULL;
    1.91              continue;
    1.92          }
    1.93  
    1.94 @@ -283,44 +293,39 @@
    1.95          free(found_keys);
    1.96          found_keys = NULL;
    1.97      } // End of file
    1.98 -
    1.99 +    
   1.100      // Now do the failsafe writing dance
   1.101  
   1.102      ret = Fclose(f);
   1.103      assert(ret == 0);
   1.104      if (ret != 0)
   1.105 -        goto quickfix_error;
   1.106 +        goto pep_free;
   1.107  
   1.108      ret = Fclose(backup_file);
   1.109      assert(ret == 0);
   1.110      if (ret != 0)
   1.111 -        goto quickfix_error;
   1.112 +        goto pep_free;
   1.113  
   1.114      // 2. Write the new config file to a temporary file in the same directory
   1.115      assert(backup_file_path_baselen > 0);
   1.116      if (backup_file_path_baselen <= 0)
   1.117 -        goto quickfix_error;
   1.118 +        goto pep_free;
   1.119  
   1.120      temp_config_file_path = (char*)calloc(backup_file_path_baselen + 8, 1);  // .XXXXXX\0
   1.121      ret = snprintf(temp_config_file_path, backup_file_path_baselen + 8, "%s.XXXXXX", config_file_path);
   1.122 +
   1.123      assert(ret >= 0);
   1.124 -
   1.125      if (ret < 0)
   1.126 -        goto quickfix_error;
   1.127 +        goto pep_free;
   1.128  
   1.129      int temp_config_filedesc = Mkstemp(temp_config_file_path);
   1.130      if (temp_config_filedesc == -1)
   1.131 -        goto quickfix_error;
   1.132 -
   1.133 -    temp_config_file = Fdopen(temp_config_filedesc, "w");    // no "b" in fdopen() is documentend, use freopen()
   1.134 -    assert(temp_config_file != NULL);
   1.135 +        goto pep_free;
   1.136 +
   1.137 +    temp_config_file = Fdopen(temp_config_filedesc, "w");    // "b" in fdopen() IS documented.
   1.138 +    assert(temp_config_file != NULL);                        // And "w" is sufficient.
   1.139      if (temp_config_file == NULL)
   1.140 -        goto quickfix_error;
   1.141 -
   1.142 -    temp_config_file = Freopen(config_file_path, "wb", temp_config_file);
   1.143 -    assert(temp_config_file != NULL);
   1.144 -    if (temp_config_file == NULL)
   1.145 -        goto quickfix_error;
   1.146 +        goto pep_free;
   1.147  
   1.148      ret = Fprintf(temp_config_file, "# File re-created by pEp%s"
   1.149                                      "# See backup in '%s'%s%s", line_end,
   1.150 @@ -328,63 +333,49 @@
   1.151                                                                  line_end, line_end);
   1.152      assert(ret >= 0);
   1.153      if (ret < 0)
   1.154 -        goto quickfix_error;
   1.155 +        goto pep_free;
   1.156          
   1.157      for (cur_string = lines; cur_string; cur_string = cur_string->next) {
   1.158          assert(cur_string->value != NULL);
   1.159          ret = Fprintf(temp_config_file, "%s%s", cur_string->value, line_end);
   1.160          assert(ret >= 0);
   1.161          if (ret < 0)
   1.162 -            goto quickfix_error;
   1.163 +            goto pep_free;
   1.164      }
   1.165  
   1.166      ret = Fclose(temp_config_file);
   1.167      assert(ret == 0);
   1.168      if (ret != 0)
   1.169 -        goto quickfix_error;
   1.170 +        goto pep_free;
   1.171  
   1.172  #ifdef WIN32
   1.173      ret = !(0 == ReplaceFile(config_file_path, temp_config_file_path, NULL, 0, NULL, NULL));
   1.174      assert(ret == 0);
   1.175      if (ret != 0)
   1.176 -        goto quickfix_error;
   1.177 +        goto pep_free;
   1.178  
   1.179      ret = unlink(temp_config_file_path);
   1.180  #else
   1.181      ret = rename(config_file_path, temp_config_file_path);
   1.182  #endif
   1.183 +
   1.184      assert(ret == 0);
   1.185      if (ret != 0)
   1.186 -        goto quickfix_error;
   1.187 +        goto pep_free;
   1.188  
   1.189      free(temp_config_file_path);
   1.190      temp_config_file_path = NULL;
   1.191  
   1.192      status = true;
   1.193  
   1.194 +    assert(found_keys == NULL);
   1.195 +    
   1.196 +pep_free:
   1.197      free(backup_file_path);
   1.198 -
   1.199 -    goto quickfix_success;
   1.200 -
   1.201 -
   1.202 -quickfix_error:
   1.203 -
   1.204 -    assert(status == false);
   1.205 -
   1.206 -    free(backup_file_path);
   1.207 -
   1.208 -
   1.209 -quickfix_success:
   1.210 -
   1.211 -    assert(found_keys == NULL);
   1.212 -
   1.213 -    if (temp_config_file_path)
   1.214 -        free(temp_config_file_path);
   1.215 -
   1.216 +    free(temp_config_file_path);
   1.217      free_stringlist(lines);
   1.218  
   1.219      return status;
   1.220 -
   1.221  }
   1.222  
   1.223  static bool ensure_config_values(stringlist_t *keys, stringlist_t *values, const char* config_file_path)