Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8c95ae8

Browse files
committed
Suppress compiler warnings about useless comparison of unsigned to zero.
Reportedly, some compilers warn about tests like "c < 0" if c is unsigned, and hence complain about the character range checks I added in commit 3bb3f42. This is a bit of a pain since the regex library doesn't really want to assume that chr is unsigned. However, since any such reconfiguration would involve manual edits of regcustom.h anyway, we can put it on the shoulders of whoever wants to do that to adjust this new range-checking macro correctly. Per gripes from Coverity and Andres.
1 parent db76b1e commit 8c95ae8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/backend/regex/regc_lex.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,13 @@ lexescape(struct vars * v)
813813
break;
814814
case CHR('u'):
815815
c = lexdigits(v, 16, 4, 4);
816-
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
816+
if (ISERR() || !CHR_IS_IN_RANGE(c))
817817
FAILW(REG_EESCAPE);
818818
RETV(PLAIN, c);
819819
break;
820820
case CHR('U'):
821821
c = lexdigits(v, 16, 8, 8);
822-
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
822+
if (ISERR() || !CHR_IS_IN_RANGE(c))
823823
FAILW(REG_EESCAPE);
824824
RETV(PLAIN, c);
825825
break;
@@ -837,7 +837,7 @@ lexescape(struct vars * v)
837837
case CHR('x'):
838838
NOTE(REG_UUNPORT);
839839
c = lexdigits(v, 16, 1, 255); /* REs >255 long outside spec */
840-
if (ISERR() || c < CHR_MIN || c > CHR_MAX)
840+
if (ISERR() || !CHR_IS_IN_RANGE(c))
841841
FAILW(REG_EESCAPE);
842842
RETV(PLAIN, c);
843843
break;

src/include/regex/regcustom.h

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ typedef int celt; /* type to hold chr, or NOCELT */
6868
#define CHR_MAX 0x7ffffffe /* CHR_MAX-CHR_MIN+1 must fit in an int, and
6969
* CHR_MAX+1 must fit in both chr and celt */
7070

71+
/*
72+
* Check if a chr value is in range. Ideally we'd just write this as
73+
* ((c) >= CHR_MIN && (c) <= CHR_MAX)
74+
* However, if chr is unsigned and CHR_MIN is zero, the first part of that
75+
* is a no-op, and certain overly-nannyish compilers give warnings about it.
76+
* So we leave that out here. If you want to make chr signed and/or CHR_MIN
77+
* not zero, redefine this macro as above. Callers should assume that the
78+
* macro may multiply evaluate its argument, even though it does not today.
79+
*/
80+
#define CHR_IS_IN_RANGE(c) ((c) <= CHR_MAX)
81+
7182
/* functions operating on chr */
7283
#define iscalnum(x) pg_wc_isalnum(x)
7384
#define iscalpha(x) pg_wc_isalpha(x)

0 commit comments

Comments
 (0)