RESOLVED FIXED 25027
JavaScript parseInt wrong on negative numbers
https://bugs.webkit.org/show_bug.cgi?id=25027
Summary JavaScript parseInt wrong on negative numbers
Mihnea Ovidenie
Reported 2009-04-03 08:16:35 PDT
Hello, I used a simple test script for JavaScript parseInt function: <html> <head> <script> function testParseInt() { var result = -0.6; alert(parseInt(result)); } </script> </head> <body> <input type="button" onclick="testParseInt()" value="TestEval1"> </body> </html> However, instead of displaying 0, it display -1. The problem seems to be in: JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp function globalFuncParseInt at line: if (isfinite(d)) return jsNumber(exec, floor(d)); I think it should be: if (isfinite(d)) return jsNumber(exec, (d > 0) ? floor(d) : ceil(d)); I tested with latest WebKit build 42198 on Leopard and the alert displays -1 instead of 0. FF3/Opera/IE8(Win) display 0 on the same test. I can provide a patch but i was not sure where to put the layout test. Is it ok to change LayoutTests/fast/js/numeric-conversion-expected by adding test for this case or should i add another different unit test? Regards, Mihnea
Attachments
Simple patch for the issue (2.81 KB, patch)
2009-04-04 00:58 PDT, Mihnea Ovidenie
sam: review-
Patch following coding style rules (2.82 KB, patch)
2009-04-08 00:19 PDT, Mihnea Ovidenie
oliver: review+
Mihnea Ovidenie
Comment 1 2009-04-04 00:58:45 PDT
Created attachment 29250 [details] Simple patch for the issue Hello, I created the patch and added a simple test for the issue in LayoutTests/fast/js/numeric-conversion test. Regards, Mihnea
Sam Weinig
Comment 2 2009-04-06 11:50:23 PDT
Comment on attachment 29250 [details] Simple patch for the issue > - return jsNumber(exec, floor(d)); > + return jsNumber(exec, (d > 0)?floor(d):ceil(d)); Our style dictates that there should be spaces around the ? and :
Mihnea Ovidenie
Comment 3 2009-04-08 00:19:41 PDT
Created attachment 29325 [details] Patch following coding style rules Hello, I corrected the coding style and posted the patch again. Thanks for your comments. I will try to double check from now on the coding style before submitting a patch. I was mislead by the fact that in Xcode project i had formatted the code in appropriate style, while the script svn-create-patch somehow removed the spaces. Regards, Mihnea
Oliver Hunt
Comment 4 2009-04-16 23:59:11 PDT
Comment on attachment 29325 [details] Patch following coding style rules r=me! will land shortly
Oliver Hunt
Comment 5 2009-04-17 00:38:00 PDT
Committing to http://svn.webkit.org/repository/webkit/trunk ... M JavaScriptCore/ChangeLog M JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp M LayoutTests/ChangeLog M LayoutTests/fast/js/numeric-conversion-expected.txt M LayoutTests/fast/js/resources/numeric-conversion.js Committed r42607
Julien Dufrenne
Comment 6 2009-04-30 02:09:30 PDT
*** Bug 25478 has been marked as a duplicate of this bug. ***
Note You need to log in before you can comment on or make changes to this bug.