sequoia: Correctly NUL terminate returned strings sync
authorNeal H. Walfield <neal@pep.foundation>
Tue, 28 May 2019 16:52:49 +0200
branchsync
changeset 37537eabcd28a967
parent 3752 856412ae847e
child 3755 df87c9865aa9
sequoia: Correctly NUL terminate returned strings
src/pgp_sequoia.c
     1.1 --- a/src/pgp_sequoia.c	Tue May 28 15:54:22 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Tue May 28 16:52:49 2019 +0200
     1.3 @@ -1647,6 +1647,11 @@
     1.4      if (write_status != 0)
     1.5          ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Encrypting message");
     1.6  
     1.7 +    pgp_status_t pgp_status = pgp_writer_stack_finalize (&err, ws);
     1.8 +    ws = NULL;
     1.9 +    if (pgp_status != 0)
    1.10 +        ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Flushing writer");
    1.11 +
    1.12      // Add a terminating NUL for naive users
    1.13      void *t = realloc(*stext, *ssize + 1);
    1.14      if (! t)
    1.15 @@ -1655,13 +1660,6 @@
    1.16      (*stext)[*ssize] = 0;
    1.17  
    1.18   out:
    1.19 -    if (ws) {
    1.20 -        pgp_status_t pgp_status = pgp_writer_stack_finalize (&err, ws);
    1.21 -        ws = NULL;
    1.22 -        if (pgp_status != 0)
    1.23 -            ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Flushing writer");
    1.24 -    }
    1.25 -
    1.26      if (signer)
    1.27          pgp_signer_free (signer);
    1.28      if (signing_keypair)
    1.29 @@ -1767,21 +1765,22 @@
    1.30      if (write_status != 0)
    1.31          ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Encrypting message");
    1.32  
    1.33 +    pgp_status_t pgp_status = pgp_writer_stack_finalize (&err, ws);
    1.34 +    ws = NULL;
    1.35 +    if (pgp_status != 0)
    1.36 +        ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Flushing writer");
    1.37 +
    1.38      // Add a terminating NUL for naive users
    1.39      void *t = realloc(*ctext, *csize + 1);
    1.40 -    if (! t)
    1.41 +    if (! t) {
    1.42 +        free(*ctext);
    1.43 +        *ctext = NULL;
    1.44          ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.45 +    }
    1.46      *ctext = t;
    1.47      (*ctext)[*csize] = 0;
    1.48  
    1.49   out:
    1.50 -    if (ws) {
    1.51 -        pgp_status_t pgp_status = pgp_writer_stack_finalize (&err, ws);
    1.52 -        ws = NULL;
    1.53 -        if (pgp_status != 0)
    1.54 -            ERROR_OUT(err, PEP_UNKNOWN_ERROR, "Flushing writer");
    1.55 -    }
    1.56 -
    1.57      if (signer)
    1.58          pgp_signer_free (signer);
    1.59      if (signing_keypair)
    1.60 @@ -2045,6 +2044,7 @@
    1.61      pgp_error_t err = NULL;
    1.62      pgp_tpk_t tpk = NULL;
    1.63      pgp_writer_t armor_writer = NULL;
    1.64 +    pgp_writer_t memory_writer = NULL;
    1.65  
    1.66      assert(session);
    1.67      assert(fpr);
    1.68 @@ -2061,13 +2061,12 @@
    1.69      status = tpk_find_by_fpr_hex(session, fpr, secret, &tpk, NULL);
    1.70      ERROR_OUT(NULL, status, "Looking up TSK for %s", fpr);
    1.71  
    1.72 -    pgp_writer_t memory_writer = pgp_writer_alloc((void **) key_data, size);
    1.73 +    memory_writer = pgp_writer_alloc((void **) key_data, size);
    1.74      if (! memory_writer)
    1.75          ERROR_OUT(NULL, PEP_UNKNOWN_ERROR, "creating memory writer");
    1.76      armor_writer = pgp_armor_writer_new(&err, memory_writer,
    1.77                                          PGP_ARMOR_KIND_PUBLICKEY, NULL, 0);
    1.78      if (! armor_writer) {
    1.79 -        pgp_writer_free(memory_writer);
    1.80          ERROR_OUT(err, PEP_UNKNOWN_ERROR, "creating armored writer");
    1.81      }
    1.82  
    1.83 @@ -2085,6 +2084,15 @@
    1.84      if (armor_writer)
    1.85          pgp_writer_free(armor_writer);
    1.86  
    1.87 +    if (memory_writer) {
    1.88 +        if (status == PEP_STATUS_OK) {
    1.89 +            // Add a trailing NUL.
    1.90 +            pgp_writer_write(NULL, memory_writer, "", 1);
    1.91 +        }
    1.92 +
    1.93 +        pgp_writer_free(memory_writer);
    1.94 +    }
    1.95 +
    1.96      if (tpk)
    1.97          pgp_tpk_free(tpk);
    1.98