Skip to content
代码片段 群组 项目
未验证 提交 e652a5df 编辑于 作者: Boris Zbarsky's avatar Boris Zbarsky 提交者: GitHub
浏览文件

Fix default values for long strings. (#632)

Long strings store their length in 2 bytes at the front of the string, with the
bytes being a little-endian representation of the string length.

But the endpoint config output was spitting out the length as big-endian, not
little-endian.  This caused things to be pretty confused and led to buffer
overruns trying to read data that was not there.
上级 20fcf73e
No related branches found
No related tags found
无相关合并请求
...@@ -125,7 +125,9 @@ function hexToBinary(hex: string) { ...@@ -125,7 +125,9 @@ function hexToBinary(hex: string) {
cleansedHex = cleansedHex.replace(/[^0-F]/g, '') cleansedHex = cleansedHex.replace(/[^0-F]/g, '')
return parseInt(cleansedHex, 16).toString(2).padStart(cleansedHex.length*4, '0') return parseInt(cleansedHex, 16)
.toString(2)
.padStart(cleansedHex.length * 4, '0')
} }
/** /**
...@@ -139,7 +141,11 @@ function hexToBinary(hex: string) { ...@@ -139,7 +141,11 @@ function hexToBinary(hex: string) {
* @returns Object containing 'length' and 'content', where length * @returns Object containing 'length' and 'content', where length
* is number of bytes used and content is actual content in C format. * is number of bytes used and content is actual content in C format.
*/ */
function stringToOneByteLengthPrefixCBytes(value: string, maxLength: number, pad = true) { function stringToOneByteLengthPrefixCBytes(
value: string,
maxLength: number,
pad = true
) {
let len = value.length let len = value.length
let ret = `${len}, ` let ret = `${len}, `
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
...@@ -169,10 +175,14 @@ function stringToOneByteLengthPrefixCBytes(value: string, maxLength: number, pad ...@@ -169,10 +175,14 @@ function stringToOneByteLengthPrefixCBytes(value: string, maxLength: number, pad
* @returns Object containing 'length' and 'content', where length * @returns Object containing 'length' and 'content', where length
* is number of bytes used and content is actual content in C format. * is number of bytes used and content is actual content in C format.
*/ */
function stringToTwoByteLengthPrefixCBytes(value: string, maxLength: number, pad = true) { function stringToTwoByteLengthPrefixCBytes(
value: string,
maxLength: number,
pad = true
) {
let len = value.length let len = value.length
let ret = `${(len >> 8) & 0xff}, ` let ret = `${len & 0xff}, `
ret = ret.concat(`${len & 0xff}, `) ret = ret.concat(`${(len >> 8) & 0xff}, `)
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
ret = ret.concat(`'${value[i]}', `) ret = ret.concat(`'${value[i]}', `)
} }
......
...@@ -118,13 +118,13 @@ test( ...@@ -118,13 +118,13 @@ test(
r = bin.stringToTwoByteLengthPrefixCBytes('Test string') r = bin.stringToTwoByteLengthPrefixCBytes('Test string')
expect(r.content).toContain( expect(r.content).toContain(
"0, 11, 'T', 'e', 's', 't', ' ', 's', 't', 'r', 'i', 'n', 'g'," "11, 0, 'T', 'e', 's', 't', ' ', 's', 't', 'r', 'i', 'n', 'g',"
) )
expect(r.length).toBe(13) expect(r.length).toBe(13)
r = bin.stringToTwoByteLengthPrefixCBytes('x'.repeat(300), 400, false) r = bin.stringToTwoByteLengthPrefixCBytes('x'.repeat(300), 400, false)
expect(r.content).toContain( expect(r.content).toContain(
"1, 44, 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x'," "44, 1, 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',"
) )
expect(r.length).toBe(302) expect(r.length).toBe(302)
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册