pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedCleaning up pgcrypto/crypt-des.c to avoid compiler warnings
5+ messages / 3 participants
[nested] [flat]
Commits from this thread:
2026-09-25 e1b2a86d6e 5f664e6128 407687a0fb 35136da00e 87762cbaa4 901952844b 2a972dd5fb Modernize crypt-des.c a bit, avoiding dubious casts.
* Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings
@ 2026-09-25 17:58 Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 5+ messages in thread
From: Tom Lane @ 2026-09-25 17:58 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org
For some time now, buildfarm member serinus has been complaining
about pgcrypto/crypt-des.c:
[1601/2408] /usr/bin/ccache /usr/lib/gcc-snapshot/bin/gcc -Icontrib/pgcrypto/pgcrypto.so.p -Isrc/include -I../pgsql/src/include -I/usr/include/libxml2 -fdiagnostics-color=never -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O2 -g -fno-strict-aliasing -fwrapv -fexcess-precision=standard -fpch-deps -D_GNU_SOURCE -Wpointer-arith -Werror=vla -Wmissing-format-attribute -Wcast-function-type -Wshadow=compatible-local -Wformat-security -Wmissing-prototypes -Wold-style-declaration -Wold-style-definition -Wstrict-prototypes -Wimplicit-fallthrough=5 -Wdeclaration-after-statement -Wmissing-variable-declarations -Wno-format-truncation -Wno-stringop-truncation -O1 -ggdb -g3 -fno-omit-frame-pointer -Wall -Wextra -Wno-unused-parameter -Wno-sign-compare -Wno-missing-field-initializers -O3 -Wno-unterminated-string-initialization -Wno-clobbered -fPIC -isystem /usr/include/mit-krb5 -isystem /usr/include/mit-krb5 -pthread -fvisibility=hidden -MD -MQ contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o -MF contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o.d -o contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o -c ../pgsql/contrib/pgcrypto/crypt-des.c
../pgsql/contrib/pgcrypto/crypt-des.c: In function 'px_crypt_des':
../pgsql/contrib/pgcrypto/crypt-des.c:675:22: warning: writing 8 bytes into a region of size 7 [-Wstringop-overflow=]
675 | *q++ = *key << 1;
| ~~~~~^~~~~~~~~~~
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [1, 2] into destination object 'keybuf' of size 8
659 | keybuf[2];
| ^~~~~~
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [2, 3] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [3, 4] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [4, 5] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [5, 6] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [6, 7] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [7, 8] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:675:22: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=]
675 | *q++ = *key << 1;
| ~~~~~^~~~~~~~~~~
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset 8 into destination object 'keybuf' of size 8
659 | keybuf[2];
| ^~~~~~
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [9, 10] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [10, 11] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [11, 12] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [12, 13] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [13, 14] into destination object 'keybuf' of size 8
../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [14, 15] into destination object 'keybuf' of size 8
It's possible to see by inspection that this loop doesn't actually
overrun the buffer. But the termination condition is oddly written:
q = (uint8 *) keybuf;
while (q - (uint8 *) keybuf - 8)
I think this odd coding, perhaps combined with all the cowboy casting
that's going on here, is what's confusing gcc into giving a warning.
I propose the attached patch to make this code less ugly and (with
luck) suppress the warning.
regards, tom lane
Attachments:
v1-0001-Modernize-crypt-des.c-a-bit-avoiding-dubious-cast.patch (text/x-diff, 4.7K) download
From 15f26029193d922468ceed0c0cef11627770bcd8 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 25 Sep 2026 13:47:38 -0400
Subject: [PATCH v1] Modernize crypt-des.c a bit, avoiding dubious casts.
The header comment for this file says it assumes that "the CPU is not
picky about alignment", which we wouldn't really accept for Postgres.
The two functions that have alignment requirements have been hacked in
ways that are inconsistent and yet both ugly. des_setkey just casts
its "const char *" argument to "const uint32 *", relying on the caller
to ensure that that pointer is actually word-aligned, which the caller
does by declaring the buffer as uint32[] and then casting to "char *".
Meanwhile des_cipher carefully memcpy's to and from an internal
uint32[] buffer, which is pretty silly given that what it's passed
must be word-aligned for the benefit of des_setkey. And on top of
that we have a bunch of random casting and strangely-written pointer
arithmetic in the caller px_crypt_des, which I think is what's
confusing buildfarm member serinus into issuing scary-looking warnings
about buffer overruns.
Let's clean this up by converting the buffer variable into a union
of a uint8 array and a uint32 array, so that we can remove all these
casts, and with a little luck satisfy serinus that the code's okay.
---
contrib/pgcrypto/crypt-des.c | 50 +++++++++++++++---------------------
1 file changed, 21 insertions(+), 29 deletions(-)
diff --git a/contrib/pgcrypto/crypt-des.c b/contrib/pgcrypto/crypt-des.c
index 98c30ea122e..4afb39615f2 100644
--- a/contrib/pgcrypto/crypt-des.c
+++ b/contrib/pgcrypto/crypt-des.c
@@ -53,11 +53,6 @@
* pbox, and final permutations are inverted (this has been brought to the
* attention of the author). A list of errata for this book has been
* posted to the sci.crypt newsgroup by the author and is available for FTP.
- *
- * ARCHITECTURE ASSUMPTIONS:
- * It is assumed that the 8-byte arrays passed by reference can be
- * addressed as arrays of uint32's (ie. the CPU is not picky about
- * alignment).
*/
#include "postgres.h"
@@ -393,7 +388,7 @@ setup_salt(long salt)
}
static int
-des_setkey(const char *key)
+des_setkey(const uint32 *key)
{
uint32 k0,
k1,
@@ -405,8 +400,8 @@ des_setkey(const char *key)
if (!des_initialised)
des_init();
- rawkey0 = pg_ntoh32(*(const uint32 *) key);
- rawkey1 = pg_ntoh32(*(const uint32 *) (key + 4));
+ rawkey0 = pg_ntoh32(key[0]);
+ rawkey1 = pg_ntoh32(key[1]);
if ((rawkey0 | rawkey1)
&& rawkey0 == old_rawkey0
@@ -614,9 +609,8 @@ do_des(uint32 l_in, uint32 r_in, uint32 *l_out, uint32 *r_out, int count)
}
static int
-des_cipher(const char *in, char *out, long salt, int count)
+des_cipher(const uint32 *in, uint32 *out, long salt, int count)
{
- uint32 buffer[2];
uint32 l_out,
r_out,
rawl,
@@ -628,21 +622,15 @@ des_cipher(const char *in, char *out, long salt, int count)
setup_salt(salt);
- /* copy data to avoid assuming input is word-aligned */
- memcpy(buffer, in, sizeof(buffer));
-
- rawl = pg_ntoh32(buffer[0]);
- rawr = pg_ntoh32(buffer[1]);
+ rawl = pg_ntoh32(in[0]);
+ rawr = pg_ntoh32(in[1]);
retval = do_des(rawl, rawr, &l_out, &r_out, count);
if (retval)
return retval;
- buffer[0] = pg_hton32(l_out);
- buffer[1] = pg_hton32(r_out);
-
- /* copy data to avoid assuming output is word-aligned */
- memcpy(out, buffer, sizeof(buffer));
+ out[0] = pg_hton32(l_out);
+ out[1] = pg_hton32(r_out);
return retval;
}
@@ -655,8 +643,12 @@ px_crypt_des(const char *key, const char *setting)
salt,
l,
r0,
- r1,
- keybuf[2];
+ r1;
+ union
+ {
+ uint8 bytes[8];
+ uint32 ints[2];
+ } keybuf;
char *p;
uint8 *q;
static char output[21];
@@ -669,14 +661,14 @@ px_crypt_des(const char *key, const char *setting)
* Copy the key, shifting each character up by one bit and padding with
* zeros.
*/
- q = (uint8 *) keybuf;
- while (q - (uint8 *) keybuf - 8)
+ q = &keybuf.bytes[0];
+ while (q < &keybuf.bytes[8])
{
*q++ = *key << 1;
if (*key != '\0')
key++;
}
- if (des_setkey((char *) keybuf))
+ if (des_setkey(keybuf.ints))
return NULL;
#ifndef DISABLE_XDES
@@ -707,17 +699,17 @@ px_crypt_des(const char *key, const char *setting)
/*
* Encrypt the key with itself.
*/
- if (des_cipher((char *) keybuf, (char *) keybuf, 0L, 1))
+ if (des_cipher(keybuf.ints, keybuf.ints, 0L, 1))
return NULL;
/*
* And XOR with the next 8 characters of the key.
*/
- q = (uint8 *) keybuf;
- while (q - (uint8 *) keybuf - 8 && *key)
+ q = &keybuf.bytes[0];
+ while (q < &keybuf.bytes[8] && *key)
*q++ ^= *key++ << 1;
- if (des_setkey((char *) keybuf))
+ if (des_setkey(keybuf.ints))
return NULL;
}
strlcpy(output, setting, 10);
--
2.52.0
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings
@ 2026-09-25 18:37 Ayush Tiwari <ayushtiwari.slg01@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 5+ messages in thread
From: Ayush Tiwari @ 2026-09-25 18:37 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@lists.postgresql.org
Hi,
On Fri, 25 Sept 2026 at 23:28, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> For some time now, buildfarm member serinus has been complaining
> about pgcrypto/crypt-des.c:
>
> [1601/2408] /usr/bin/ccache /usr/lib/gcc-snapshot/bin/gcc
-Icontrib/pgcrypto/pgcrypto.so.p -Isrc/include -I../pgsql/src/include
-I/usr/include/libxml2 -fdiagnostics-color=never -D_FILE_OFFSET_BITS=64
-Wall -Winvalid-pch -O2 -g -fno-strict-aliasing -fwrapv
-fexcess-precision=standard -fpch-deps -D_GNU_SOURCE -Wpointer-arith
-Werror=vla -Wmissing-format-attribute -Wcast-function-type
-Wshadow=compatible-local -Wformat-security -Wmissing-prototypes
-Wold-style-declaration -Wold-style-definition -Wstrict-prototypes
-Wimplicit-fallthrough=5 -Wdeclaration-after-statement
-Wmissing-variable-declarations -Wno-format-truncation
-Wno-stringop-truncation -O1 -ggdb -g3 -fno-omit-frame-pointer -Wall
-Wextra -Wno-unused-parameter -Wno-sign-compare
-Wno-missing-field-initializers -O3 -Wno-unterminated-string-initialization
-Wno-clobbered -fPIC -isystem /usr/include/mit-krb5 -isystem
/usr/include/mit-krb5 -pthread -fvisibility=hidden -MD -MQ
contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o -MF
contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o.d -o
contrib/pgcrypto/pgcrypto.so.p/crypt-des.c.o -c
../pgsql/contrib/pgcrypto/crypt-des.c
> ../pgsql/contrib/pgcrypto/crypt-des.c: In function 'px_crypt_des':
> ../pgsql/contrib/pgcrypto/crypt-des.c:675:22: warning: writing 8 bytes
into a region of size 7 [-Wstringop-overflow=]
> 675 | *q++ = *key << 1;
> | ~~~~~^~~~~~~~~~~
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [1, 2] into
destination object 'keybuf' of size 8
> 659 | keybuf[2];
> | ^~~~~~
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [2, 3] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [3, 4] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [4, 5] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [5, 6] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [6, 7] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [7, 8] into
destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:675:22: warning: writing 1 byte
into a region of size 0 [-Wstringop-overflow=]
> 675 | *q++ = *key << 1;
> | ~~~~~^~~~~~~~~~~
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset 8 into
destination object 'keybuf' of size 8
> 659 | keybuf[2];
> | ^~~~~~
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [9, 10]
into destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [10, 11]
into destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [11, 12]
into destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [12, 13]
into destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [13, 14]
into destination object 'keybuf' of size 8
> ../pgsql/contrib/pgcrypto/crypt-des.c:659:33: note: at offset [14, 15]
into destination object 'keybuf' of size 8
>
> It's possible to see by inspection that this loop doesn't actually
> overrun the buffer. But the termination condition is oddly written:
>
> q = (uint8 *) keybuf;
> while (q - (uint8 *) keybuf - 8)
>
> I think this odd coding, perhaps combined with all the cowboy casting
> that's going on here, is what's confusing gcc into giving a warning.
> I propose the attached patch to make this code less ugly and (with
> luck) suppress the warning.
Yeah I agree that it's pretty ugly.
We had an open patch for that obscure loop earlier[1].
(I'll close that commitfest item)
I reviewed your patch and it looks good and clearer to me.
Thanks for the patch.
Regards,
Ayush
[1]
https://www.postgresql.org/message-id/CAJTYsWWkNO0fKZit%2B_hRUUgrP-Gyu6pGDT5w0w%2B4dYOM7HMF1Q%40mail...
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings
@ 2026-09-25 18:54 Nathan Bossart <nathandbossart@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 5+ messages in thread
From: Nathan Bossart @ 2026-09-25 18:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers@lists.postgresql.org
On Fri, Sep 25, 2026 at 01:58:17PM -0400, Tom Lane wrote:
> I think this odd coding, perhaps combined with all the cowboy casting
> that's going on here, is what's confusing gcc into giving a warning.
> I propose the attached patch to make this code less ugly and (with
> luck) suppress the warning.
Getting rid of the "q" variable altogether might have a better chance of
clearing the warnings, and IMHO it further improves readability:
> + q = &keybuf.bytes[0];
> + while (q < &keybuf.bytes[8])
> {
> *q++ = *key << 1;
for (i = 0; i < 8; i++)
{
keybuf.bytes[i] = *key << 1;
> + q = &keybuf.bytes[0];
> + while (q < &keybuf.bytes[8] && *key)
> *q++ ^= *key++ << 1;
for (i = 0; i < 8 && *key; i++)
keybuf.bytes[i] ^= *key++ << 1;
--
nathan
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings
@ 2026-09-25 19:18 Tom Lane <tgl@sss.pgh.pa.us>
parent: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2026-09-25 19:18 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> On Fri, 25 Sept 2026 at 23:28, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> For some time now, buildfarm member serinus has been complaining
>> about pgcrypto/crypt-des.c:
>> ...
>> I think this odd coding, perhaps combined with all the cowboy casting
>> that's going on here, is what's confusing gcc into giving a warning.
>> I propose the attached patch to make this code less ugly and (with
>> luck) suppress the warning.
> Yeah I agree that it's pretty ugly.
> We had an open patch for that obscure loop earlier[1].
> (I'll close that commitfest item)
Oh! I'd missed that thread. Thank you for pointing me to it, because
I had been unable to reproduce the warning, but now I know that it's
necessary to use -O3 along with a recent gcc to see it. Armed with
that knowledge, I find that my patch does not suppress the warning :-(
I'll use this variant of your proposal instead:
for (size_t q = 0; q < lengthof(keybuf.bytes); q++)
{
keybuf.bytes[q] = *key << 1;
...
That does suppress the warning, at least with the gcc 16.2.1 that
I'm testing with.
Thanks for the review!
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings
@ 2026-09-25 19:39 Tom Lane <tgl@sss.pgh.pa.us>
parent: Nathan Bossart <nathandbossart@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2026-09-25 19:39 UTC (permalink / raw)
To: Nathan Bossart <nathandbossart@gmail.com>; +Cc: pgsql-hackers@lists.postgresql.org
Nathan Bossart <nathandbossart@gmail.com> writes:
> On Fri, Sep 25, 2026 at 01:58:17PM -0400, Tom Lane wrote:
>> I think this odd coding, perhaps combined with all the cowboy casting
>> that's going on here, is what's confusing gcc into giving a warning.
>> I propose the attached patch to make this code less ugly and (with
>> luck) suppress the warning.
> Getting rid of the "q" variable altogether might have a better chance of
> clearing the warnings, and IMHO it further improves readability:
Yeah, I adopted something close to Ayush's variant of that.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2026-09-25 19:39 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-25 17:58 Cleaning up pgcrypto/crypt-des.c to avoid compiler warnings Tom Lane <tgl@sss.pgh.pa.us>
2026-09-25 18:37 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-09-25 19:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-09-25 18:54 ` Nathan Bossart <nathandbossart@gmail.com>
2026-09-25 19:39 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox