Skip to content

Commit

Permalink
A futher round of minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NWilson committed Nov 29, 2024
1 parent c4551c5 commit 6c5b9f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
27 changes: 14 additions & 13 deletions src/pcre2_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6178,8 +6178,8 @@ for (;; pptr++)
{
eclass_op_info op_info;
PCRE2_SIZE previous_length = (lengthptr != NULL)? *lengthptr : 0;
int classones = 0;
int classzeros = 0;
BOOL allbitsone = TRUE;
BOOL allbitszero = TRUE;

previous = code;
*code++ = OP_ECLASS;
Expand All @@ -6200,10 +6200,11 @@ for (;; pptr++)
}

/* Do some useful counting of what's in the bitmap. */
while (classones < 8 && op_info.bits.classwords[classones] == 0xffffffff)
classones += 32;
while (classzeros < 8 && op_info.bits.classwords[classzeros] == 0)
classzeros += 32;
for (int i = 0; i < 8; i++)
if (op_info.bits.classwords[i] != 0xffffffff)
{ allbitsone = FALSE; break; }
for (int i = 0; i < 8; i++)
if (op_info.bits.classwords[i] != 0) { allbitszero = FALSE; break; }

/* After constant-folding the extended class syntax, it may turn out to be
a simple class after all. In that case, we can unwrap it from the
Expand All @@ -6222,7 +6223,7 @@ for (;; pptr++)
/* If the bits are all ones, and the "high characters" are all matched
too, we use a special-cased encoding of OP_ALLANY. */

