diff options
Diffstat (limited to 'lib/libc/gdtoa/strtodg.c')
-rw-r--r-- | lib/libc/gdtoa/strtodg.c | 111 |
1 files changed, 100 insertions, 11 deletions
diff --git a/lib/libc/gdtoa/strtodg.c b/lib/libc/gdtoa/strtodg.c index 159d3c91bae..a4a422fd507 100644 --- a/lib/libc/gdtoa/strtodg.c +++ b/lib/libc/gdtoa/strtodg.c @@ -80,6 +80,8 @@ increment(Bigint *b) { if (b->wds >= b->maxwds) { b1 = Balloc(b->k+1); + if (b1 == NULL) + return (NULL); Bcopy(b1,b); Bfree(b); b = b1; @@ -154,6 +156,8 @@ set_ones(Bigint *b, int n) if (b->k < k) { Bfree(b); b = Balloc(k); + if (b == NULL) + return (NULL); } k = n >> kshift; if (n &= kmask) @@ -183,6 +187,10 @@ rvOK carry = rv = 0; b = d2b(d, &e, &bdif); + if (b == NULL) { + *irv = STRTOG_NoMemory; + return 1; + } bdif -= nb = fpi->nbits; e += bdif; if (bdif <= 0) { @@ -235,6 +243,10 @@ rvOK if (carry) { inex = STRTOG_Inexhi; b = increment(b); + if (b == NULL) { + *irv = STRTOG_NoMemory; + return 1; + } if ( (j = nb & kmask) !=0) j = ULbits - j; if (hi0bits(b->x[b->wds - 1]) != j) { @@ -245,8 +257,13 @@ rvOK } } } - else if (bdif < 0) + else if (bdif < 0) { b = lshift(b, -bdif); + if (b == NULL) { + *irv = STRTOG_NoMemory; + return 1; + } + } if (e < fpi->emin) { k = fpi->emin - e; e = fpi->emin; @@ -266,6 +283,10 @@ rvOK *irv = STRTOG_Denormal; if (carry) { b = increment(b); + if (b == NULL) { + *irv = STRTOG_NoMemory; + return 1; + } inex = STRTOG_Inexhi | STRTOG_Underflow; } else if (lostbits) @@ -385,6 +406,8 @@ strtodg case 'x': case 'X': irv = gethex(&s, fpi, exp, &rvb, sign); + if (irv == STRTOG_NoMemory) + return (STRTOG_NoMemory); if (irv == STRTOG_NoNumber) { s = s00; sign = 0; @@ -550,9 +573,12 @@ strtodg bd0 = 0; if (nbits <= P && nd <= DBL_DIG) { if (!e) { - if (rvOK(dval(rv), fpi, exp, bits, 1, rd, &irv)) + if (rvOK(dval(rv), fpi, exp, bits, 1, rd, &irv)) { + if (irv == STRTOG_NoMemory) + return (STRTOG_NoMemory); goto ret; } + } else if (e > 0) { if (e <= Ten_pmax) { #ifdef VAX @@ -560,8 +586,11 @@ strtodg #else i = fivesbits[e] + mantbits(dval(rv)) <= P; /* rv = */ rounded_product(dval(rv), tens[e]); - if (rvOK(dval(rv), fpi, exp, bits, i, rd, &irv)) + if (rvOK(dval(rv), fpi, exp, bits, i, rd, &irv)) { + if (irv == STRTOG_NoMemory) + return (STRTOG_NoMemory); goto ret; + } e1 -= e; goto rv_notOK; #endif @@ -590,16 +619,22 @@ strtodg #else /* rv = */ rounded_product(dval(rv), tens[e2]); #endif - if (rvOK(dval(rv), fpi, exp, bits, 0, rd, &irv)) + if (rvOK(dval(rv), fpi, exp, bits, 0, rd, &irv)) { + if (irv == STRTOG_NoMemory) + return (STRTOG_NoMemory); goto ret; + } e1 -= e2; } } #ifndef Inaccurate_Divide else if (e >= -Ten_pmax) { /* rv = */ rounded_quotient(dval(rv), tens[-e]); - if (rvOK(dval(rv), fpi, exp, bits, 0, rd, &irv)) + if (rvOK(dval(rv), fpi, exp, bits, 0, rd, &irv)) { + if (irv == STRTOG_NoMemory) + return (STRTOG_NoMemory); goto ret; + } e1 -= e; } #endif @@ -662,6 +697,8 @@ strtodg e2 <<= 2; #endif rvb = d2b(dval(rv), &rve, &rvbits); /* rv = rvb * 2^rve */ + if (rvb == NULL) + return (STRTOG_NoMemory); rve += e2; if ((j = rvbits - nbits) > 0) { rshift(rvb, j); @@ -678,6 +715,8 @@ strtodg j = rve - emin; if (j > 0) { rvb = lshift(rvb, j); + if (rvb == NULL) + return (STRTOG_NoMemory); rvbits += j; } else if (j < 0) { @@ -706,15 +745,23 @@ strtodg /* Put digits into bd: true value = bd * 10^e */ bd0 = s2b(s0, nd0, nd, y); + if (bd0 == NULL) + return (STRTOG_NoMemory); for(;;) { bd = Balloc(bd0->k); + if (bd == NULL) + return (STRTOG_NoMemory); Bcopy(bd, bd0); bb = Balloc(rvb->k); + if (bb == NULL) + return (STRTOG_NoMemory); Bcopy(bb, rvb); bbbits = rvbits - bb0; bbe = rve + bb0; bs = i2b(1); + if (bs == NULL) + return (STRTOG_NoMemory); if (e >= 0) { bb2 = bb5 = 0; @@ -745,24 +792,42 @@ strtodg } if (bb5 > 0) { bs = pow5mult(bs, bb5); + if (bs == NULL) + return (STRTOG_NoMemory); bb1 = mult(bs, bb); + if (bb1 == NULL) + return (STRTOG_NoMemory); Bfree(bb); bb = bb1; } bb2 -= bb0; - if (bb2 > 0) + if (bb2 > 0) { bb = lshift(bb, bb2); + if (bb == NULL) + return (STRTOG_NoMemory); + } else if (bb2 < 0) rshift(bb, -bb2); - if (bd5 > 0) + if (bd5 > 0) { bd = pow5mult(bd, bd5); - if (bd2 > 0) + if (bd == NULL) + return (STRTOG_NoMemory); + } + if (bd2 > 0) { bd = lshift(bd, bd2); - if (bs2 > 0) + if (bd == NULL) + return (STRTOG_NoMemory); + } + if (bs2 > 0) { bs = lshift(bs, bs2); + if (bs == NULL) + return (STRTOG_NoMemory); + } asub = 1; inex = STRTOG_Inexhi; delta = diff(bb, bd); + if (delta == NULL) + return (STRTOG_NoMemory); if (delta->wds <= 1 && !delta->x[0]) break; dsign = delta->sign; @@ -788,6 +853,8 @@ strtodg goto adj1; rve = rve1 - 1; rvb = set_ones(rvb, rvbits = nbits); + if (rvb == NULL) + return (STRTOG_NoMemory); break; } irv |= dsign ? STRTOG_Inexlo : STRTOG_Inexhi; @@ -803,6 +870,8 @@ strtodg if (dsign || bbbits > 1 || denorm || rve1 == emin) break; delta = lshift(delta,1); + if (delta == NULL) + return (STRTOG_NoMemory); if (cmp(delta, bs) > 0) { irv = STRTOG_Normal | STRTOG_Inexlo; goto drop_down; @@ -835,6 +904,8 @@ strtodg } rve -= nbits; rvb = set_ones(rvb, rvbits = nbits); + if (rvb == NULL) + return (STRTOG_NoMemory); break; } else @@ -843,6 +914,8 @@ strtodg break; if (dsign) { rvb = increment(rvb); + if (rvb == NULL) + return (STRTOG_NoMemory); j = kmask & (ULbits - (rvbits & kmask)); if (hi0bits(rvb->x[rvb->wds - 1]) != j) rvbits++; @@ -907,19 +980,28 @@ strtodg if (!denorm && rvbits < nbits) { rvb = lshift(rvb, j = nbits - rvbits); + if (rvb == NULL) + return (STRTOG_NoMemory); rve -= j; rvbits = nbits; } ab = d2b(dval(adj), &abe, &abits); + if (ab == NULL) + return (STRTOG_NoMemory); if (abe < 0) rshift(ab, -abe); - else if (abe > 0) + else if (abe > 0) { ab = lshift(ab, abe); + if (ab == NULL) + return (STRTOG_NoMemory); + } rvb0 = rvb; if (asub) { /* rv -= adj; */ j = hi0bits(rvb->x[rvb->wds-1]); rvb = diff(rvb, ab); + if (rvb == NULL) + return (STRTOG_NoMemory); k = rvb0->wds - 1; if (denorm) /* do nothing */; @@ -933,6 +1015,8 @@ strtodg } else { rvb = lshift(rvb, 1); + if (rvb == NULL) + return (STRTOG_NoMemory); --rve; --rve1; L = finished = 0; @@ -941,6 +1025,8 @@ strtodg } else { rvb = sum(rvb, ab); + if (rvb == NULL) + return (STRTOG_NoMemory); k = rvb->wds - 1; if (k >= rvb0->wds || hi0bits(rvb->x[k]) < hi0bits(rvb0->x[k])) { @@ -984,8 +1070,11 @@ strtodg Bfree(delta); } if (!denorm && (j = nbits - rvbits)) { - if (j > 0) + if (j > 0) { rvb = lshift(rvb, j); + if (rvb == NULL) + return (STRTOG_NoMemory); + } else rshift(rvb, -j); rve -= j; |