luci-base: cbi.js: fix number rounding in string.format()
authorJo-Philipp Wich <jo@mein.io>
Thu, 6 Jun 2019 19:06:25 +0000 (21:06 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 7 Jul 2019 13:36:26 +0000 (15:36 +0200)
Ensure that patterns like %d, %x, %o or %b properly truncate their
operands to whole integers.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/cbi.js

index 701de0386b14ee713c9c7b651df12e8a85a10599..d4d61eb381119b18dc5761fe50776497a74e11a6 100644 (file)
@@ -566,7 +566,7 @@ String.prototype.format = function()
 
                                switch(pType) {
                                        case 'b':
-                                               subst = (+param || 0).toString(2);
+                                               subst = (~~param || 0).toString(2);
                                                break;
 
                                        case 'c':
@@ -574,7 +574,7 @@ String.prototype.format = function()
                                                break;
 
                                        case 'd':
-                                               subst = ~~(+param || 0);
+                                               subst = (~~param || 0);
                                                break;
 
                                        case 'u':
@@ -588,7 +588,7 @@ String.prototype.format = function()
                                                break;
 
                                        case 'o':
-                                               subst = (+param || 0).toString(8);
+                                               subst = (~~param || 0).toString(8);
                                                break;
 
                                        case 's':
@@ -596,11 +596,11 @@ String.prototype.format = function()
                                                break;
 
                                        case 'x':
-                                               subst = ('' + (+param || 0).toString(16)).toLowerCase();
+                                               subst = ('' + (~~param || 0).toString(16)).toLowerCase();
                                                break;
 
                                        case 'X':
-                                               subst = ('' + (+param || 0).toString(16)).toUpperCase();
+                                               subst = ('' + (~~param || 0).toString(16)).toUpperCase();
                                                break;
 
                                        case 'h':