diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2021-08-23 11:48:41 +0900 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2021-08-23 11:48:41 +0900 |
commit | 581e04386d875672ba6c3fc4a4232ca72bc46c8c (patch) | |
tree | 2832479019d7d350b8697afc0d1a57f236d7a74f /cipher/md.c | |
parent | c9acca865ba4abaa4443712f87fb8926fe7ad8b7 (diff) | |
download | libgcrypt-581e04386d875672ba6c3fc4a4232ca72bc46c8c.tar.gz |
cipher: Use offsetof for allocation of gcry_md_context.
* cipher/md.c (md_open): Allocate exact space for the allocation and
use offsetof for the calculation.
--
Also, use hd->buf to get the pointer for CTX, so that compiler can
detect it's flexible array (for GCC 11).
GnuPG-bug-id: 5556
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to 'cipher/md.c')
-rw-r--r-- | cipher/md.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/cipher/md.c b/cipher/md.c index ea9ade19..5f1c05b4 100644 --- a/cipher/md.c +++ b/cipher/md.c @@ -452,7 +452,6 @@ md_open (gcry_md_hd_t *h, int algo, unsigned int flags) int secure = !!(flags & GCRY_MD_FLAG_SECURE); int hmac = !!(flags & GCRY_MD_FLAG_HMAC); int bufsize = secure ? 512 : 1024; - struct gcry_md_context *ctx; gcry_md_hd_t hd; size_t n; @@ -470,7 +469,7 @@ md_open (gcry_md_hd_t *h, int algo, unsigned int flags) * * We have to make sure that private is well aligned. */ - n = sizeof (struct gcry_md_handle) + bufsize; + n = offsetof (struct gcry_md_handle, buf) + bufsize; n = ((n + sizeof (PROPERLY_ALIGNED_TYPE) - 1) / sizeof (PROPERLY_ALIGNED_TYPE)) * sizeof (PROPERLY_ALIGNED_TYPE); @@ -485,13 +484,16 @@ md_open (gcry_md_hd_t *h, int algo, unsigned int flags) if (! err) { - hd->ctx = ctx = (void *) ((char *) hd + n); + struct gcry_md_context *ctx; + + ctx = (void *) (hd->buf - offsetof (struct gcry_md_handle, buf) + n); /* Setup the globally visible data (bctl in the diagram).*/ - hd->bufsize = n - sizeof (struct gcry_md_handle) + 1; + hd->ctx = ctx; + hd->bufsize = n - offsetof (struct gcry_md_handle, buf); hd->bufpos = 0; /* Initialize the private data. */ - memset (hd->ctx, 0, sizeof *hd->ctx); + memset (ctx, 0, sizeof *ctx); ctx->magic = secure ? CTX_MAGIC_SECURE : CTX_MAGIC_NORMAL; ctx->actual_handle_size = n + sizeof (struct gcry_md_context); ctx->flags.secure = secure; @@ -637,7 +639,7 @@ md_copy (gcry_md_hd_t ahd, gcry_md_hd_t *b_hd) bhd->ctx = b = (void *) ((char *) bhd + n); /* No need to copy the buffer due to the write above. */ - gcry_assert (ahd->bufsize == (n - sizeof (struct gcry_md_handle) + 1)); + gcry_assert (ahd->bufsize == (n - offsetof (struct gcry_md_handle, buf))); bhd->bufsize = ahd->bufsize; bhd->bufpos = 0; gcry_assert (! ahd->bufpos); |