ENGINE-427: added Claudio's fix ENGINE-427
authorKrista Bennett <krista@pep-project.org>
Thu, 24 May 2018 10:46:53 +0200
branchENGINE-427
changeset 269082d925538dd1
parent 2688 a34d253c9292
child 2692 dfe058245ed8
child 2693 f78c4741405e
ENGINE-427: added Claudio's fix
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Thu May 24 05:48:04 2018 +0200
     1.2 +++ b/src/pgp_gpg.c	Thu May 24 10:46:53 2018 +0200
     1.3 @@ -22,6 +22,17 @@
     1.4      stringlist_t *_v;
     1.5      unsigned int i;
     1.6      unsigned int found = 0;
     1.7 +    bool eof_nl = 0;
     1.8 +    char * rest;
     1.9 +    char * token;
    1.10 +    char * s;
    1.11 +    const char* line_end;
    1.12 +
    1.13 +#ifdef WIN32
    1.14 +    line_end = "\r\n";
    1.15 +#else
    1.16 +    line_end = "\n";
    1.17 +#endif    
    1.18  
    1.19      FILE *f = Fopen(config_file_path, "r");
    1.20      if (f == NULL && errno == ENOMEM)
    1.21 @@ -29,7 +40,6 @@
    1.22  
    1.23      if (f != NULL) {
    1.24          int length = stringlist_length(keys);
    1.25 -        unsigned int n = (1 << length) - 1;
    1.26  
    1.27          // make sure we 1) have the same number of keys and values
    1.28          // and 2) we don't have more key/value pairs than
    1.29 @@ -44,31 +54,26 @@
    1.30              return false;
    1.31          }
    1.32  
    1.33 -        do {
    1.34 -            char * s = Fgets(buf, MAX_LINELENGTH, f);
    1.35 -            if (!feof(f)) {
    1.36 -                assert(s);
    1.37 -                if (s == NULL)
    1.38 -                    return false;
    1.39 -
    1.40 -                if (s && !feof(f)) {
    1.41 -                    char * rest;
    1.42 -                    char * t = strtok_r(s, " ", &rest);
    1.43 -                    for (i = 1, _k = keys, _v = values; _k != NULL;
    1.44 -                            _k = _k->next, _v = _v->next, i <<= 1) {
    1.45 -                        if (t && strncmp(t, _k->value, strlen(_k->value)) == 0)
    1.46 -                            found |= i;
    1.47 -
    1.48 -                        if (i == n) {
    1.49 -                            r = Fclose(f);
    1.50 -                            if (r != 0)
    1.51 -                                return false;
    1.52 -                            return true;
    1.53 -                        }
    1.54 -                    }
    1.55 +        while ((s = Fgets(buf, MAX_LINELENGTH, f))) {
    1.56 +            token = strtok_r(s, " \t\r\n", &rest);
    1.57 +            for (_k = keys, _v = values, i = 1;
    1.58 +                 _k != NULL;
    1.59 +                 _k = _k->next, _v = _v->next, i <<= 1) {
    1.60 +
    1.61 +                if (((found & i) != i) && token &&
    1.62 +                    (strncmp(token, _k->value, strlen(_k->value)) == 0)) {
    1.63 +                    found |= i;
    1.64 +                    break;
    1.65                  }
    1.66              }
    1.67 -        } while (!feof(f));
    1.68 +            if (feof(f)) {
    1.69 +                eof_nl = 1;
    1.70 +                break;
    1.71 +            }
    1.72 +        }
    1.73 +
    1.74 +        if (!s && ferror(f))
    1.75 +            return false;
    1.76          f = Freopen(config_file_path, "a", f);
    1.77      }
    1.78      else {
    1.79 @@ -78,13 +83,16 @@
    1.80      assert(f);
    1.81      if (f == NULL)
    1.82          return false;
    1.83 +    
    1.84 +    if (eof_nl)
    1.85 +        r = Fprintf(f, line_end);
    1.86  
    1.87      for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
    1.88              _v = _v->next, i <<= 1) {
    1.89          if ((found & i) == 0) {
    1.90 -            r = Fprintf(f, "%s %s\n", _k->value, _v->value);
    1.91 +            r = Fprintf(f, "%s %s%s", _k->value, _v->value, line_end);
    1.92              assert(r >= 0);
    1.93 -            if(r<0)
    1.94 +            if (r < 0)
    1.95                  return false;
    1.96          }
    1.97      }