Skip to content

Commit 92cf557

Browse files
committed
Add static assertion that RELSEG_SIZE fits in an int.
Our configure script intended to ensure this, but it supposed that expr(1) would report an error for integer overflow. Maybe that was true when the code was written (commit 3c6248a of 2008-05-02), but all the modern expr's I tried will deliver bigger-than-int32 results without complaint. Moreover, if you use --with-segsize-blocks then there's no check at all. Ideally we'd add a test in configure itself to check that the value fits in int, but to do that we'd need to suppose that test(1) handles bigger-than-int32 numbers correctly. Probably modern ones do, but that's an assumption I could do without; and I'm not too trusting about meson either. Instead, let's install a static assertion, so that even people who ignore all the compiler warnings you get from such values will be forced to confront the fact that it won't work. This has been hazardous for awhile, but given that we hadn't heard a complaint about it till now, I don't feel a need to back-patch. Reported-by: Casey Shobe <casey.allen.shobe@icloud.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/C5DC82D6-C76D-4E8F-BC2E-DF03EFC4FA24@icloud.com
1 parent 277dec6 commit 92cf557

File tree

1 file changed

+10
-0
lines changed
  • src/backend/storage/smgr

1 file changed

+10
-0
lines changed

src/backend/storage/smgr/md.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
#include "postgres.h"
2323

24+
#include <limits.h>
2425
#include <unistd.h>
2526
#include <fcntl.h>
2627
#include <sys/file.h>
@@ -65,6 +66,15 @@
6566
* out to an unlinked old copy of a segment file that will eventually
6667
* disappear.
6768
*
69+
* RELSEG_SIZE must fit into BlockNumber; but since we expose its value
70+
* as an integer GUC, it actually needs to fit in signed int. It's worth
71+
* having a cross-check for this since configure's --with-segsize options
72+
* could let people select insane values.
73+
*/
74+
StaticAssertDecl(RELSEG_SIZE > 0 && RELSEG_SIZE <= INT_MAX,
75+
"RELSEG_SIZE must fit in an integer");
76+
77+
/*
6878
* File descriptors are stored in the per-fork md_seg_fds arrays inside
6979
* SMgrRelation. The length of these arrays is stored in md_num_open_segs.
7080
* Note that a fork's md_num_open_segs having a specific value does not

0 commit comments

Comments
 (0)