kaitai_struct
kaitai_struct copied to clipboard
JavaScript: `repeat` can create an Array with wrong size
This KSY when used without providing argument to the parameter (as WebIDE does) produces a value
with value of [undefined]
, because the first Array
constructor will be used (that accept elements):
meta:
id: js_array_bug
params:
- id: param
type: u4
seq:
- id: value
size: 2
repeat: expr
repeat-expr: param
Generated code looks like
Contexts.prototype._read = function() {
this.value = new Array(this.param);
for (var i = 0; i < this.param; i++) {
this.value[i] = this._io.readBytes(2);
}
}
and because this.param
is undefined it creates an array with one undefined
element. Then i < undefined
evaluated to false
and we get an [undefined]
in the value
.
To eliminate this problem it is possible use the conversion to integer trick: this.param|0