From f3822d4ab005ac5db9000ed384519edfd5bbc041 Mon Sep 17 00:00:00 2001 From: select-case <86375512+select-case@users.noreply.github.com> Date: Sat, 24 Sep 2022 21:46:08 +0530 Subject: [PATCH] Fixed #34026 -- Fixed WKBReader.read() crash on string input. --- django/contrib/gis/geos/prototypes/io.py | 5 ++++- tests/gis_tests/geos_tests/test_io.py | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/django/contrib/gis/geos/prototypes/io.py b/django/contrib/gis/geos/prototypes/io.py index 555d0e3a25..7eb6eb8244 100644 --- a/django/contrib/gis/geos/prototypes/io.py +++ b/django/contrib/gis/geos/prototypes/io.py @@ -169,8 +169,11 @@ class _WKBReader(IOBase): if isinstance(wkb, memoryview): wkb_s = bytes(wkb) return wkb_reader_read(self.ptr, wkb_s, len(wkb_s)) - elif isinstance(wkb, (bytes, str)): + elif isinstance(wkb, bytes): return wkb_reader_read_hex(self.ptr, wkb, len(wkb)) + elif isinstance(wkb, str): + wkb_s = wkb.encode() + return wkb_reader_read_hex(self.ptr, wkb_s, len(wkb_s)) else: raise TypeError diff --git a/tests/gis_tests/geos_tests/test_io.py b/tests/gis_tests/geos_tests/test_io.py index b9d24cc63e..cab4b0ed5b 100644 --- a/tests/gis_tests/geos_tests/test_io.py +++ b/tests/gis_tests/geos_tests/test_io.py @@ -56,15 +56,17 @@ class GEOSIOTest(SimpleTestCase): # Creating a WKBReader instance wkb_r = WKBReader() - hex = b"000000000140140000000000004037000000000000" - wkb = memoryview(binascii.a2b_hex(hex)) - ref = GEOSGeometry(hex) + hex_bin = b"000000000140140000000000004037000000000000" + hex_str = "000000000140140000000000004037000000000000" + wkb = memoryview(binascii.a2b_hex(hex_bin)) + ref = GEOSGeometry(hex_bin) # read() should return a GEOSGeometry on either a hex string or # a WKB buffer. g1 = wkb_r.read(wkb) - g2 = wkb_r.read(hex) - for geom in (g1, g2): + g2 = wkb_r.read(hex_bin) + g3 = wkb_r.read(hex_str) + for geom in (g1, g2, g3): self.assertEqual(ref, geom) bad_input = (1, 5.23, None, False)