diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-01-24 18:14:36 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-01-24 22:10:17 +0100 |
commit | a1b3c5dfbe0aaf46d3d71656e65d72526bfa5543 (patch) | |
tree | 22a01f1f8350ae65eff996cd504fdf5e11fb5105 /tests/custom/01_arithmetic/03_bitwise | |
parent | b2fac62699687368559af06487338f282e30c723 (diff) |
struct: implement `*` format, fix invalid memory accesses
Implement a new `*` format which acts like `s` on unpack but accepts
input records which are shorter than the specified length, e.g. the
following call will yield "abc" while an equivalent "10s" format would
fail:
unpack("2*", "abc") // [ "ab" ]
unpack("10*", "abc") // [ "abc" ]
unpack("10s", "abc") // null
The `*` format is primarily useful to extract the remainder of a variable
length record without having to encode the specific length of the record
directly into the format string.
When packing records, the `*` format takes at most as many bytes as
specified in the format string repeat count. If the input string is
shorter than the given repeat count, only as many bytes as present in
the input string are taken. A bare `*` without any repeat count will take
all bytes from the given input string:
pack("2*", "abc") // "ab"
pack("10*", "abc") // "abc"
pack("*", "abc") // "abc"
pack("10s", "abc") // "abc\0\0\0\0\0\0\0"
Additionally prevent invalid memory accesses when unpacking a buffer
shorter than the length expected by the format string.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/01_arithmetic/03_bitwise')
0 files changed, 0 insertions, 0 deletions