Change asprintf with calloc+snprintf sync
authorHussein Kasem <huss@pep-project.org>
Thu, 25 Apr 2019 16:41:02 +0200
branchsync
changeset 35465ecdb4544dc7
parent 3545 713b5fb5f372
child 3547 bb5c8ef9a938
Change asprintf with calloc+snprintf
src/pgp_sequoia.c
     1.1 --- a/src/pgp_sequoia.c	Thu Apr 25 14:17:07 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Thu Apr 25 16:41:02 2019 +0200
     1.3 @@ -79,11 +79,17 @@
     1.4          ERROR_OUT(NULL, PEP_INIT_GPGME_INIT_FAILED, "HOME unset");
     1.5  
     1.6      // Create the DB and initialize it.
     1.7 -    char *path = NULL;
     1.8 -    asprintf(&path, "%s/.pEp_keys.db", home_env);
     1.9 +    size_t path_size = strlen(home_env)+13+1;
    1.10 +    char *path = (char *) calloc(1, path_size);
    1.11 +    assert(path);
    1.12      if (!path)
    1.13          ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.14  
    1.15 +    int r = snprintf(path, path_size, "%s/.pEp_keys.db", home_env);
    1.16 +    assert(r >= 0 && r < path_size);
    1.17 +    if (r < 0)
    1.18 +        ERROR_OUT(NULL, PEP_UNKNOWN_ERROR, "snprintf");
    1.19 +
    1.20      int sqlite_result;
    1.21      sqlite_result = sqlite3_open_v2(path,
    1.22                                      &session->key_db,
    1.23 @@ -1506,9 +1512,16 @@
    1.24      assert(identity->fpr == NULL || identity->fpr[0] == 0);
    1.25      assert(identity->username);
    1.26  
    1.27 -    asprintf(&userid, "%s <%s>", identity->username, identity->address);
    1.28 -    if (! userid)
    1.29 -        ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "asprintf");
    1.30 +    size_t userid_size = strlen(identity->username)+strlen(identity->address)+3+1;
    1.31 +    userid = (char *) calloc(1, userid_size);
    1.32 +    assert(userid);
    1.33 +    if (!userid)
    1.34 +        ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.35 +
    1.36 +    int r = snprintf(userid, userid_size, "%s <%s>", identity->username, identity->address);
    1.37 +    assert(r >= 0 && r < userid_size);
    1.38 +    if (r < 0)
    1.39 +        ERROR_OUT(NULL, PEP_UNKNOWN_ERROR, "snprintf");
    1.40  
    1.41      T("(%s)", userid);
    1.42