Skip to content

Commit d3e83a7

Browse files
authored
Fix three minor issues in pcre2test (#798)
- A dangerous STRLEN macro was casting only part of its argument, which lead to unexpected behaviour in one instance (luckily harmless) - A "compiler warning suppression" from a previous version of the code, which is definitely now obsolete. - A belt-and-braces check which attempted to add a null terminator, but which was only writing a single byte. Thankfully no code actually reads the terminator anyway.
1 parent 379123d commit d3e83a7

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/pcre2test.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,11 @@ are supported. */
11681168
(test_mode == PCRE16_MODE)? (uint32_t)(((PCRE2_SPTR16)(a))[b]) : \
11691169
(uint32_t)(((PCRE2_SPTR32)(a))[b]))
11701170

1171+
#define INCSTR(x,offset) ( \
1172+
(test_mode == PCRE8_MODE)? (void *)((PCRE2_SPTR8)(x)+offset) : \
1173+
(test_mode == PCRE16_MODE)? (void *)((PCRE2_SPTR16)(x)+offset) : \
1174+
(void *)((PCRE2_SPTR32)(x)+offset))
1175+
11711176
#define CONCTXCPY(a,b) \
11721177
if (test_mode == PCRE8_MODE) \
11731178
memcpy(G(a,8),G(b,8),sizeof(pcre2_convert_context_8)); \
@@ -1719,9 +1724,9 @@ are supported. */
17191724
else \
17201725
G(x,32) = (uint32_t *)(y)
17211726

1722-
#define STRLEN(p) ((test_mode == PCRE8_MODE)? strlen((char *)p) : \
1723-
(test_mode == PCRE16_MODE)? strlen16((PCRE2_SPTR16)p) : \
1724-
strlen32((PCRE2_SPTR32)p))
1727+
#define STRLEN(p) ((test_mode == PCRE8_MODE)? strlen((char *)(p)) : \
1728+
(test_mode == PCRE16_MODE)? strlen16((PCRE2_SPTR16)(p)) : \
1729+
strlen32((PCRE2_SPTR32)(p)))
17251730

17261731
#define SUB1(a,b) \
17271732
if (test_mode == PCRE8_MODE) G(a,8)(G(b,8)); \
@@ -1789,6 +1794,11 @@ the three different cases. */
17891794
(uint32_t)(((G(PCRE2_SPTR,BITONE))(a))[b]) : \
17901795
(uint32_t)(((G(PCRE2_SPTR,BITTWO))(a))[b]))
17911796

1797+
#define INCSTR(x,offset) ( \
1798+
(test_mode == G(G(PCRE,BITONE),_MODE))? \
1799+
(void *)((G(PCRE2_SPTR,BITONE))(x)+offset) : \
1800+
(void *)((G(PCRE2_SPTR,BITTWO))(x)+offset))
1801+
17921802
#define CONCTXCPY(a,b) \
17931803
if (test_mode == G(G(PCRE,BITONE),_MODE)) \
17941804
memcpy(G(a,BITONE),G(b,BITONE),sizeof(G(pcre2_convert_context_,BITONE))); \
@@ -2239,8 +2249,8 @@ the three different cases. */
22392249
G(x,BITTWO) = (G(G(uint,BITTWO),_t) *)(y)
22402250

22412251
#define STRLEN(p) ((test_mode == G(G(PCRE,BITONE),_MODE))? \
2242-
G(strlen,BITONE)((G(PCRE2_SPTR,BITONE))p) : \
2243-
G(strlen,BITTWO)((G(PCRE2_SPTR,BITTWO))p))
2252+
G(strlen,BITONE)((G(PCRE2_SPTR,BITONE))(p)) : \
2253+
G(strlen,BITTWO)((G(PCRE2_SPTR,BITTWO))(p)))
22442254

