Improve const use in zlib-using code

Started by Peter Eisentrautover 2 years ago5 messages
#1Peter Eisentraut
peter@eisentraut.org
1 attachment(s)

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.

Attachments:

0001-Improve-const-use-in-zlib-using-code.patchtext/plain; charset=UTF-8; name=0001-Improve-const-use-in-zlib-using-code.patchDownload
From 324e37adff4c51f4f10807386c0c098cb8d21608 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
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

#2Tristan Partin
tristan@neon.tech
In reply to: Peter Eisentraut (#1)
Re: Improve const use in zlib-using code

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)

#3Peter Eisentraut
peter@eisentraut.org
In reply to: Tristan Partin (#2)
2 attachment(s)
Re: Improve const use in zlib-using code

On 02.08.23 16:39, Tristan Partin wrote:

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.

Ok, moved to c.h.

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.

Well, it's not a hard requirement, so I think this is not necessary.

I am wondering if we
could remove the `z_streamp` check too. The version that it was added
isn't obvious.

Yeah, that appears to be very obsolete. I have made an additional patch
to remove that.

Attachments:

v2-0001-Improve-const-use-in-zlib-using-code.patchtext/plain; charset=UTF-8; name=v2-0001-Improve-const-use-in-zlib-using-code.patchDownload
From 81f053c68109d85ae657f6d9f652d90f1d55fb2a Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 3 Aug 2023 08:47:02 +0200
Subject: [PATCH v2 1/2] 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         | 2 +-
 src/bin/pg_basebackup/bbstreamer_gzip.c | 2 +-
 src/bin/pg_basebackup/walmethods.c      | 5 ++---
 src/include/c.h                         | 5 +++++
 4 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/contrib/pgcrypto/pgp-compress.c b/contrib/pgcrypto/pgp-compress.c
index 086bec31ae..961cf21e74 100644
--- a/contrib/pgcrypto/pgp-compress.c
+++ b/contrib/pgcrypto/pgp-compress.c
@@ -113,7 +113,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/bin/pg_basebackup/bbstreamer_gzip.c b/src/bin/pg_basebackup/bbstreamer_gzip.c
index 3bdbfa0bc4..fb25fef150 100644
--- a/src/bin/pg_basebackup/bbstreamer_gzip.c
+++ b/src/bin/pg_basebackup/bbstreamer_gzip.c
@@ -269,7 +269,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/walmethods.c b/src/bin/pg_basebackup/walmethods.c
index 376ddf72b7..965d49dec4 100644
--- a/src/bin/pg_basebackup/walmethods.c
+++ b/src/bin/pg_basebackup/walmethods.c
@@ -705,7 +705,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 +782,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/include/c.h b/src/include/c.h
index f69d739be5..82f8e9d4c7 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -75,6 +75,11 @@
 #include <libintl.h>
 #endif
 
+/* Define before including zlib.h to add const decorations to zlib API. */
+#ifdef HAVE_LIBZ
+#define ZLIB_CONST
+#endif
+
 
 /* ----------------------------------------------------------------
  *				Section 1: compiler characteristics
-- 
2.41.0

v2-0002-Remove-check-for-z_streamp.patchtext/plain; charset=UTF-8; name=v2-0002-Remove-check-for-z_streamp.patchDownload
From 76fb004fa151746d584816befb3a070570697298 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Thu, 3 Aug 2023 08:52:30 +0200
Subject: [PATCH v2 2/2] Remove check for z_streamp

This is surely obsolete.  zlib version 1.0.4, which includes
z_streamp, was released 1996-07-24.  When this check was put in in
2001 (19c97b8579), it was already labeling that release ancient.
---
 configure    | 15 ---------------
 configure.ac |  9 ---------
 meson.build  |  8 --------
 3 files changed, 32 deletions(-)

diff --git a/configure b/configure
index 2e518c8007..963fbbcf1e 100755
--- a/configure
+++ b/configure
@@ -15162,21 +15162,6 @@ _ACEOF
 fi
 
 
-if test "$with_zlib" = yes; then
-  # Check that <zlib.h> defines z_streamp (versions before about 1.0.4
-  # did not).  While we could work around the lack of z_streamp, it
-  # seems unwise to encourage people to use such old zlib versions...
-  ac_fn_c_check_type "$LINENO" "z_streamp" "ac_cv_type_z_streamp" "#include <zlib.h>
-"
-if test "x$ac_cv_type_z_streamp" = xyes; then :
-
-else
-  as_fn_error $? "zlib version is too old
-Use --without-zlib to disable zlib support." "$LINENO" 5
-fi
-
-fi
-
 case $host_cpu in
   x86_64)
     # On x86_64, check if we can compile a popcntq instruction
diff --git a/configure.ac b/configure.ac
index 3ebe1a796d..5153b8b3fd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1678,15 +1678,6 @@ AC_CHECK_TYPES([struct option], [], [],
 #include <getopt.h>
 #endif])
 
-if test "$with_zlib" = yes; then
-  # Check that <zlib.h> defines z_streamp (versions before about 1.0.4
-  # did not).  While we could work around the lack of z_streamp, it
-  # seems unwise to encourage people to use such old zlib versions...
-  AC_CHECK_TYPE(z_streamp, [], [AC_MSG_ERROR([zlib version is too old
-Use --without-zlib to disable zlib support.])],
-                [#include <zlib.h>])
-fi
-
 case $host_cpu in
   x86_64)
     # On x86_64, check if we can compile a popcntq instruction
diff --git a/meson.build b/meson.build
index 04ea348852..0a11efc97a 100644
--- a/meson.build
+++ b/meson.build
@@ -1384,14 +1384,6 @@ if not zlibopt.disabled()
       args: test_c_args, include_directories: postgres_inc,
       dependencies: [zlib_t], required: zlibopt)
     warning('zlib header not found')
-  elif not cc.has_type('z_streamp',
-      dependencies: [zlib_t], prefix: '#include <zlib.h>',
-      args: test_c_args, include_directories: postgres_inc)
-    if zlibopt.enabled()
-      error('zlib version is too old')
-    else
-      warning('zlib version is too old')
-    endif
   else
     zlib = zlib_t
   endif
-- 
2.41.0

#4Tristan Partin
tristan@neon.tech
In reply to: Peter Eisentraut (#3)
Re: Improve const use in zlib-using code

Both patches look good to me.

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

#5Peter Eisentraut
peter@eisentraut.org
In reply to: Tristan Partin (#4)
Re: Improve const use in zlib-using code

On 03.08.23 16:30, Tristan Partin wrote:

Both patches look good to me.

committed, thanks