Fix *_ctx_new and *_ctx_free implementations

They both will only alloc and free the internal *_ctx structs. Get rid
of the void * argument for new and only pass arg to *_free instead of
the whole lc_*_ctx struct.
This commit is contained in:
Lucas Gabriel Vuotto 2024-06-06 11:45:30 +00:00
parent 0a47025c19
commit f511cddf0d
6 changed files with 17 additions and 18 deletions

11
auth.c
View File

@ -55,7 +55,7 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->ctx_new != NULL) { if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL); ctx->arg = impl->ctx_new();
if (ctx->arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
@ -70,11 +70,10 @@ lc_auth_ctx_new(const struct lc_auth_impl *impl)
void void
lc_auth_ctx_free(struct lc_auth_ctx *ctx) lc_auth_ctx_free(struct lc_auth_ctx *ctx)
{ {
if (ctx != NULL && ctx->impl != NULL && ctx->impl->ctx_free != NULL) if (ctx != NULL) {
ctx->impl->ctx_free(ctx); if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
else { ctx->impl->ctx_free(ctx->arg);
if (ctx != NULL)
free(ctx->arg); free(ctx->arg);
free(ctx);
} }
free(ctx);
} }

2
auth.h
View File

@ -25,7 +25,7 @@ struct lc_auth_impl {
int (*auth)(const uint8_t *, size_t, uint8_t *, size_t *, int (*auth)(const uint8_t *, size_t, uint8_t *, size_t *,
const uint8_t *, size_t); const uint8_t *, size_t);
void *(*ctx_new)(const void *); void *(*ctx_new)(void);
void (*ctx_free)(void *); void (*ctx_free)(void *);
}; };

View File

@ -154,11 +154,12 @@ poly1305_auth(const uint8_t *key, size_t keylen, uint8_t *out, size_t *outlen,
} }
static void * static void *
poly1305_ctx_new(const void *arg) poly1305_ctx_new(void)
{ {
return malloc(sizeof(struct poly1305_ctx)); return malloc(sizeof(struct poly1305_ctx));
} }
static struct lc_auth_impl poly1305_impl = { static struct lc_auth_impl poly1305_impl = {
.init = &poly1305_init, .init = &poly1305_init,
.update = &poly1305_update, .update = &poly1305_update,

View File

@ -87,7 +87,7 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
if (ctx == NULL) if (ctx == NULL)
return NULL; return NULL;
if (impl->ctx_new != NULL) { if (impl->ctx_new != NULL) {
ctx->arg = impl->ctx_new(NULL); ctx->arg = impl->ctx_new();
if (ctx->arg == NULL) { if (ctx->arg == NULL) {
free(ctx); free(ctx);
return NULL; return NULL;
@ -102,11 +102,10 @@ lc_cipher_ctx_new(const struct lc_cipher_impl *impl)
void void
lc_cipher_ctx_free(struct lc_cipher_ctx *ctx) lc_cipher_ctx_free(struct lc_cipher_ctx *ctx)
{ {
if (ctx != NULL && ctx->impl != NULL && ctx->impl->ctx_free != NULL) if (ctx != NULL) {
ctx->impl->ctx_free(ctx); if (ctx->impl != NULL && ctx->impl->ctx_free != NULL)
else { ctx->impl->ctx_free(ctx->arg);
if (ctx != NULL)
free(ctx->arg); free(ctx->arg);
free(ctx);
} }
free(ctx);
} }

View File

@ -35,7 +35,7 @@ struct lc_cipher_impl {
int (*decrypt)(const uint8_t *, size_t, const uint8_t *, size_t, int (*decrypt)(const uint8_t *, size_t, const uint8_t *, size_t,
uint8_t *, size_t *, const uint8_t *, size_t); uint8_t *, size_t *, const uint8_t *, size_t);
void *(*ctx_new)(const void *); void *(*ctx_new)(void);
void (*ctx_free)(void *); void (*ctx_free)(void *);
}; };

View File

@ -179,7 +179,7 @@ chacha20_x(const uint8_t *key, size_t keylen, const uint8_t *iv, size_t ivlen,
} }
static void * static void *
chacha20_ctx_new(const void *arg) chacha20_ctx_new(void)
{ {
return malloc(sizeof(struct chacha20_ctx)); return malloc(sizeof(struct chacha20_ctx));
} }