22452255
#define SUB1(a,b) \
22462256
if (test_mode == G(G(PCRE,BITONE),_MODE)) \
@@ -2274,6 +2284,7 @@ the three different cases. */
22742284
#define CASTFLD(t,a,b) (t)(G(a,8)->b)
22752285
#define CASTVAR(t,x) (t)G(x,8)
22762286
#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR8)(a))[b])
2287+
#define INCSTR(x,offset) (void *)((PCRE2_SPTR8)(x)+offset)
22772288
#define CONCTXCPY(a,b) memcpy(G(a,8),G(b,8),sizeof(pcre2_convert_context_8))
22782289
#define CONVERT_COPY(a,b,c) memcpy(G(a,8),(char *)b, c)
22792290
#define DATCTXCPY(a,b) memcpy(G(a,8),G(b,8),sizeof(pcre2_match_context_8))
@@ -2380,7 +2391,7 @@ the three different cases. */
23802391
#define SETFLD(x,y,z) G(x,8)->y = z
23812392
#define SETOP(x,y,z) G(x,8) z y
23822393
#define SETCASTPTR(x,y) G(x,8) = (uint8_t *)(y)
2383-
#define STRLEN(p) strlen((char *)p)
2394+
#define STRLEN(p) strlen((char *)(p))
23842395
#define SUB1(a,b) G(a,8)(G(b,8))
23852396
#define SUB2(a,b,c) G(a,8)(G(b,8),G(c,8))
23862397
#define TEST(x,r,y) (G(x,8) r (y))
@@ -2393,6 +2404,7 @@ the three different cases. */
23932404
#define CASTFLD(t,a,b) (t)(G(a,16)->b)
23942405
#define CASTVAR(t,x) (t)G(x,16)
23952406
#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR16)(a))[b])
2407+
#define INCSTR(x,offset) (void *)((PCRE2_SPTR16)(x)+offset)
23962408
#define CONCTXCPY(a,b) memcpy(G(a,16),G(b,16),sizeof(pcre2_convert_context_16))
23972409
#define CONVERT_COPY(a,b,c) memcpy(G(a,16),(char *)b, (c)*2)
23982410
#define DATCTXCPY(a,b) memcpy(G(a,16),G(b,16),sizeof(pcre2_match_context_16))
@@ -2497,7 +2509,7 @@ the three different cases. */
24972509
#define SETFLD(x,y,z) G(x,16)->y = z
24982510
#define SETOP(x,y,z) G(x,16) z y
24992511
#define SETCASTPTR(x,y) G(x,16) = (uint16_t *)(y)
2500-
#define STRLEN(p) strlen16((PCRE2_SPTR16)p)
2512+
#define STRLEN(p) strlen16((PCRE2_SPTR16)(p))
25012513
#define SUB1(a,b) G(a,16)(G(b,16))
25022514
#define SUB2(a,b,c) G(a,16)(G(b,16),G(c,16))
25032515
#define TEST(x,r,y) (G(x,16) r (y))
@@ -2510,6 +2522,7 @@ the three different cases. */
25102522
#define CASTFLD(t,a,b) (t)(G(a,32)->b)
25112523
#define CASTVAR(t,x) (t)G(x,32)
25122524
#define CODE_UNIT(a,b) (uint32_t)(((PCRE2_SPTR32)(a))[b])
2525+
#define INCSTR(x,offset) (void *)((PCRE2_SPTR32)(x)+offset)
25132526
#define CONCTXCPY(a,b) memcpy(G(a,32),G(b,32),sizeof(pcre2_convert_context_32))
25142527
#define CONVERT_COPY(a,b,c) memcpy(G(a,32),(char *)b, (c)*4)
25152528
#define DATCTXCPY(a,b) memcpy(G(a,32),G(b,32),sizeof(pcre2_match_context_32))
@@ -2614,7 +2627,7 @@ the three different cases. */
26142627
#define SETFLD(x,y,z) G(x,32)->y = z
26152628
#define SETOP(x,y,z) G(x,32) z y
26162629
#define SETCASTPTR(x,y) G(x,32) = (uint32_t *)(y)
2617-
#define STRLEN(p) strlen32((PCRE2_SPTR32)p)
2630+
#define STRLEN(p) strlen32((PCRE2_SPTR32)(p))
26182631
#define SUB1(a,b) G(a,32)(G(b,32))
26192632
#define SUB2(a,b,c) G(a,32)(G(b,32),G(c,32))
26202633
#define TEST(x,r,y) (G(x,32) r (y))
@@ -5337,7 +5350,7 @@ if ((pat_patctl.control & CTL_INFO) != 0)
53375350
for (; namecount > 0; namecount--)
53385351
{
53395352
int imm2_size = test_mode == PCRE8_MODE ? 2 : 1;
5340-
size_t length = STRLEN(nametable + imm2_size);
5353+
size_t length = STRLEN(INCSTR(nametable, imm2_size));
53415354
fprintf(outfile, " ");
53425355

53435356
/* In UTF mode the name may be a UTF string containing non-ASCII
@@ -9109,9 +9122,6 @@ for (gmatched = 0;; gmatched++)
91099122

91109123
if ((dat_datctl.control & (CTL_FINDLIMITS|CTL_FINDLIMITS_NOHEAP)) != 0)
91119124
{
9112-
capcount = 0; /* This stops compiler warnings */
9113-
(void)capcount;
9114-
91159125
if ((dat_datctl.control & CTL_FINDLIMITS_NOHEAP) == 0 &&
91169126
(FLD(compiled_code, executable_jit) == NULL ||
91179127
(dat_datctl.options & PCRE2_NO_JIT) != 0))
@@ -9250,20 +9260,20 @@ for (gmatched = 0;; gmatched++)
92509260

92519261
/* The result of the match is now in capcount. First handle a successful
92529262
match. If pp was forced to be NULL (to test NULL handling) it will have been
9253-
treated as an empty string if the length was zero. So re-create that for
9254-
outputting. Don't just point to "" because that leads to a "loss of const"
9255-
warning. */
9263+
treated as an empty string if the length was zero. So, re-create that for
9264+
outputting, preserving the invariant that pp is a valid pointer to a region
9265+
of length len followed by a null. */
92569266

92579267
if (capcount >= 0)
92589268
{
92599269
if (pp == NULL)
92609270
{
92619271
#ifdef SUPPORT_VALGRIND
92629272
/* Mark the start of dbuffer addressable again. */
9263-
VALGRIND_MAKE_MEM_UNDEFINED(dbuffer, 1);
9273+
VALGRIND_MAKE_MEM_UNDEFINED(dbuffer, code_unit_size);
92649274
#endif
92659275
pp = dbuffer;
9266-
pp[0] = 0;
9276+
memset(pp, 0, code_unit_size);
92679277
}
92689278

92699279
if ((unsigned)capcount > oveccount) /* Check for lunatic return value */

0 commit comments

Comments
 (0)