fix for reallocarray sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 25 Nov 2019 11:04:53 +0100
branchsync
changeset 42219016d0c7b5c6
parent 4220 fa9a1793f6e0
child 4222 3247a7e15516
fix for reallocarray
README.md
src/pgp_sequoia.c
     1.1 --- a/README.md	Mon Nov 25 11:00:36 2019 +0100
     1.2 +++ b/README.md	Mon Nov 25 11:04:53 2019 +0100
     1.3 @@ -56,5 +56,11 @@
     1.4  Each file includes a notice near its beginning, that indicates the applicable license.
     1.5  If you wish to license the p≡p Engine under different terms, please contact <mailto:council@pep.foundation>.
     1.6  
     1.7 +_pEp_reallocarray in pgp_sequoia.c is reallocarray from the OpenBSD source. It is 
     1.8 +copyright (c) 2008 Otto Moerbeek <otto@drijf.net> with the following permissions: 
     1.9 +Permission to use, copy, modify, and distribute this software for any
    1.10 +purpose with or without fee is hereby granted, provided that the above
    1.11 +copyright notice and this permission notice appear in all copies.
    1.12 +
    1.13  # Contact
    1.14  The p≡p foundation and the developers of the p≡p Engine can be reached as detailed here: <https://pep.foundation/contact-us/index.html>.
     2.1 --- a/src/pgp_sequoia.c	Mon Nov 25 11:00:36 2019 +0100
     2.2 +++ b/src/pgp_sequoia.c	Mon Nov 25 11:04:53 2019 +0100
     2.3 @@ -12,6 +12,7 @@
     2.4  #include <limits.h>
     2.5  #include <sys/stat.h>
     2.6  #include <sys/types.h>
     2.7 +#include <stdlib.h>
     2.8  
     2.9  #include "wrappers.h"
    2.10  
    2.11 @@ -122,6 +123,24 @@
    2.12  }
    2.13  #endif
    2.14  
    2.15 +/* This is reallocarray taken from OpenBSD. See README.md for licensing. */
    2.16 +/* Symbols are renamed for clashes, not to hide source. */
    2.17 +/*
    2.18 + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
    2.19 + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
    2.20 + */
    2.21 +#define PEP_MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
    2.22 +static void* _pEp_reallocarray(void *optr, size_t nmemb, size_t size)
    2.23 +{
    2.24 +    if ((nmemb >= PEP_MUL_NO_OVERFLOW || size >= PEP_MUL_NO_OVERFLOW) &&
    2.25 +        nmemb > 0 && SIZE_MAX / nmemb < size) {
    2.26 +            errno = ENOMEM;
    2.27 +            return NULL;
    2.28 +    }
    2.29 +    return realloc(optr, size * nmemb);
    2.30 +}
    2.31 +
    2.32 +
    2.33  PEP_STATUS pgp_config_cipher_suite(PEP_SESSION session,
    2.34          PEP_CIPHER_SUITE suite)
    2.35  {
    2.36 @@ -1790,13 +1809,13 @@
    2.37                  assert(recipient_alloc > 0);
    2.38                  recipient_alloc *= 2;
    2.39  
    2.40 -                void *t = reallocarray(recipient_keys, recipient_alloc,
    2.41 +                void *t = _pEp_reallocarray(recipient_keys, recipient_alloc,
    2.42                                         sizeof(*recipient_keys));
    2.43                  if (! t)
    2.44                      ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    2.45                  recipient_keys = t;
    2.46  
    2.47 -                t = reallocarray(recipients, recipient_alloc,
    2.48 +                t = _pEp_reallocarray(recipients, recipient_alloc,
    2.49                                   sizeof(*recipients));
    2.50                  if (! t)
    2.51                      ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");