Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/byte-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ function utf8ToBytes (str) {
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
} else {
if ((c >= 0xD800) && (c <= 0xDFFF)) {
c = 0xFFFD // Unpaired Surrogate
}
out[p++] = (c >> 12) | 224
out[p++] = ((c >> 6) & 63) | 128
out[p++] = (c & 63) | 128
Expand Down
18 changes: 18 additions & 0 deletions test/test-3string.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ describe('string', () => {
() => decode(fromHex('7ba5f702b3a5f702b34c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e6563206d692074656c6c75732c20696163756c6973206e656320766573746962756c756d20717569732c206665726d656e74756d206e6f6e2066656c69732e204d616563656e6173207574206a7573746f20706f73756572652e')),
/CBOR decode error: 64-bit integer string lengths not supported/)
})

it('decodes unpaired surrogates as U+FFFD', () => {
assert.strictEqual(decode(fromHex('63edb88e')), '\uFFFD\uFFFD\uFFFD')
})
}
})

Expand All @@ -131,6 +135,20 @@ describe('string', () => {
assert.strictEqual(toHex(encode(data)), expectedHex, `encode ${fixture.type}`)
}
})

it('should encode unpaired surrogates as U+FFFD', () => {
// short strings
assert.strictEqual(
toHex(encode('😎'.slice(1))),
toHex(encode('\uFFFD'))
)

// long strings (>64)
assert.strictEqual(
toHex(encode('A'.repeat(65) + '😎'.slice(1))),
toHex(encode('A'.repeat(65) + '\uFFFD'))
)
})
}
})
})