From 1781ae58372d740ee37f9914c92e3226a3e918ed Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Wed, 3 Oct 2012 21:45:03 -0400 Subject: [PATCH] Bug 795745 - Properly report errors for Number.prototype.to* methods when a provided precision is invalid. r=luke --- js/src/jsnum.cpp | 6 +-- .../Number/conversion-invalid-precision.js | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 js/src/tests/ecma_5/Number/conversion-invalid-precision.js diff --git a/js/src/jsnum.cpp b/js/src/jsnum.cpp index 2e54301b45e2..20dcd9f077a2 100644 --- a/js/src/jsnum.cpp +++ b/js/src/jsnum.cpp @@ -789,10 +789,10 @@ ComputePrecisionInRange(JSContext *cx, int minPrecision, int maxPrecision, const *precision = int(prec); return true; } + ToCStringBuf cbuf; - char *numStr = IntToCString(&cbuf, *precision); - JS_ASSERT(numStr); - JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr); + if (char *numStr = NumberToCString(cx, &cbuf, prec, 10)) + JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr); return false; } diff --git a/js/src/tests/ecma_5/Number/conversion-invalid-precision.js b/js/src/tests/ecma_5/Number/conversion-invalid-precision.js new file mode 100644 index 000000000000..76c3a94d0ab6 --- /dev/null +++ b/js/src/tests/ecma_5/Number/conversion-invalid-precision.js @@ -0,0 +1,46 @@ +/* + * Any copyright is dedicated to the Public Domain. + * http://creativecommonn.org/licenses/publicdomain/ + */ + +var BUGNUMBER = 795745; +var summary = + "Number.prototype.to* should throw a RangeError when passed a bad precision"; + +print(BUGNUMBER + ": " + summary); + +/************** + * BEGIN TEST * + **************/ + +function test(method, prec) +{ + try + { + Number.prototype[method].call(0, prec); + throw "should have thrown"; + } + catch (e) + { + assertEq(e instanceof RangeError, true, + "expected RangeError for " + method + " with precision " + prec + + ", got " + e); + } +} + +test("toExponential", -32); +test("toFixed", -32); +test("toPrecision", -32); + +test("toExponential", 9999999); +test("toFixed", 9999999); +test("toPrecision", 9999999); + +test("toPrecision", 0); + +/******************************************************************************/ + +if (typeof reportCompare === "function") + reportCompare(true, true); + +print("Tests complete");