@@ -2017,30 +2017,46 @@ public function decodePoint(string $wkb): array
20172017 throw new DatabaseException ('Invalid hex WKB string ' );
20182018 }
20192019
2020+ if (strlen ($ bin ) < 13 ) { // 1 byte endian + 4 bytes type + 8 bytes for X
2021+ throw new DatabaseException ('WKB too short ' );
2022+ }
2023+
20202024 $ isLE = ord ($ bin [0 ]) === 1 ;
2021- $ type = unpack ($ isLE ? 'V ' : 'N ' , substr ($ bin , 1 , 4 ));
2022- if ($ type === false ) {
2025+
2026+ // Type (4 bytes)
2027+ $ typeBytes = substr ($ bin , 1 , 4 );
2028+ if (strlen ($ typeBytes ) !== 4 ) {
2029+ throw new DatabaseException ('Failed to extract type bytes from WKB ' );
2030+ }
2031+
2032+ $ typeArr = unpack ($ isLE ? 'V ' : 'N ' , $ typeBytes );
2033+ if ($ typeArr === false || !isset ($ typeArr [1 ])) {
20232034 throw new DatabaseException ('Failed to unpack type from WKB ' );
20242035 }
2036+ $ type = $ typeArr [1 ];
20252037
2026- $ type = $ type [ 1 ];
2038+ // Offset to coordinates (skip SRID if present)
20272039 $ offset = 5 + (($ type & 0x20000000 ) ? 4 : 0 );
20282040
2029- $ fmt = $ isLE ? 'e ' : 'E ' ; // little vs big endian double
2030-
2031- $ x = unpack ($ fmt , substr ($ bin , $ offset , 8 ));
2032- if ($ x === false ) {
2033- throw new DatabaseException ('Failed to unpack double from WKB ' );
2041+ if (strlen ($ bin ) < $ offset + 16 ) { // 16 bytes for X,Y
2042+ throw new DatabaseException ('WKB too short for coordinates ' );
20342043 }
20352044
2036- $ x = ( float ) $ x [ 1 ];
2045+ $ fmt = $ isLE ? ' e ' : ' E ' ; // little vs big endian double
20372046
2038- $ y = unpack ($ fmt , substr ($ bin , $ offset + 8 , 8 ));
2039- if ($ y === false ) {
2040- throw new DatabaseException ('Failed to unpack Y coordinate from WKB ' );
2047+ // X coordinate
2048+ $ xArr = unpack ($ fmt , substr ($ bin , $ offset , 8 ));
2049+ if ($ xArr === false || !isset ($ xArr [1 ])) {
2050+ throw new DatabaseException ('Failed to unpack X coordinate ' );
20412051 }
2052+ $ x = (float )$ xArr [1 ];
20422053
2043- $ y = (float )$ y [1 ];
2054+ // Y coordinate
2055+ $ yArr = unpack ($ fmt , substr ($ bin , $ offset + 8 , 8 ));
2056+ if ($ yArr === false || !isset ($ yArr [1 ])) {
2057+ throw new DatabaseException ('Failed to unpack Y coordinate ' );
2058+ }
2059+ $ y = (float )$ yArr [1 ];
20442060
20452061 return [$ x , $ y ];
20462062 }
0 commit comments