Subject: Improve const use in zlib-using code

From: Peter Eisentraut <[email-masked]>
Date: Wed, 2 Aug 2023 11:50:57 +0200

Now that we have effectively de-supported CentOS 6, by removing support 
for its OpenSSL version, I think we could also modernize the use of some 
other libraries, such as zlib.

If we define ZLIB_CONST before including zlib.h, zlib augments some
interfaces with const decorations.  By doing that we can keep our own
interfaces cleaner and can remove some unconstify calls.

ZLIB_CONST was introduced in zlib 1.2.5.2 (17 Dec 2011); CentOS 6 has 
zlib-1.2.3-29.el6.x86_64.

Note that if you use this patch and compile on CentOS 6, it still works, 
you just get a few compiler warnings about discarding qualifiers.  Old 
environments tend to produce more compiler warnings anyway, so this 
doesn't seem so bad.

From 324e37adff4c51f4f10807386c0c098cb8d21608 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email-masked]>
Date: Wed, 2 Aug 2023 11:01:27 +0200
Subject: [PATCH] Improve const use in zlib-using code

If we define ZLIB_CONST before including zlib.h, zlib augments some
interfaces with const decorations.  By doing that we can keep our own
interfaces cleaner and can remove some unconstify calls.

ZLIB_CONST was introduced in zlib 1.2.5.2 (17 Dec 2011).

XXX CentOS 6 has zlib-1.2.3-29.el6.x86_64
---
 contrib/pgcrypto/pgp-compress.c         | 3 ++-
 src/backend/backup/basebackup_gzip.c    | 1 +
 src/bin/pg_basebackup/bbstreamer_gzip.c | 3 ++-
 src/bin/pg_basebackup/pg_basebackup.c   | 1 +
 src/bin/pg_basebackup/pg_receivewal.c   | 1 +
 src/bin/pg_basebackup/walmethods.c      | 6 +++---
 src/bin/pg_dump/compress_gzip.c         | 1 +
 src/common/compression.c                | 1 +
 8 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/contrib/pgcrypto/pgp-compress.c b/contrib/pgcrypto/pgp-compress.c
index 086bec31ae..5615f1acce 100644
--- a/contrib/pgcrypto/pgp-compress.c
+++ b/contrib/pgcrypto/pgp-compress.c
@@ -40,6 +40,7 @@
 
 #ifdef HAVE_LIBZ
 
+#define ZLIB_CONST
 #include <zlib.h>
 
 #define ZIP_OUT_BUF 8192
@@ -113,7 +114,7 @@ compress_process(PushFilter *next, void *priv, const uint8 *data, int len)
 	/*
 	 * process data
 	 */
-	st->stream.next_in = unconstify(uint8 *, data);
+	st->stream.next_in = data;
 	st->stream.avail_in = len;
 	while (st->stream.avail_in > 0)
 	{
diff --git a/src/backend/backup/basebackup_gzip.c b/src/backend/backup/basebackup_gzip.c
index b2d5e19ad9..45210a2ff0 100644
--- a/src/backend/backup/basebackup_gzip.c
+++ b/src/backend/backup/basebackup_gzip.c
@@ -13,6 +13,7 @@
 #include "postgres.h"
 
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
diff --git a/src/bin/pg_basebackup/bbstreamer_gzip.c b/src/bin/pg_basebackup/bbstreamer_gzip.c
index 3bdbfa0bc4..fa52392a48 100644
--- a/src/bin/pg_basebackup/bbstreamer_gzip.c
+++ b/src/bin/pg_basebackup/bbstreamer_gzip.c
@@ -14,6 +14,7 @@
 #include <unistd.h>
 
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
@@ -269,7 +270,7 @@ bbstreamer_gzip_decompressor_content(bbstreamer *streamer,
 	mystreamer = (bbstreamer_gzip_decompressor *) streamer;
 
 	zs = &mystreamer->zstream;
-	zs->next_in = (uint8 *) data;
+	zs->next_in = (const uint8 *) data;
 	zs->avail_in = len;
 
 	/* Process the current chunk */
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 1dc8efe0cb..90fd044ec3 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -22,6 +22,7 @@
 #include <signal.h>
 #include <time.h>
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index d0a4079d50..c0de77b5aa 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -24,6 +24,7 @@
 #include <lz4frame.h>
 #endif
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
diff --git a/src/bin/pg_basebackup/walmethods.c b/src/bin/pg_basebackup/walmethods.c
index 376ddf72b7..b663a3d978 100644
--- a/src/bin/pg_basebackup/walmethods.c
+++ b/src/bin/pg_basebackup/walmethods.c
@@ -19,6 +19,7 @@
 #include <lz4frame.h>
 #endif
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
@@ -705,7 +706,7 @@ typedef struct TarMethodData
 
 #ifdef HAVE_LIBZ
 static bool
-tar_write_compressed_data(TarMethodData *tar_data, void *buf, size_t count,
+tar_write_compressed_data(TarMethodData *tar_data, const void *buf, size_t count,
 						  bool flush)
 {
 	tar_data->zp->next_in = buf;
@@ -782,8 +783,7 @@ tar_write(Walfile *f, const void *buf, size_t count)
 #ifdef HAVE_LIBZ
 	else if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
 	{
-		if (!tar_write_compressed_data(tar_data, unconstify(void *, buf),
-									   count, false))
+		if (!tar_write_compressed_data(tar_data, buf, count, false))
 			return -1;
 		f->currpos += count;
 		return count;
diff --git a/src/bin/pg_dump/compress_gzip.c b/src/bin/pg_dump/compress_gzip.c
index 63dfd9668c..b536a527d7 100644
--- a/src/bin/pg_dump/compress_gzip.c
+++ b/src/bin/pg_dump/compress_gzip.c
@@ -18,6 +18,7 @@
 #include "pg_backup_utils.h"
 
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include "zlib.h"
 
 /*----------------------
diff --git a/src/common/compression.c b/src/common/compression.c
index ee937623f0..07a5d1b51d 100644
--- a/src/common/compression.c
+++ b/src/common/compression.c
@@ -31,6 +31,7 @@
 #include <zstd.h>
 #endif
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include <zlib.h>
 #endif
 
-- 
2.41.0

---

From: "Tristan Partin" <[email-masked]>
Date: Wed, 02 Aug 2023 09:39:00 -0500

Peter,

I like the idea. Though the way you have it implemented at the moment 
seems like a trap in that any time zlib.h is included someone also has 
to remember to add this define. I would recommend adding the define to 
the build systems instead.

Since you put in the work to find the version of zlib that added this, 
You might also want to add `required: '>= 1.2.5.2'` to the 
`dependency('zlib')` call in the main meson.build. I am wondering if we 
could remove the `z_streamp` check too. The version that it was added 
isn't obvious.

-- 
Tristan Partin
Neon (https://neon.tech)