Skip to content

Commit

Permalink
maketables: avoid misleading values in case flipping table (#313)
Browse files Browse the repository at this point in the history
The tables generated by pcre2_maketables() include one that maps
all lowercase characters on the first 255 code points to their
corresponding upper case code point, but fails to notice that
toupper() could return a larger code point and therefore result
in the store of a truncated and unrelated code instead.

Restrict all values to what is valid for uint8_t and document
in the test case the failure for character 'μ'[1] (U+00B5) and
that was incorrectly getting back 924 (U+039C) from macOS fr_FR,
and resulting in an incorrect case equivalent with the truncated
value of 159.

[1] https://en.wikipedia.org/wiki/Mu_(letter)
  • Loading branch information
carenas authored Oct 22, 2023
1 parent d11400f commit 8d3e96c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/pcre2_maketables.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ PCRE2_DFTABLES is defined. */
# include "pcre2_internal.h"
#endif



/*************************************************
* Create PCRE2 character tables *
*************************************************/
Expand Down Expand Up @@ -98,7 +96,11 @@ for (i = 0; i < 256; i++) *p++ = tolower(i);

/* Next the case-flipping table */

for (i = 0; i < 256; i++) *p++ = islower(i)? toupper(i) : tolower(i);
for (i = 0; i < 256; i++)
{
int c = islower(i)? toupper(i) : tolower(i);
*p++ = (c < 256)? c : i;
}

/* Then the character class tables. Don't try to be clever and save effort on
exclusive ones - in some locales things may be different.
Expand Down
5 changes: 5 additions & 0 deletions testdata/testinput3
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
\= Expect no match
�cole

/\xb5/i
\= Expect no match
\x9c

/\W+/
>>>\xaa<<<
>>>\xba<<<
Expand Down
7 changes: 7 additions & 0 deletions testdata/testoutput3
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ Subject length lower bound = 1
�cole
No match

/\xb5/i
0: �
\= Expect no match
\x9c
No match

/\W+/
>>>\xaa<<<
0: >>>
Expand Down
7 changes: 7 additions & 0 deletions testdata/testoutput3A
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ Subject length lower bound = 1
�cole
No match

/\xb5/i
0: �
\= Expect no match
\x9c
No match

/\W+/
>>>\xaa<<<
0: >>>
Expand Down
7 changes: 7 additions & 0 deletions testdata/testoutput3B
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ Subject length lower bound = 1
�cole
No match

/\xb5/i
0: �
\= Expect no match
\x9c
No match

/\W+/
>>>\xaa<<<
0: >>>
Expand Down

0 comments on commit 8d3e96c

Please sign in to comment.