Prevent crash when /home/pepdev env var isn't set
authorEdouard Tisserant
Wed, 02 Sep 2015 13:47:52 +0200
changeset 34668a5a5e22905
parent 345 9e129a270a46
child 347 098632924bd2
Prevent crash when /home/pepdev env var isn't set
src/platform_unix.c
     1.1 --- a/src/platform_unix.c	Mon Aug 31 16:34:18 2015 +0200
     1.2 +++ b/src/platform_unix.c	Wed Sep 02 13:47:52 2015 +0200
     1.3 @@ -62,17 +62,23 @@
     1.4      static bool done = false;
     1.5  
     1.6      if (!done) {
     1.7 -        char *p = local_stpncpy(buffer, getenv("HOME"), MAX_PATH);
     1.8 -        size_t len = MAX_PATH - (p - buffer) - 2;
     1.9 +        char *home_env;
    1.10 +        if(home_env = getenv("HOME")){
    1.11 +            char *p = local_stpncpy(buffer, home_env, MAX_PATH);
    1.12 +            size_t len = MAX_PATH - (p - buffer) - 2;
    1.13  
    1.14 -        if (len < strlen(LOCAL_DB_FILENAME)) {
    1.15 -            assert(0);
    1.16 +            if (len < strlen(LOCAL_DB_FILENAME)) {
    1.17 +                assert(0);
    1.18 +                return NULL;
    1.19 +            }
    1.20 +
    1.21 +            *p++ = '/';
    1.22 +            strncpy(p, LOCAL_DB_FILENAME, len);
    1.23 +            done = true;
    1.24 +        }else{
    1.25              return NULL;
    1.26          }
    1.27  
    1.28 -        *p++ = '/';
    1.29 -        strncpy(p, LOCAL_DB_FILENAME, len);
    1.30 -        done = true;
    1.31      }
    1.32      return buffer;
    1.33  }
    1.34 @@ -90,6 +96,7 @@
    1.35          char *p;
    1.36          size_t len;
    1.37          char *gpg_home_env = getenv("GNUPGHOME");
    1.38 +        char *home_env = getenv("HOME");
    1.39  
    1.40          if(gpg_home_env){
    1.41  
    1.42 @@ -102,9 +109,9 @@
    1.43                  return false;
    1.44              }
    1.45  
    1.46 -        }else{
    1.47 +        }else if(home_env){
    1.48  
    1.49 -            p = local_stpncpy(path, getenv("HOME"), MAX_PATH);
    1.50 +            p = local_stpncpy(path, home_env, MAX_PATH);
    1.51              len = MAX_PATH - (p - path) - 3;
    1.52  
    1.53              if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
    1.54 @@ -117,6 +124,11 @@
    1.55              strncpy(p, gpg_conf_path, len);
    1.56              p += strlen(gpg_conf_path);
    1.57              len -= strlen(gpg_conf_path) - 1;
    1.58 +
    1.59 +        }else{
    1.60 +
    1.61 +            assert(0);
    1.62 +            return false;
    1.63          }
    1.64  
    1.65          strncpy(dirname, path, MAX_PATH);