Skip to content

Commit 9df8b2d

Browse files
committed
Prohibit char codes that would overflow the BASE_MAP
1 parent dd4c308 commit 9df8b2d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ function base (ALPHABET) {
8181
var b256 = new Uint8Array(size)
8282
// Process the characters.
8383
while (source[psz]) {
84+
// Find code of next character
85+
var charCode = source.charCodeAt(psz)
86+
// Base map can not be indexed using char code
87+
if (charCode > 255) { return }
8488
// Decode character
85-
var carry = BASE_MAP[source.charCodeAt(psz)]
89+
var carry = BASE_MAP[charCode]
8690
// Invalid character
8791
if (carry === 255) { return }
8892
var i = 0

test/fixtures.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@
660660
"alphabet": "0123456789fabcdef",
661661
"description": "poorly formed alphabet",
662662
"exception": "^TypeError: f is ambiguous$"
663+
},
664+
{
665+
"alphabet": "base58",
666+
"description": "character whose code exceeds the highest index of base map (>=256)",
667+
"exception": "^Error: Non-base58 character$",
668+
"string": "\u1000"
663669
}
664670
]
665671
}

ts_src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,14 @@ function base (ALPHABET: string): base.BaseConverter {
100100

101101
// Process the characters.
102102
while (source[psz]) {
103+
// Find code of next character
104+
const charCode = source.charCodeAt(psz)
105+
106+
// Base map can not be indexed using char code
107+
if (charCode > 255) return
108+
103109
// Decode character
104-
let carry = BASE_MAP[source.charCodeAt(psz)]
110+
let carry = BASE_MAP[charCode]
105111

106112
// Invalid character
107113
if (carry === 255) return

0 commit comments

Comments
 (0)