From 850f7a98c657f68b69c72cf7a369541cd3bbefc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Tue, 22 Sep 2015 09:39:42 +0200 Subject: Make codeq.codePointCount() resilient to erraneous parameters. --- js/codeq.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/js/codeq.js b/js/codeq.js index e6a8a90..0ff78b2 100644 --- a/js/codeq.js +++ b/js/codeq.js @@ -716,8 +716,20 @@ window.phandler = null; // TODO: this is for debug only */ codeq.codePointCount = function (s) { var n = 0, i, code; + if (typeof s !== 'string') { + code = 'codePointCount(): argument not a string: type = ' + typeof s + ', is null = ' + (s === null); + if ((typeof s === 'object') && (s !== null) && s.constructor) code += ', constructor = ' + s.constructor.name; + codeq.log.error(code); + return 0; + } for (i = s.length - 1; i >= 0; i--) { - code = s.charCodeAt(i); + try { + code = s.charCodeAt(i); + } + catch (e) { + codeq.log.error('Invocation of charCodeAt() failed at iteration #' + i + ': ' + e, e); + return 0; + } if ((code >= 0xd800) && (code < 0xe000)) i++; n++; } -- cgit v1.2.1