[PATCH][BUG FIX] Pointer arithmetic with NULL

Started by Ranier Vilelaabout 6 years ago2 messages
#1Ranier Vilela
ranier_gyn@hotmail.com
1 attachment(s)

Hi,
Pointer addition with NULL, is technically undefined behavior.

Best regards.
Ranier Vilela

--- \dll\postgresql-12.0\a\backend\access\transam\xlog.c	Mon Sep 30 17:06:55 2019
+++ xlog.c	Fri Nov 22 13:57:17 2019
@@ -1861,7 +1861,7 @@
 	{
 		Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
 		Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
-		return cachedPos + ptr % XLOG_BLCKSZ;
+		return ptr % XLOG_BLCKSZ;
 	}

/*

Attachments:

xlog.c.patchapplication/octet-stream; name=xlog.c.patchDownload
--- \dll\postgresql-12.0\a\backend\access\transam\xlog.c	Mon Sep 30 17:06:55 2019
+++ xlog.c	Fri Nov 22 13:57:17 2019
@@ -1861,7 +1861,7 @@
 	{
 		Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
 		Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
-		return cachedPos + ptr % XLOG_BLCKSZ;
+		return ptr % XLOG_BLCKSZ;
 	}
 
 	/*
#2Tomas Vondra
tomas.vondra@2ndquadrant.com
In reply to: Ranier Vilela (#1)
Re: [PATCH][BUG FIX] Pointer arithmetic with NULL

On Fri, Nov 22, 2019 at 05:19:11PM +0000, Ranier Vilela wrote:

Hi,
Pointer addition with NULL, is technically undefined behavior.

Best regards.
Ranier Vilela

--- \dll\postgresql-12.0\a\backend\access\transam\xlog.c	Mon Sep 30 17:06:55 2019
+++ xlog.c	Fri Nov 22 13:57:17 2019
@@ -1861,7 +1861,7 @@
{
Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
-		return cachedPos + ptr % XLOG_BLCKSZ;
+		return ptr % XLOG_BLCKSZ;
}

/*

But the value is not necessarily NULL, because it's defined like this:

static char *cachedPos = NULL;

that is, it's a static value - i.e. retained across multiple calls. The
question is whether we can get into that branch before it's set, but
it's certainly not correct to just remove it ...

regards

--
Tomas Vondra http://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services