From d13d67bf5dff0fb20613d138c27a5815f7a4d4b1 Mon Sep 17 00:00:00 2001 From: Jonathan Albrecht Date: Fri, 4 Aug 2023 17:23:12 -0700 Subject: [PATCH] PHP: Fix read and write of float and double on big endian platforms (#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 #13444 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/13444 from linux-on-ibm-z:php-endian-fix 7840e097558aaff19d4fe268b49c8df9f9eaa61f PiperOrigin-RevId: 553954880 --- php/src/Google/Protobuf/Internal/GPBWire.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/php/src/Google/Protobuf/Internal/GPBWire.php b/php/src/Google/Protobuf/Internal/GPBWire.php index 034f5df92edcf..b6c532465a70f 100644 --- a/php/src/Google/Protobuf/Internal/GPBWire.php +++ b/php/src/Google/Protobuf/Internal/GPBWire.php @@ -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; } @@ -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; } @@ -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); }