Prevent NULL ptr accesses due to Balloc out of memory
- add new eBalloc macro to mprec.h which calls Balloc and aborts if Balloc fails due to out of memory - change mprec.c functions that use Balloc without checking to use eBalloc instead - fix dtoa.c to use eBalloc
This commit is contained in:
		| @@ -423,7 +423,7 @@ _dtoa_r (struct _reent *ptr, | |||||||
|   for (_REENT_MP_RESULT_K(ptr) = 0; sizeof (_Bigint) - sizeof (__ULong) + j <= i; |   for (_REENT_MP_RESULT_K(ptr) = 0; sizeof (_Bigint) - sizeof (__ULong) + j <= i; | ||||||
|        j <<= 1) |        j <<= 1) | ||||||
|     _REENT_MP_RESULT_K(ptr)++; |     _REENT_MP_RESULT_K(ptr)++; | ||||||
|   _REENT_MP_RESULT(ptr) = Balloc (ptr, _REENT_MP_RESULT_K(ptr)); |   _REENT_MP_RESULT(ptr) = eBalloc (ptr, _REENT_MP_RESULT_K(ptr)); | ||||||
|   s = s0 = (char *) _REENT_MP_RESULT(ptr); |   s = s0 = (char *) _REENT_MP_RESULT(ptr); | ||||||
|  |  | ||||||
|   if (ilim >= 0 && ilim <= Quick_max && try_quick) |   if (ilim >= 0 && ilim <= Quick_max && try_quick) | ||||||
| @@ -743,7 +743,7 @@ _dtoa_r (struct _reent *ptr, | |||||||
|       mlo = mhi; |       mlo = mhi; | ||||||
|       if (spec_case) |       if (spec_case) | ||||||
| 	{ | 	{ | ||||||
| 	  mhi = Balloc (ptr, mhi->_k); | 	  mhi = eBalloc (ptr, mhi->_k); | ||||||
| 	  Bcopy (mhi, mlo); | 	  Bcopy (mhi, mlo); | ||||||
| 	  mhi = lshift (ptr, mhi, Log2P); | 	  mhi = lshift (ptr, mhi, Log2P); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -178,7 +178,7 @@ multadd (struct _reent *ptr, | |||||||
|     { |     { | ||||||
|       if (wds >= b->_maxwds) |       if (wds >= b->_maxwds) | ||||||
| 	{ | 	{ | ||||||
| 	  b1 = Balloc (ptr, b->_k + 1); | 	  b1 = eBalloc (ptr, b->_k + 1); | ||||||
| 	  Bcopy (b1, b); | 	  Bcopy (b1, b); | ||||||
| 	  Bfree (ptr, b); | 	  Bfree (ptr, b); | ||||||
| 	  b = b1; | 	  b = b1; | ||||||
| @@ -203,11 +203,11 @@ s2b (struct _reent * ptr, | |||||||
|   x = (nd + 8) / 9; |   x = (nd + 8) / 9; | ||||||
|   for (k = 0, y = 1; x > y; y <<= 1, k++); |   for (k = 0, y = 1; x > y; y <<= 1, k++); | ||||||
| #ifdef Pack_32 | #ifdef Pack_32 | ||||||
|   b = Balloc (ptr, k); |   b = eBalloc (ptr, k); | ||||||
|   b->_x[0] = y9; |   b->_x[0] = y9; | ||||||
|   b->_wds = 1; |   b->_wds = 1; | ||||||
| #else | #else | ||||||
|   b = Balloc (ptr, k + 1); |   b = eBalloc (ptr, k + 1); | ||||||
|   b->_x[0] = y9 & 0xffff; |   b->_x[0] = y9 & 0xffff; | ||||||
|   b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1; |   b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1; | ||||||
| #endif | #endif | ||||||
| @@ -317,7 +317,7 @@ i2b (struct _reent * ptr, int i) | |||||||
| { | { | ||||||
|   _Bigint *b; |   _Bigint *b; | ||||||
|  |  | ||||||
|   b = Balloc (ptr, 1); |   b = eBalloc (ptr, 1); | ||||||
|   b->_x[0] = i; |   b->_x[0] = i; | ||||||
|   b->_wds = 1; |   b->_wds = 1; | ||||||
|   return b; |   return b; | ||||||
| @@ -346,7 +346,7 @@ mult (struct _reent * ptr, _Bigint * a, _Bigint * b) | |||||||
|   wc = wa + wb; |   wc = wa + wb; | ||||||
|   if (wc > a->_maxwds) |   if (wc > a->_maxwds) | ||||||
|     k++; |     k++; | ||||||
|   c = Balloc (ptr, k); |   c = eBalloc (ptr, k); | ||||||
|   for (x = c->_x, xa = x + wc; x < xa; x++) |   for (x = c->_x, xa = x + wc; x < xa; x++) | ||||||
|     *x = 0; |     *x = 0; | ||||||
|   xa = a->_x; |   xa = a->_x; | ||||||
| @@ -470,7 +470,7 @@ lshift (struct _reent * ptr, _Bigint * b, int k) | |||||||
|   n1 = n + b->_wds + 1; |   n1 = n + b->_wds + 1; | ||||||
|   for (i = b->_maxwds; n1 > i; i <<= 1) |   for (i = b->_maxwds; n1 > i; i <<= 1) | ||||||
|     k1++; |     k1++; | ||||||
|   b1 = Balloc (ptr, k1); |   b1 = eBalloc (ptr, k1); | ||||||
|   x1 = b1->_x; |   x1 = b1->_x; | ||||||
|   for (i = 0; i < n; i++) |   for (i = 0; i < n; i++) | ||||||
|     *x1++ = 0; |     *x1++ = 0; | ||||||
| @@ -559,7 +559,7 @@ diff (struct _reent * ptr, | |||||||
|   i = cmp (a, b); |   i = cmp (a, b); | ||||||
|   if (!i) |   if (!i) | ||||||
|     { |     { | ||||||
|       c = Balloc (ptr, 0); |       c = eBalloc (ptr, 0); | ||||||
|       c->_wds = 1; |       c->_wds = 1; | ||||||
|       c->_x[0] = 0; |       c->_x[0] = 0; | ||||||
|       return c; |       return c; | ||||||
| @@ -573,7 +573,7 @@ diff (struct _reent * ptr, | |||||||
|     } |     } | ||||||
|   else |   else | ||||||
|     i = 0; |     i = 0; | ||||||
|   c = Balloc (ptr, a->_k); |   c = eBalloc (ptr, a->_k); | ||||||
|   c->_sign = i; |   c->_sign = i; | ||||||
|   wa = a->_wds; |   wa = a->_wds; | ||||||
|   xa = a->_x; |   xa = a->_x; | ||||||
| @@ -775,9 +775,9 @@ d2b (struct _reent * ptr, | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef Pack_32 | #ifdef Pack_32 | ||||||
|   b = Balloc (ptr, 1); |   b = eBalloc (ptr, 1); | ||||||
| #else | #else | ||||||
|   b = Balloc (ptr, 2); |   b = eBalloc (ptr, 2); | ||||||
| #endif | #endif | ||||||
|   x = b->_x; |   x = b->_x; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -30,6 +30,7 @@ | |||||||
| #include <math.h> | #include <math.h> | ||||||
| #include <float.h> | #include <float.h> | ||||||
| #include <errno.h> | #include <errno.h> | ||||||
|  | #include <assert.h> | ||||||
| #include <sys/config.h> | #include <sys/config.h> | ||||||
| #include <sys/types.h> | #include <sys/types.h> | ||||||
| #include "../locale/setlocale.h" | #include "../locale/setlocale.h" | ||||||
| @@ -340,6 +341,13 @@ typedef struct _Bigint _Bigint; | |||||||
| #define copybits 	__copybits | #define copybits 	__copybits | ||||||
| #define hexnan	__hexnan | #define hexnan	__hexnan | ||||||
|  |  | ||||||
|  | #define eBalloc(__reent_ptr, __len) ({ \ | ||||||
|  |    void *__ptr = Balloc(__reent_ptr, __len); \ | ||||||
|  |    if (__ptr == NULL) \ | ||||||
|  |      __assert_func(__FILE__, __LINE__, (char *)0, "Balloc succeeded"); \ | ||||||
|  |    __ptr; \ | ||||||
|  |    }) | ||||||
|  |     | ||||||
| #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) | #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) | ||||||
| #define __get_hexdig(x) __hexdig[x] /* NOTE: must evaluate arg only once */ | #define __get_hexdig(x) __hexdig[x] /* NOTE: must evaluate arg only once */ | ||||||
| #else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) */ | #else /* !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && !defined(_SMALL_HEXDIG) */ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user