From 0289433aa0112a128d7d18415156b2352dbeb2f6 Mon Sep 17 00:00:00 2001
From: Denis Smirnov <sd@picodata.io>
Date: Sat, 23 Apr 2022 23:20:48 +0700
Subject: [PATCH] sql: fix -Wnull-pointer-subtraction warning
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

clang 13 includes a check for subtraction from NULL pointer which
is considered UB: historically, we had an alignment checking macro
which is affected by this. It seems like the intention of the
macro's author was to implicitly cast the pointer being checked to
uintptr_t without including stddef.h — replace this subtraction
with an explicit cast.

There is no way to set SQL_4_BYTE_ALIGNED_MALLOC, so the corresponding
part of the code was removed. Now there are only 8 byte alignment
assertions.

NO_CHANGELOG=UB fix
NO_DOC=UB fix
NO_TEST=UB fix

Co-authored-by: Georgiy Lebedev <curiousgeorgiy@gmail.com>
---
 src/box/sql/sqlInt.h | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 393b5e9a79..bc3f7b48e0 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -832,16 +832,8 @@ typedef u64 uptr;
  * Assert that the pointer X is aligned to an 8-byte boundary.  This
  * macro is used only within assert() to verify that the code gets
  * all alignment restrictions correct.
- *
- * Except, if sql_4_BYTE_ALIGNED_MALLOC is defined, then the
- * underlying malloc() implementation might return us 4-byte aligned
- * pointers.  In that case, only verify 4-byte alignment.
  */
-#ifdef SQL_4_BYTE_ALIGNED_MALLOC
-#define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
-#else
-#define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
-#endif
+#define EIGHT_BYTE_ALIGNMENT(X)   ((((uintptr_t)((char *)(X))) & 7) == 0)
 
 /*
  * Default maximum size of memory used by memory-mapped I/O in the VFS
-- 
GitLab