From 31d5c3897686a2e2055a2f14acb76fec1f5c6305 Mon Sep 17 00:00:00 2001 From: Jeffrey Altman Date: Mon, 24 Jan 2022 10:14:17 -0500 Subject: [PATCH] lib/hcrypto: mpz2BN return NULL if mp_ubin_size(s) returns zero If mp_ubin_size(s) returns zero then mp_to_ubin() will fail and not return MP_OKAY. If MP_OKAY is not returned, NULL is returned to the caller of mpz2BN(). This change avoids the unnecessary memory allocation and function calls. It also removes a dereference after null warning from coverity. Change-Id: I52ff2c166964e41cb4eef1dac637904bf2bf13bf --- lib/hcrypto/rsa-ltm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/hcrypto/rsa-ltm.c b/lib/hcrypto/rsa-ltm.c index 28515195d..1d5b73e60 100644 --- a/lib/hcrypto/rsa-ltm.c +++ b/lib/hcrypto/rsa-ltm.c @@ -456,8 +456,11 @@ mpz2BN(mp_int *s) void *p; size = mp_ubin_size(s); + if (size == 0) + return NULL; + p = malloc(size); - if (p == NULL && size != 0) + if (p == NULL) return NULL; ret = mp_to_ubin(s, p, SIZE_MAX, NULL); -- 2.11.4.GIT