Skip to content

Commit 5753ba6

Browse files
committed
Fixing the out of bounds dataview error when attribute is of size 3 bytes
1 parent 419f479 commit 5753ba6

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src-electron/util/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function convertFloatToBigEndian(value, size) {
116116
* the given size. The value is returned in hex format and prefixed with '0x'.
117117
*/
118118
function convertIntToBigEndian(value, size) {
119-
const arrayBuffer = size <= 4 ? new ArrayBuffer(size) : new ArrayBuffer(8)
119+
const arrayBuffer = new ArrayBuffer(Math.pow(2, Math.ceil(Math.log2(size))))
120120
const dataView = new DataView(arrayBuffer)
121121
let i = 0
122122
if (size == 1) {

test/helpers.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,32 @@ test(
234234
testUtil.timeout.short()
235235
)
236236

237+
test(
238+
'Generated Macro little endian for attribute of size 3 bytes',
239+
() => {
240+
let options = { hash: { endian: 'little' } }
241+
return zclHelper
242+
.as_generated_default_macro('0x003840', 3, options)
243+
.then((res) => {
244+
return expect(res).toBe('0x40, 0x38, 0x00, ')
245+
})
246+
},
247+
testUtil.timeout.short()
248+
)
249+
250+
test(
251+
'Generated Macro big endian for attribute of size 3 bytes',
252+
() => {
253+
let options = { hash: { endian: 'big' } }
254+
return zclHelper
255+
.as_generated_default_macro('0x003840', 3, options)
256+
.then((res) => {
257+
return expect(res).toBe(' 0x00, 0x38, 0x40,')
258+
})
259+
},
260+
testUtil.timeout.short()
261+
)
262+
237263
test(
238264
'Generated Macro little endian',
239265
() => {

0 commit comments

Comments
 (0)