if (op_info.op_single_type == ECL_ANY && classones == 256)
if (op_info.op_single_type == ECL_ANY && allbitsone)
{
if (lengthptr == NULL) *code++ = OP_ALLANY;
}
Expand All @@ -6239,7 +6240,7 @@ for (;; pptr++)
{
/* Don't unconditionally request 32 more bytes - we probably already
reserved that much space inside compile_class_nested(). */
if (required_len < (*lengthptr - previous_length))
if (required_len > (*lengthptr - previous_length))
{
*lengthptr = previous_length + required_len;
}
Expand All @@ -6259,13 +6260,13 @@ for (;; pptr++)
else
{
PCRE2_ASSERT(op_info.op_single_type == ECL_XCLASS);
BOOL need_map = classzeros != 256;
BOOL need_map = !allbitszero;
PCRE2_SIZE required_len = op_info.length +
(need_map? 32/sizeof(PCRE2_UCHAR) : 0);

if (lengthptr != NULL)
{
if (required_len < (*lengthptr - previous_length))
if (required_len > (*lengthptr - previous_length))
{
*lengthptr = previous_length + required_len;
}
Expand All @@ -6288,7 +6289,7 @@ for (;; pptr++)

/* Finally write the header data. */
*code++ = OP_XCLASS;
PUT(code, 0, required_len);
PUT(code, 0, (int)required_len);
code += LINK_SIZE;
*code++ = flags | (need_map? XCL_MAP : 0);
if (need_map)
Expand All @@ -6307,13 +6308,13 @@ for (;; pptr++)
#ifdef SUPPORT_WIDE_CHARS
else
{
BOOL need_map = classzeros != 256;
BOOL need_map = !allbitszero;
PCRE2_SIZE required_len = 1 + LINK_SIZE + 1 +
(need_map? 32/sizeof(PCRE2_UCHAR) : 0) + op_info.length;

if (lengthptr != NULL)
{
if (required_len < (*lengthptr - previous_length))
if (required_len > (*lengthptr - previous_length))
{
*lengthptr = previous_length + required_len;
}
Expand Down
20 changes: 12 additions & 8 deletions src/pcre2_compile_class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,8 @@ else if (pop_info->op_single_type == ECL_ANY ||
}
else
{
PCRE2_ASSERT(pop_info->op_single_type == ECL_XCLASS);
PCRE2_ASSERT(pop_info->op_single_type == ECL_XCLASS &&
pop_info->length >= 1 + LINK_SIZE + 1);
if (lengthptr == NULL)
pop_info->code_start[1 + LINK_SIZE] ^= XCL_NOT;
}
Expand Down Expand Up @@ -1913,7 +1914,8 @@ else if (lhs_op_info->op_single_type == ECL_NONE)
{
/* no-op: drop the LHS, and memmove the RHS into its place */
if (lengthptr == NULL)
memmove(lhs_op_info->code_start, rhs_op_info->code_start, rhs_op_info->length);
memmove(lhs_op_info->code_start, rhs_op_info->code_start,
rhs_op_info->length);
lhs_op_info->length = rhs_op_info->length;
lhs_op_info->op_single_type = rhs_op_info->op_single_type;
}
Expand Down Expand Up @@ -1968,7 +1970,8 @@ else if (lhs_op_info->op_single_type == ECL_NONE)
{
/* no-op: drop the LHS, and memmove the RHS into its place */
if (lengthptr == NULL)
memmove(lhs_op_info->code_start, rhs_op_info->code_start, rhs_op_info->length);
memmove(lhs_op_info->code_start, rhs_op_info->code_start,
rhs_op_info->length);
lhs_op_info->length = rhs_op_info->length;
lhs_op_info->op_single_type = rhs_op_info->op_single_type;
}
Expand All @@ -1982,7 +1985,8 @@ else if (lhs_op_info->op_single_type == ECL_ANY)
/* the result is !RHS: drop the LHS, memmove the RHS into its place, and
fold in the negation */
if (lengthptr == NULL)
memmove(lhs_op_info->code_start, rhs_op_info->code_start, rhs_op_info->length);
memmove(lhs_op_info->code_start, rhs_op_info->code_start,
rhs_op_info->length);
lhs_op_info->length = rhs_op_info->length;
lhs_op_info->op_single_type = rhs_op_info->op_single_type;

Expand Down Expand Up @@ -2113,7 +2117,7 @@ switch (meta)
(*code_start == OP_CLASS)? ECL_NONE : ECL_ANY;
memcpy(pop_info->bits.classbits, code_start + 1, 32);
/* Rewind the code pointer, but make sure we adjust *lengthptr, because we
do need reserve that space (even though we only use it temporarily). */
do need to reserve that space (even though we only use it temporarily). */
if (lengthptr != NULL)
*lengthptr += code - (code_start + 1);
code = code_start + 1;
Expand Down Expand Up @@ -2141,7 +2145,7 @@ switch (meta)
code - code_start >= 1 + LINK_SIZE + 1 + 32 / sizeof(PCRE2_UCHAR));

put_length = GET(code_start, 1) - 32 / sizeof(PCRE2_UCHAR);
PUT(code_start, 1, put_length);
PUT(code_start, 1, (int)put_length);
code_start[1 + LINK_SIZE] &= ~XCL_MAP;

map_start = code_start + 1 + LINK_SIZE + 1;
Expand All @@ -2150,7 +2154,7 @@ switch (meta)
memmove(map_start, data_start, (code - data_start) * sizeof(PCRE2_UCHAR));

/* Rewind the code pointer, but make sure we adjust *lengthptr, because we
do need reserve that space (even though we only use it temporarily). */
do need to reserve that space (even though we only use it temporarily). */
if (lengthptr != NULL)
*lengthptr += 32 / sizeof(PCRE2_UCHAR);
code -= 32 / sizeof(PCRE2_UCHAR);
Expand Down Expand Up @@ -2181,7 +2185,7 @@ PCRE2_ASSERT(lengthptr == NULL || (code == code_start));
*pcode = code;
return TRUE;

// XXX produce some manual tests to verify that it's OK to chuck out unconsumed
// XXX produce some manual tests to verify that it's OK to leave some unconsumed
// tokens? should crash/fail even without debug assertions
}

Expand Down
3 changes: 1 addition & 2 deletions src/pcre2_printint.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ const char *yield = "??";
size_t len = 0;
unsigned int ptypex = (ptype == PT_SC)? PT_SCX : ptype;

for (int i = PRIV(utt_size) - 1; i >= 0; i--)
for (ptrdiff_t i = PRIV(utt_size) - 1; i >= 0; i--)
{
const ucp_type_table *u = PRIV(utt) + i;

Expand Down Expand Up @@ -521,7 +521,6 @@ print_class(FILE *f, int type, PCRE2_SPTR code, const uint8_t *char_lists_end,
{
BOOL printmap, negated;
PCRE2_SPTR ccode;
int i;

/* Negative XCLASS and NCLASS both have a bitmap indicating which characters
are accepted. For clarity we print this inverted and prefixed by "^". */
Expand Down

0 comments on commit 6c5b9f6

Please sign in to comment.