From 42230d3783013a4d5bccc5508401fbdc73d7b1c6 Mon Sep 17 00:00:00 2001
From: Werner Koch <wk@gnupg.org>
Date: Fri, 20 Sep 2002 07:41:00 +0000
Subject: [PATCH] * mpicoder.c (do_get_buffer): Avoid zero length allocation.
 Checked that all callers behave properly when NBYTES returns 0 as the length
 of the allocated buffer.

---
 mpi/ChangeLog  | 6 ++++++
 mpi/mpicoder.c | 5 ++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/mpi/ChangeLog b/mpi/ChangeLog
index 238f69f31..0a2d05b64 100644
--- a/mpi/ChangeLog
+++ b/mpi/ChangeLog
@@ -1,3 +1,9 @@
+2002-09-20  Werner Koch  <wk@gnupg.org>
+
+	* mpicoder.c (do_get_buffer): Avoid zero length allocation.
+	Checked that all callers behave properly when NBYTES returns 0 as
+	the length of the allocated buffer.
+
 2002-09-10  Werner Koch  <wk@gnupg.org>
 
 	* mpi-bit.c (mpi_normalize): Replaced the check for protected by
diff --git a/mpi/mpicoder.c b/mpi/mpicoder.c
index f796888c8..b05a6f92a 100644
--- a/mpi/mpicoder.c
+++ b/mpi/mpicoder.c
@@ -332,10 +332,13 @@ do_get_buffer( MPI a, unsigned *nbytes, int *sign, int force_secure )
     byte *p, *buffer;
     mpi_limb_t alimb;
     int i;
+    unsigned int n;
 
     if( sign )
 	*sign = a->sign;
-    *nbytes = a->nlimbs * BYTES_PER_MPI_LIMB;
+    *nbytes = n = a->nlimbs * BYTES_PER_MPI_LIMB;
+    if (!n)
+      n++; /* avoid zero length allocation */
     p = buffer = force_secure || mpi_is_secure(a) ? m_alloc_secure( *nbytes)
 						  : m_alloc( *nbytes );