calloc: Make sure we always have an overflow-checking calloc() available

This tries to make sure that everywhere in this source tree, we always have
an appropriate version of calloc() (i.e. grub_calloc(), xcalloc(), etc.)
available, and that they all safely check for overflow and return NULL when
it would occur.

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Peter Jones 2020-06-15 12:15:29 -04:00 committed by Daniel Kiper
parent 68708c4503
commit 64e26162eb
7 changed files with 85 additions and 3 deletions

View file

@ -4,6 +4,7 @@
#include <grub/crypto.h>
#include <grub/dl.h>
#include <grub/env.h>
#include <grub/safemath.h>
GRUB_MOD_LICENSE ("GPLv3+");
@ -36,7 +37,10 @@ void *
gcry_xcalloc (size_t n, size_t m)
{
void *ret;
ret = grub_zalloc (n * m);
size_t sz;
if (grub_mul (n, m, &sz))
grub_fatal ("gcry_xcalloc would overflow");
ret = grub_zalloc (sz);
if (!ret)
grub_fatal ("gcry_xcalloc failed");
return ret;
@ -56,7 +60,10 @@ void *
gcry_xcalloc_secure (size_t n, size_t m)
{
void *ret;
ret = grub_zalloc (n * m);
size_t sz;
if (grub_mul (n, m, &sz))
grub_fatal ("gcry_xcalloc would overflow");
ret = grub_zalloc (sz);
if (!ret)
grub_fatal ("gcry_xcalloc failed");
return ret;