From df8958e99f88014256ed245ddb2c08fe60270fde Mon Sep 17 00:00:00 2001 From: Wan-Teh Chang Date: Wed, 4 Jun 2014 15:14:30 -0700 Subject: [PATCH] Bug 1018783: Update to NSPR_4_10_6_BETA3. --- nsprpub/TAG-INFO | 2 +- nsprpub/config/prdepend.h | 1 - nsprpub/pr/src/io/prprf.c | 26 +++++++++++++------------- nsprpub/pr/tests/Makefile.in | 1 + nsprpub/pr/tests/prfdbl.c | 29 +++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 nsprpub/pr/tests/prfdbl.c diff --git a/nsprpub/TAG-INFO b/nsprpub/TAG-INFO index a5f6233ffe5..2c5ea8b1af2 100644 --- a/nsprpub/TAG-INFO +++ b/nsprpub/TAG-INFO @@ -1 +1 @@ -NSPR_4_10_6_BETA2 +NSPR_4_10_6_BETA3 diff --git a/nsprpub/config/prdepend.h b/nsprpub/config/prdepend.h index 6c66b37ca0f..e49e92677e3 100644 --- a/nsprpub/config/prdepend.h +++ b/nsprpub/config/prdepend.h @@ -10,4 +10,3 @@ */ #error "Do not include this header file." - diff --git a/nsprpub/pr/src/io/prprf.c b/nsprpub/pr/src/io/prprf.c index aa7852ffad1..a2eb4171a84 100644 --- a/nsprpub/pr/src/io/prprf.c +++ b/nsprpub/pr/src/io/prprf.c @@ -18,6 +18,10 @@ #include "prlog.h" #include "prmem.h" +#ifdef _MSC_VER +#define snprintf _snprintf +#endif + /* ** WARNING: This code may *NOT* call PR_LOG (because PR_LOG calls it) */ @@ -304,7 +308,7 @@ static int cvt_ll(SprintfState *ss, PRInt64 num, int width, int prec, int radix, ** Convert a double precision floating point number into its printable ** form. ** -** XXX stop using sprintf to convert floating point +** XXX stop using snprintf to convert floating point */ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) { @@ -312,15 +316,14 @@ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) char fout[300]; int amount = fmt1 - fmt0; - PR_ASSERT((amount > 0) && (amount < sizeof(fin))); - if (amount >= sizeof(fin)) { - /* Totally bogus % command to sprintf. Just ignore it */ + if (amount <= 0 || amount >= sizeof(fin)) { + /* Totally bogus % command to snprintf. Just ignore it */ return 0; } memcpy(fin, fmt0, amount); fin[amount] = 0; - /* Convert floating point using the native sprintf code */ + /* Convert floating point using the native snprintf code */ #ifdef DEBUG { const char *p = fin; @@ -330,14 +333,11 @@ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) } } #endif - sprintf(fout, fin, d); - - /* - ** This assert will catch overflow's of fout, when building with - ** debugging on. At least this way we can track down the evil piece - ** of calling code and fix it! - */ - PR_ASSERT(strlen(fout) < sizeof(fout)); + memset(fout, 0, sizeof(fout)); + snprintf(fout, sizeof(fout), fin, d); + /* Explicitly null-terminate fout because on Windows snprintf doesn't + * append a null-terminator if the buffer is too small. */ + fout[sizeof(fout) - 1] = '\0'; return (*ss->stuff)(ss, fout, strlen(fout)); } diff --git a/nsprpub/pr/tests/Makefile.in b/nsprpub/pr/tests/Makefile.in index ca42c59c380..9ebd923343c 100644 --- a/nsprpub/pr/tests/Makefile.in +++ b/nsprpub/pr/tests/Makefile.in @@ -107,6 +107,7 @@ CSRCS = \ poll_nm.c \ poll_to.c \ pollable.c \ + prfdbl.c \ prftest.c \ prftest1.c \ prftest2.c \ diff --git a/nsprpub/pr/tests/prfdbl.c b/nsprpub/pr/tests/prfdbl.c new file mode 100644 index 00000000000..3bb0650b2e6 --- /dev/null +++ b/nsprpub/pr/tests/prfdbl.c @@ -0,0 +1,29 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * This is a simple test of the PR_fprintf() function for doubles. + */ + +#include "prprf.h" + +int main() +{ + double pi = 3.1415926; + double e = 2.71828; + double root2 = 1.414; + double zero = 0.0; + double nan = zero / zero; + + PR_fprintf(PR_STDOUT, "pi is %f.\n", pi); + PR_fprintf(PR_STDOUT, "e is %f.\n", e); + PR_fprintf(PR_STDOUT, "The square root of 2 is %f.\n", root2); + PR_fprintf(PR_STDOUT, "NaN is %f.\n", nan); + + PR_fprintf(PR_STDOUT, "pi is %301f.\n", pi); + PR_fprintf(PR_STDOUT, "e is %65416.123f.\n", e); + PR_fprintf(PR_STDOUT, "e is %0000000000000000000065416.123f.\n", e); + PR_fprintf(PR_STDOUT, "NaN is %1024.1f.\n", nan); + return 0; +}