From 9d4387e685cfd1b285080785df027976ec6e2e2e Mon Sep 17 00:00:00 2001 From: Maciej Bielecki Date: Tue, 6 Nov 2018 12:30:06 +0100 Subject: [PATCH 1/2] Run tests without codePointAt to exercise fallbacks --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d44d01e..28a3fd3 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "scripts": { "clean": "rimraf output && rimraf .pulp-cache", "build": "eslint src && pulp build -- --censor-lib --strict", - "test": "pulp test", + "test": "pulp test && npm run test:run:without_codePointAt", + "test:run:without_codePointAt": "node -e \"delete String.prototype.codePointAt; require('./output/Test.Main/index.js').main();\"", "bench:build": "purs compile 'bench/**/*.purs' 'src/**/*.purs' 'bower_components/*/src/**/*.purs'", "bench:run": "node --expose-gc -e 'require(\"./output/Bench.Main/index.js\").main()'", "bench": "npm run bench:build && npm run bench:run" From 45be85e59f5aea683633160f0484c35f114bade3 Mon Sep 17 00:00:00 2001 From: Maciej Bielecki Date: Tue, 6 Nov 2018 12:34:30 +0100 Subject: [PATCH 2/2] Fix out-out-bounds access in unsafeCodePointAt0Fallback `unsafeCodePointAt0Fallback` crashed when passed a string of length 1 due to always accessing index 1. --- src/Data/String/CodePoints.purs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Data/String/CodePoints.purs b/src/Data/String/CodePoints.purs index 7f72b7b..8492087 100644 --- a/src/Data/String/CodePoints.purs +++ b/src/Data/String/CodePoints.purs @@ -415,8 +415,10 @@ unsafeCodePointAt0Fallback :: String -> CodePoint unsafeCodePointAt0Fallback s = let cu0 = fromEnum (Unsafe.charAt 0 s) - cu1 = fromEnum (Unsafe.charAt 1 s) in - if isLead cu0 && isTrail cu1 - then unsurrogate cu0 cu1 - else CodePoint cu0 + if isLead cu0 && CU.length s > 1 + then + let cu1 = fromEnum (Unsafe.charAt 1 s) in + if isTrail cu1 then unsurrogate cu0 cu1 else CodePoint cu0 + else + CodePoint cu0