On Tue, Apr 19, 2022 at 07:35:35PM +0200, Theo Buehler wrote:
> On Tue, Apr 19, 2022 at 07:24:04PM +0200, Claudio Jeker wrote:
> > I tripped over this and this optimisation hurts more then it helps.
> > So lets just create a new EVP_ENCODE_CTX for every base64_decode()
> > call and cleanup at the end of the call.
>
> Should malloc() failure and EVP_ENCODE_CTX_new() failure not be treated
> the same way? Either both return -1 or both err()?
I think in both out-of-memory cases we should err() and not return -1.
I changed my code to do that.
> ok either way
>
> >
> > --
> > :wq Claudio
> >
> > Index: encoding.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/rpki-client/encoding.c,v
> > retrieving revision 1.10
> > diff -u -p -r1.10 encoding.c
> > --- encoding.c 24 Nov 2021 15:24:16 -0000 1.10
> > +++ encoding.c 19 Apr 2022 17:16:32 -0000
> > @@ -96,21 +96,21 @@ int
> > base64_decode(const unsigned char *in, size_t inlen,
> > unsigned char **out, size_t *outlen)
> > {
> > - static EVP_ENCODE_CTX *ctx;
> > - unsigned char *to;
> > + EVP_ENCODE_CTX *ctx;
> > + unsigned char *to = NULL;
> > size_t tolen;
> > int evplen;
> >
> > - if (ctx == NULL && (ctx = EVP_ENCODE_CTX_new()) == NULL)
> > + if ((ctx = EVP_ENCODE_CTX_new()) == NULL)
> > err(1, "EVP_ENCODE_CTX_new");
> >
> > *out = NULL;
> > *outlen = 0;
> >
> > if (base64_decode_len(inlen, &tolen) == -1)
> > - return -1;
> > + goto fail;
> > if ((to = malloc(tolen)) == NULL)
> > - return -1;
> > + goto fail;
> >
> > evplen = tolen;
> > EVP_DecodeInit(ctx);
> > @@ -121,10 +121,13 @@ base64_decode(const unsigned char *in, s
> > goto fail;
> > *outlen += evplen;
> > *out = to;
> > +
> > + EVP_ENCODE_CTX_free(ctx);
> > return 0;
> >
> > fail:
> > free(to);
> > + EVP_ENCODE_CTX_free(ctx);
> > return -1;
> > }
> >
> >
>
--
:wq Claudio