From 2a7e98a8cceb4546c602e6e50bafacf8a0595d59 Mon Sep 17 00:00:00 2001 From: Daryl McDaniel Date: Mon, 10 Nov 2014 23:05:45 +0000 Subject: [PATCH] StdLib: Fix a "potentially uninitialized variable" error. gdtoa/gdtoa.c: Several "goto" paths allowed the initialization of a variable to be bypassed. Initialized it at the top of the function in order to eliminate the error. Updated the file header and copyright notices. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Daryl McDaniel Reviewed-by: Erik Bjorge git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16324 6f19259b-4bc3-4df7-8a09-765794883524 --- StdLib/LibC/gdtoa/gdtoa.c | 209 ++++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 98 deletions(-) diff --git a/StdLib/LibC/gdtoa/gdtoa.c b/StdLib/LibC/gdtoa/gdtoa.c index b1457c1470..f203a30325 100644 --- a/StdLib/LibC/gdtoa/gdtoa.c +++ b/StdLib/LibC/gdtoa/gdtoa.c @@ -1,6 +1,15 @@ -/* $NetBSD: gdtoa.c,v 1.1.1.1.4.1.4.1 2008/04/08 21:10:55 jdc Exp $ */ +/** @file -/**************************************************************** + Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.
+ This program and the accompanying materials are licensed and made available under + the terms and conditions of the BSD License that accompanies this distribution. + The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + + *************************************************************** The author of this software is David M. Gay. @@ -26,10 +35,11 @@ IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -****************************************************************/ + Please send bug reports to David M. Gay (dmg at acm dot org, + with " at " changed at "@" and " dot " changed to "."). -/* Please send bug reports to David M. Gay (dmg at acm dot org, - * with " at " changed at "@" and " dot " changed to "."). */ + NetBSD: gdtoa.c,v 1.1.1.1.4.1.4.1 2008/04/08 21:10:55 jdc Exp +**/ #include #include "gdtoaimp.h" @@ -53,7 +63,7 @@ bitstob(ULong *bits, int nbits, int *bbits) while(i < nbits) { i <<= 1; k++; - } + } #ifndef Pack_32 if (!k) k = 1; @@ -68,19 +78,19 @@ bitstob(ULong *bits, int nbits, int *bbits) #ifdef Pack_16 *x++ = (*bits >> 16) & ALL_ON; #endif - } while(++bits <= be); + } while(++bits <= be); i = x - x0; while(!x0[--i]) if (!i) { b->wds = 0; *bbits = 0; goto ret; - } + } b->wds = i + 1; *bbits = i*ULbits + 32 - hi0bits(b->x[i]); - ret: +ret: return b; - } +} /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. * @@ -162,11 +172,13 @@ gdtoa double d, d2, ds, eps; char *s, *s0; + mlo = NULL; + #ifndef MULTIPLE_THREADS if (dtoa_result) { freedtoa(dtoa_result); dtoa_result = 0; - } + } #endif inex = 0; if (*kindp & STRTOG_NoMemory) @@ -174,19 +186,19 @@ gdtoa kind = *kindp &= ~STRTOG_Inexact; switch(kind & STRTOG_Retmask) { case STRTOG_Zero: - goto ret_zero; + goto ret_zero; case STRTOG_Normal: case STRTOG_Denormal: - break; + break; case STRTOG_Infinite: - *decpt = -32768; - return nrv_alloc("Infinity", rve, 8); + *decpt = -32768; + return nrv_alloc("Infinity", rve, 8); case STRTOG_NaN: - *decpt = -32768; - return nrv_alloc("NaN", rve, 3); + *decpt = -32768; + return nrv_alloc("NaN", rve, 3); default: - return 0; - } + return 0; + } b = bitstob(bits, nbits = fpi->nbits, &bbits); if (b == NULL) return NULL; @@ -195,13 +207,13 @@ gdtoa rshift(b, i); be += i; bbits -= i; - } + } if (!b->wds) { Bfree(b); - ret_zero: +ret_zero: *decpt = 1; return nrv_alloc("0", rve, 1); - } + } dval(d) = b2d(b, &i); i = be + bbits - 1; @@ -261,33 +273,33 @@ gdtoa if (dval(d) < tens[k]) k--; k_check = 0; - } + } j = bbits - i - 1; if (j >= 0) { b2 = 0; s2 = j; - } + } else { b2 = -j; s2 = 0; - } + } if (k >= 0) { b5 = 0; s5 = k; s2 += k; - } + } else { b2 -= k; b5 = -k; s5 = 0; - } + } if (mode < 0 || mode > 9) mode = 0; try_quick = 1; if (mode > 5) { mode -= 4; try_quick = 0; - } + } leftright = 1; switch(mode) { case 0: @@ -313,7 +325,7 @@ gdtoa ilim1 = i - 1; if (i <= 0) i = 1; - } + } s = s0 = rv_alloc((size_t)i); if (s == NULL) return NULL; @@ -323,7 +335,7 @@ gdtoa rdir = 2; if (kind & STRTOG_Neg) rdir = 3 - rdir; - } + } /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */ @@ -352,13 +364,13 @@ gdtoa j &= Bletch - 1; dval(d) /= bigtens[n_bigtens-1]; ieps++; - } + } for(; j; j /= 2, i++) if (j & 1) { ieps++; ds *= bigtens[i]; - } - } + } + } else { ds = 1.; if ( (jj1 = -k) !=0) { @@ -367,9 +379,9 @@ gdtoa if (j & 1) { ieps++; dval(d) *= bigtens[i]; - } - } + } } + } if (k_check && dval(d) < 1. && ilim > 0) { if (ilim1 <= 0) goto fast_failed; @@ -377,7 +389,7 @@ gdtoa k--; dval(d) *= 10.; ieps++; - } + } dval(eps) = ieps*dval(d) + 7.; word0(eps) -= (P-1)*Exp_msk1; if (ilim == 0) { @@ -388,7 +400,7 @@ gdtoa if (dval(d) < -dval(eps)) goto no_digits; goto fast_failed; - } + } #ifndef No_leftright if (leftright) { /* Use Steele & White method of only @@ -403,15 +415,15 @@ gdtoa if (dval(d)) inex = STRTOG_Inexlo; goto ret1; - } + } if (ds - dval(d) < dval(eps)) goto bump_up; if (++i >= ilim) break; dval(eps) *= 10.; dval(d) *= 10.; - } } + } else { #endif /* Generate ilim digits, then fix them up. */ @@ -430,19 +442,19 @@ gdtoa if (dval(d)) inex = STRTOG_Inexlo; goto ret1; - } - break; } + break; } -#ifndef No_leftright } +#ifndef No_leftright + } #endif - fast_failed: +fast_failed: s = s0; dval(d) = d2; k = k0; ilim = ilim0; - } + } /* Do we have a "small" integer? */ @@ -454,7 +466,7 @@ gdtoa if (ilim < 0 || dval(d) <= 5*ds) goto no_digits; goto one_digit; - } + } for(i = 1;; i++, dval(d) *= 10.) { L = dval(d) / ds; dval(d) -= L*ds; @@ -463,7 +475,7 @@ gdtoa if (dval(d) < 0) { L--; dval(d) += ds; - } + } #endif *s++ = '0' + (int)L; if (dval(d) == 0.) @@ -474,37 +486,38 @@ gdtoa goto bump_up; inex = STRTOG_Inexlo; goto ret1; - } + } dval(d) += dval(d); if (dval(d) > ds || (dval(d) == ds && L & 1)) { - bump_up: +bump_up: inex = STRTOG_Inexhi; while(*--s == '9') if (s == s0) { k++; *s = '0'; break; - } + } ++*s++; - } + } else inex = STRTOG_Inexlo; break; - } } - goto ret1; } + goto ret1; + } m2 = b2; m5 = b5; - mhi = mlo = 0; + mhi = NULL; + mlo = NULL; if (leftright) { if (mode < 2) { i = nbits - bbits; if (be - i++ < fpi->emin) /* denormal */ i = be - fpi->emin + 1; - } + } else { j = ilim - 1; if (m5 >= j) @@ -513,22 +526,22 @@ gdtoa s5 += j -= m5; b5 += j; m5 = 0; - } + } if ((i = ilim) < 0) { m2 -= i; i = 0; - } } + } b2 += i; s2 += i; mhi = i2b(1); - } + } if (m2 > 0 && s2 > 0) { i = m2 < s2 ? m2 : s2; b2 -= i; m2 -= i; s2 -= i; - } + } if (b5 > 0) { if (leftright) { if (m5 > 0) { @@ -540,19 +553,19 @@ gdtoa return NULL; Bfree(b); b = b1; - } + } if ( (j = b5 - m5) !=0) { b = pow5mult(b, j); if (b == NULL) return NULL; - } } + } else { b = pow5mult(b, b5); if (b == NULL) return NULL; - } } + } S = i2b(1); if (S == NULL) return NULL; @@ -560,7 +573,7 @@ gdtoa S = pow5mult(S, s5); if (S == NULL) return NULL; - } + } /* Check for special case that d is a normalized power of 2. */ @@ -571,8 +584,8 @@ gdtoa b2++; s2++; spec_case = 1; - } } + } /* Arrange for convenient computation of quotients: * shift left if necessary so divisor has 4 leading 0 bits. @@ -593,13 +606,13 @@ gdtoa b2 += i; m2 += i; s2 += i; - } + } else if (i < 4) { i += 28; b2 += i; m2 += i; s2 += i; - } + } if (b2 > 0) b = lshift(b, b2); if (s2 > 0) @@ -614,30 +627,30 @@ gdtoa mhi = multadd(mhi, 10, 0); if (mhi == NULL) return NULL; - } - ilim = ilim1; } + ilim = ilim1; } + } if (ilim <= 0 && mode > 2) { if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { /* no digits, fcvt style */ - no_digits: +no_digits: k = -1 - ndigits; inex = STRTOG_Inexlo; goto ret; - } - one_digit: + } +one_digit: inex = STRTOG_Inexhi; *s++ = '1'; k++; goto ret; - } + } if (leftright) { if (m2 > 0) { mhi = lshift(mhi, m2); if (mhi == NULL) return NULL; - } + } /* Compute mlo -- check for special case * that d is a normalized power of 2. @@ -652,7 +665,7 @@ gdtoa mhi = lshift(mhi, 1); if (mhi == NULL) return NULL; - } + } for(i = 1;;i++) { dig = quorem(b,S) + '0'; @@ -672,14 +685,14 @@ gdtoa if (j <= 0) { if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; - } + } else { dig++; inex = STRTOG_Inexhi; - } + } *s++ = dig; goto ret; - } + } #endif if (j < 0 || (j == 0 && !mode #ifndef ROUND_BIASED @@ -690,7 +703,7 @@ gdtoa if (rdir == 2) { inex = STRTOG_Inexlo; goto accept; - } + } while (cmp(S,mhi) > 0) { *s++ = dig; mhi1 = multadd(mhi, 10, 0); @@ -703,12 +716,12 @@ gdtoa if (b == NULL) return NULL; dig = quorem(b,S) + '0'; - } + } if (dig++ == '9') goto round_9_up; inex = STRTOG_Inexhi; goto accept; - } + } if (jj1 > 0) { b = lshift(b, 1); if (b == NULL) @@ -718,24 +731,24 @@ gdtoa && dig++ == '9') goto round_9_up; inex = STRTOG_Inexhi; - } + } if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; - accept: +accept: *s++ = dig; goto ret; - } + } if (jj1 > 0 && rdir != 2) { if (dig == '9') { /* possible if i == 1 */ - round_9_up: +round_9_up: *s++ = '9'; inex = STRTOG_Inexhi; goto roundoff; - } + } inex = STRTOG_Inexhi; *s++ = dig + 1; goto ret; - } + } *s++ = dig; if (i == ilim) break; @@ -746,7 +759,7 @@ gdtoa mlo = mhi = multadd(mhi, 10, 0); if (mlo == NULL) return NULL; - } + } else { mlo = multadd(mlo, 10, 0); if (mlo == NULL) @@ -754,9 +767,9 @@ gdtoa mhi = multadd(mhi, 10, 0); if (mhi == NULL) return NULL; - } } } + } else for(i = 1;; i++) { *s++ = dig = quorem(b,S) + '0'; @@ -765,7 +778,7 @@ gdtoa b = multadd(b, 10, 0); if (b == NULL) return NULL; - } + } /* Round off last digit */ @@ -773,37 +786,37 @@ gdtoa if (rdir == 2 || (b->wds <= 1 && !b->x[0])) goto chopzeros; goto roundoff; - } + } b = lshift(b, 1); if (b == NULL) return NULL; j = cmp(b, S); if (j > 0 || (j == 0 && dig & 1)) { - roundoff: +roundoff: inex = STRTOG_Inexhi; while(*--s == '9') if (s == s0) { k++; *s++ = '1'; goto ret; - } + } ++*s++; - } + } else { - chopzeros: +chopzeros: if (b->wds > 1 || b->x[0]) inex = STRTOG_Inexlo; while(*--s == '0'){} s++; - } - ret: + } +ret: Bfree(S); if (mhi) { if (mlo && mlo != mhi) Bfree(mlo); Bfree(mhi); - } - ret1: + } +ret1: Bfree(b); *s = 0; *decpt = k + 1; @@ -811,4 +824,4 @@ gdtoa *rve = s; *kindp |= inex; return s0; - } +}