[Python-modules-commits] [asyncpg] 05/08: use alignment-safe unpacking of buffers

Piotr Ożarowski piotr at moszumanska.debian.org
Wed Oct 25 09:02:44 UTC 2017


This is an automated email from the git hooks/post-receive script.

piotr pushed a commit to branch master
in repository asyncpg.

commit 83c0ec5802b588cf7c5d559f5c14773468323ad9
Author: Steve Langasek <steve.langasek at ubuntu.com>
Date:   Wed Oct 25 10:22:58 2017 +0200

    use alignment-safe unpacking of buffers
    
    Casting a char* to an int32* is not universally safe due to alignment
    constraints on reads on some platforms.  So skip using ntohl(), just
    unpack this ourselves.
---
 asyncpg/protocol/hton.pxd | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/asyncpg/protocol/hton.pxd b/asyncpg/protocol/hton.pxd
index 89ca506..219d837 100644
--- a/asyncpg/protocol/hton.pxd
+++ b/asyncpg/protocol/hton.pxd
@@ -5,7 +5,7 @@
 # the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
 
 
-from libc.stdint cimport int16_t, int32_t, uint16_t, uint32_t, int64_t, uint64_t
+from libc.stdint cimport uint8_t, int16_t, int32_t, uint16_t, uint32_t, int64_t, uint64_t
 
 
 IF UNAME_SYSNAME == "Windows":
@@ -35,7 +35,10 @@ cdef inline void pack_int32(char* buf, int32_t x):
 
 
 cdef inline int32_t unpack_int32(const char* buf):
-    return <int32_t>ntohl((<uint32_t*>buf)[0])
+    cdef uint32_t hh = (<uint8_t *>buf)[0]
+    hh = (hh << 8) | (<uint8_t *>buf)[1]
+    hh = (hh << 8) | (<uint8_t *>buf)[2]
+    return <int32_t>((hh << 8) | (<uint8_t *>buf)[3])
 
 
 cdef inline void pack_int64(char* buf, int64_t x):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/asyncpg.git



More information about the Python-modules-commits mailing list