summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2021-05-11 17:16:58 +0100
committerLinus Groh <mail@linusgroh.de>2021-05-11 17:29:37 +0100
commit2ff03ecfa80eddae59ac81a0a2910ad00b0774aa (patch)
tree9821a4755ad7fa3e7d7c3fe75060e3172f02657c /Userland/Libraries
parentecbe17fb11e587341404a3bc2cc0b3db799374f1 (diff)
downloadserenity-2ff03ecfa80eddae59ac81a0a2910ad00b0774aa.zip
LibJS: Make number parts unsigned in NumberPrototype::to_string
Fixes #3931
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp8
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js3
2 files changed, 7 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp
index 23e10ede20..54852f6a57 100644
--- a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp
@@ -80,7 +80,7 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
if (negative)
number *= -1;
- int int_part = floor(number);
+ u32 int_part = floor(number);
double decimal_part = number - int_part;
Vector<char> backwards_characters;
@@ -107,11 +107,11 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_string)
if (decimal_part != 0.0) {
characters.append('.');
- int precision = max_precision_for_radix[radix];
+ u8 precision = max_precision_for_radix[radix];
- for (int i = 0; i < precision; ++i) {
+ for (u8 i = 0; i < precision; ++i) {
decimal_part *= radix;
- int integral = floor(decimal_part);
+ u32 integral = floor(decimal_part);
characters.append(digits[integral]);
decimal_part -= integral;
}
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js
index c07ca792f6..0054162395 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toString.js
@@ -13,6 +13,9 @@ describe("correct behavior", () => {
[12, "12"],
[93465, "93465"],
[358000, "358000"],
+ // Numbers above 2 ** 31 - 1 (Issue #3931)
+ [2147483648, "2147483648"], // 2 ** 31
+ [4294967295, "4294967295"], // 2 ** 32 - 1
].forEach(testCase => {
expect(testCase[0].toString()).toBe(testCase[1]);
});