Skip to content

Commit

Permalink
PHP: Fix read and write of float and double on big endian platforms (p…
Browse files Browse the repository at this point in the history
…rotocolbuffers#13444)

Change the pack/unpack format codes 'f' -> 'g' and 'd' -> 'e' which ensure little endian format is used on the wire regardless of machine endianness. This allows the php composer and conformance tests to pass on big endian platforms and should not change any behavior on little endian platforms.

According to the PHP documentation, 'g' and 'e' were added in PHP versions 7.0.15 and 7.1.1.

Closes protocolbuffers#13444

COPYBARA_INTEGRATE_REVIEW=protocolbuffers#13444 from linux-on-ibm-z:php-endian-fix 7840e09
PiperOrigin-RevId: 553954880
  • Loading branch information
jonathan-albrecht-ibm authored and copybara-github committed Aug 5, 2023
1 parent 6104c43 commit d13d67b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions php/src/Google/Protobuf/Internal/GPBWire.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static function readFloat(&$input, &$value)
if (!$input->readRaw(4, $data)) {
return false;
}
$value = unpack('f', $data)[1];
$value = unpack('g', $data)[1];
return true;
}

Expand All @@ -251,7 +251,7 @@ public static function readDouble(&$input, &$value)
if (!$input->readRaw(8, $data)) {
return false;
}
$value = unpack('d', $data)[1];
$value = unpack('e', $data)[1];
return true;
}

Expand Down Expand Up @@ -360,13 +360,13 @@ public static function writeBool(&$output, $value)

public static function writeFloat(&$output, $value)
{
$data = pack("f", $value);
$data = pack("g", $value);
return $output->writeRaw($data, 4);
}

public static function writeDouble(&$output, $value)
{
$data = pack("d", $value);
$data = pack("e", $value);
return $output->writeRaw($data, 8);
}

Expand Down

0 comments on commit d13d67b

Please sign in to comment.