in reply to Re^3: Seeking with 'x' in unpack and out of bounds reads
in thread Seeking with 'x' in unpack and out of bounds reads

If rogue chunk is e.g. 7 bytes long, then unpacking with the proposed template will die on "X", so wrapping into eval is required anyway if data are unreliable.

However, 'x' doesn't have a direct effect on the output, so a partial match involving 'x' must be communicated through an error.

Looks to me like an attempt to whitewash inconsistent Perl's behaviour :-), By similar reasoning, failure to unpack e.g. Pascal strings (as "unpack 'C/a', qq(\03ab)") should be fatal, I think.

Side-note: PNG tags were made human-readable for a good reason, so perhaps "A4" instead of "N" (or "L") will serve better. E.g., if data are super-reliable (CRC sums to be ignored), then chunks can be read into a hash:

my ( $head, %chunks ) = unpack 'a8 (x4 A4 X8 N x4 /a x4)*', $input; say for keys %chunks;

Replies are listed 'Best First'.
Re^5: Seeking with 'x' in unpack and out of bounds reads
by Eily (Monsignor) on Apr 27, 2018 at 15:56 UTC

    Yes you're right. About everything ;). I should maybe have made it clearer that I was stating my understanding of x's behaviour, rather than an absolute truth :).

    For the X8 error, @0 might work better. But at that point, if you can't at least rely on the data to have the correct length, it might be easier to just read chunk by chunk, maybe even dividing each chunk into several unpacks.