]> Savannah Git Hosting - gnulib.git/commitdiff
crc-x86_64: better fix for unaligned access
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 Jan 2025 18:27:55 +0000 (10:27 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 17 Jan 2025 18:28:31 +0000 (10:28 -0800)
Avoid undefined behavior in a way that doesn’t require
the input buffer to be aligned.
From a suggestion by Lasse Collin in:
https://lists.gnu.org/r/bug-gnulib/2025-01/msg00148.html
* lib/crc-x86_64-pclmul.c (crc32_update_no_xor_pclmul):
Since the const void * pointer ‘buf’ might not be aligned,
assign it to const __m128i_u * instead of to const __m128i *.
* lib/crc.c (crc32_update_no_xor):
Remove recently-addeda check for buffer alignment.

ChangeLog
lib/crc-x86_64-pclmul.c
lib/crc.c

index f80d7579c088a8742510c9d4461b076d91e30cf1..3164c1c5870feda958d714ea139c8c411e16a90a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2025-01-17  Paul Eggert  <eggert@cs.ucla.edu>
+
+       crc-x86_64: better fix for unaligned access
+       Avoid undefined behavior in a way that doesn’t require
+       the input buffer to be aligned.
+       From a suggestion by Lasse Collin in:
+       https://lists.gnu.org/r/bug-gnulib/2025-01/msg00148.html
+       * lib/crc-x86_64-pclmul.c (crc32_update_no_xor_pclmul):
+       Since the const void * pointer ‘buf’ might not be aligned,
+       assign it to const __m128i_u * instead of to const __m128i *.
+       * lib/crc.c (crc32_update_no_xor):
+       Remove recently-addeda check for buffer alignment.
+
 2025-01-17  Pádraig Brady  <P@draigBrady.com>
 
        Avoid -Wformat=security failures with --disable-nls
index 48f5c7a84c28ca1bd7871a6dae18382607924fe1..16508433946e9aaf2c3f446fffd6ca30ace1c6c1 100644 (file)
@@ -32,7 +32,7 @@ __attribute__ ((__target__ ("pclmul,avx")))
 uint32_t
 crc32_update_no_xor_pclmul (uint32_t crc, const void *buf, size_t len)
 {
-  const __m128i *data = buf;
+  const __m128i_u *data = buf;
   __m128i *datarw;
   size_t bytes_remaining = len;
   __m128i in256[4] = { 0 };
index 57abf27cc3160d750bfc267236d23822710f64f4..319a23715b82a7cbad276e583049cdc8b2c9af19 100644 (file)
--- a/lib/crc.c
+++ b/lib/crc.c
@@ -123,7 +123,7 @@ crc32_update_no_xor (uint32_t crc, const char *buf, size_t len)
       pclmul_checked = true;
     }
 
-  if (pclmul_enabled && len >= 16 && (intptr_t) buf % 16 == 0)
+  if (pclmul_enabled && len >= 16)
     return crc32_update_no_xor_pclmul (crc, buf, len);
 #endif
 
@@ -205,7 +205,7 @@ crc32_update_no_xor (uint32_t crc, const char *buf, size_t len)
       pclmul_checked = true;
     }
 
-  if (pclmul_enabled && len >= 16 && (intptr_t) buf % 16 == 0)
+  if (pclmul_enabled && len >= 16)
     return crc32_update_no_xor_pclmul (crc, buf, len);
 #endif