Re: Improve const use in zlib-using code

2023-08-07 Thread Peter Eisentraut

On 03.08.23 16:30, Tristan Partin wrote:

Both patches look good to me.


committed, thanks





Re: Improve const use in zlib-using code

2023-08-03 Thread Tristan Partin

Both patches look good to me.

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




Re: Improve const use in zlib-using code

2023-08-03 Thread Peter Eisentraut

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.
From 81f053c68109d85ae657f6d9f652d90f1d55fb2a Mon Sep 17 00:00:00 2001
From: Peter Eisentraut 
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 = >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 
 #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

From 76fb004fa151746d584816befb3a070570697298 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut 
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  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 s

Re: Improve const use in zlib-using code

2023-08-02 Thread Tristan Partin

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)




Improve const use in zlib-using code

2023-08-02 Thread Peter Eisentraut
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 
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 
 
 #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 
 #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 
 
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include 
 #endif
 
@@ -269,7 +270,7 @@ bbstreamer_gzip_decompressor_content(bbstreamer *streamer,
mystreamer = (bbstreamer_gzip_decompressor *) streamer;
 
zs = >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 
 #include 
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include 
 #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 
 #endif
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include 
 #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 
 #endif
 #ifdef HAVE_LIBZ
+#define ZLIB_CONST
 #include 
 #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->