From 2235830e31b16c5a0af9605864b69a9da7106b84 Mon Sep 17 00:00:00 2001
From: Oliver Schmidt
Date: Sun, 19 Dec 2021 16:44:37 +0100
Subject: [PATCH 1/2] Don't rely on (actually changed) unspecified compiler
behavior.
---
libsrc/common/pmemalign.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libsrc/common/pmemalign.c b/libsrc/common/pmemalign.c
index 8f055a3fc..d9d6b4e97 100644
--- a/libsrc/common/pmemalign.c
+++ b/libsrc/common/pmemalign.c
@@ -75,7 +75,7 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size)
}
/* Test alignment: is it a power of two? There must be only one bit set. */
- if (alignment == 0 || (alignment & --alignment) != 0) {
+ if (alignment == 0 || (alignment & alignment - 1) != 0) {
*memptr = NULL;
return EINVAL;
}
From 884f72637b3b97a4fce0d5fbbe4f4a2c18df3281 Mon Sep 17 00:00:00 2001
From: Greg King
Date: Sun, 19 Dec 2021 20:27:56 -0500
Subject: [PATCH 2/2] Put the alignment decrement code back into
posix_memalign().
Without that code, the function returns a very broken pointer.
---
libsrc/common/pmemalign.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/libsrc/common/pmemalign.c b/libsrc/common/pmemalign.c
index d9d6b4e97..52adb240d 100644
--- a/libsrc/common/pmemalign.c
+++ b/libsrc/common/pmemalign.c
@@ -75,7 +75,7 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size)
}
/* Test alignment: is it a power of two? There must be only one bit set. */
- if (alignment == 0 || (alignment & alignment - 1) != 0) {
+ if (alignment == 0 || (alignment & (alignment - 1)) != 0) {
*memptr = NULL;
return EINVAL;
}
@@ -86,7 +86,7 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size)
** overhead added one time; and, the worst thing that might happen is that
** we cannot free the upper and lower blocks.
*/
- b = malloc (size + alignment);
+ b = malloc (--alignment + size);
/* Handle out-of-memory */
if (b == NULL) {
@@ -169,6 +169,3 @@ int __fastcall__ posix_memalign (void** memptr, size_t alignment, size_t size)
return EOK;
}
-
-
-