Skip to content

Commit

Permalink
Replace size_t with int and add the check for the EVP_MD_get_size()
Browse files Browse the repository at this point in the history
Replace the type of "digest_size" with int to avoid implicit conversion when it is assigned by EVP_MD_get_size().
Moreover, add the check for the "digest_size".

Fixes: 29ce106 ("Update the demos/README file because it is really old. New demos should provide best practice for API use. Add demonstration for computing a SHA3-512 digest - digest/EVP_MD_demo")
Signed-off-by: Jiasheng Jiang <jiasheng@purdue.edu>

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #23924)

(cherry picked from commit 87e7470)
  • Loading branch information
JiangJias authored and t8m committed Mar 25, 2024
1 parent c6b2290 commit ba3b337
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion demos/digest/BIO_f_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(int argc, char *argv[])
BIO *bio_digest = NULL, *reading = NULL;
EVP_MD *md = NULL;
unsigned char buffer[512];
size_t digest_size;
int digest_size;
char *digest_value = NULL;
int j;

Expand All @@ -68,6 +68,11 @@ int main(int argc, char *argv[])
goto cleanup;
}
digest_size = EVP_MD_get_size(md);
if (digest_size <= 0) {
fprintf(stderr, "EVP_MD_get_size returned invalid size.\n");
goto cleanup;
}

digest_value = OPENSSL_malloc(digest_size);
if (digest_value == NULL) {
fprintf(stderr, "Can't allocate %lu bytes for the digest value.\n", (unsigned long)digest_size);
Expand Down

0 comments on commit ba3b337

Please sign in to comment.