Re: unpack() offset and consumed data measurement

From: Date: Tue, 30 Jan 2018 11:56:27 +0000
Subject: Re: unpack() offset and consumed data measurement
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-101730@lists.php.net to get a copy of this message
On 28 January 2018 at 12:12, Chris Wright <cw@daverandom.com> wrote: > > Here is some code that demonstrates the problem: > > /* This is the only way to know for certain how big float is on the > local system */ > define('FLOAT_WIDTH', strlen(pack('f', 0.0))); > > /* an exaggerated example using two variable width codes and a code > that > does not produce output but modifies the input buffer offset */ > $pieces = unpack('f/X/Z*', $data, $offset); > > /* we now have to modify the offset before we can continue to unpack > data */ > $offset += FLOAT_WIDTH // f > - 1 // x > + strlen($pieces[3]); // Z* > Re-reading this mail I have noticed there was a small mistake in the code sample, in that I forgot to include the terminating null byte for the Z* data. This (unintentionally) demonstrates the exact reason I would like to add this, as it's very easy to accidentally write subtle bugs.

« previous php.internals (#101730) next »