src/pgp_sequoia.c
branchsync
changeset 4221 9016d0c7b5c6
parent 4217 738d4cbc5af5
child 4234 b720642cd9e0
     1.1 --- a/src/pgp_sequoia.c	Mon Nov 25 11:00:36 2019 +0100
     1.2 +++ b/src/pgp_sequoia.c	Mon Nov 25 11:04:53 2019 +0100
     1.3 @@ -12,6 +12,7 @@
     1.4  #include <limits.h>
     1.5  #include <sys/stat.h>
     1.6  #include <sys/types.h>
     1.7 +#include <stdlib.h>
     1.8  
     1.9  #include "wrappers.h"
    1.10  
    1.11 @@ -122,6 +123,24 @@
    1.12  }
    1.13  #endif
    1.14  
    1.15 +/* This is reallocarray taken from OpenBSD. See README.md for licensing. */
    1.16 +/* Symbols are renamed for clashes, not to hide source. */
    1.17 +/*
    1.18 + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
    1.19 + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
    1.20 + */
    1.21 +#define PEP_MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
    1.22 +static void* _pEp_reallocarray(void *optr, size_t nmemb, size_t size)
    1.23 +{
    1.24 +    if ((nmemb >= PEP_MUL_NO_OVERFLOW || size >= PEP_MUL_NO_OVERFLOW) &&
    1.25 +        nmemb > 0 && SIZE_MAX / nmemb < size) {
    1.26 +            errno = ENOMEM;
    1.27 +            return NULL;
    1.28 +    }
    1.29 +    return realloc(optr, size * nmemb);
    1.30 +}
    1.31 +
    1.32 +
    1.33  PEP_STATUS pgp_config_cipher_suite(PEP_SESSION session,
    1.34          PEP_CIPHER_SUITE suite)
    1.35  {
    1.36 @@ -1790,13 +1809,13 @@
    1.37                  assert(recipient_alloc > 0);
    1.38                  recipient_alloc *= 2;
    1.39  
    1.40 -                void *t = reallocarray(recipient_keys, recipient_alloc,
    1.41 +                void *t = _pEp_reallocarray(recipient_keys, recipient_alloc,
    1.42                                         sizeof(*recipient_keys));
    1.43                  if (! t)
    1.44                      ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.45                  recipient_keys = t;
    1.46  
    1.47 -                t = reallocarray(recipients, recipient_alloc,
    1.48 +                t = _pEp_reallocarray(recipients, recipient_alloc,
    1.49                                   sizeof(*recipients));
    1.50                  if (! t)
    1.51                      ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");