Skip to content

Commit a82be82

Browse files
committed
Add macro for Unicode Corregindum #9 strict
This macro follows Unicode Corrigendum #9 to allow non-character code points. These are still discouraged but not completely forbidden. It's best for code that isn't intended to operate on arbitrary other code text to use the original definition, but code that does things, such as source code control, should change to use this definition if it wants to be Unicode-strict. Perl can't adopt C9 wholesale, as it might create security holes in existing applications that rely on Perl keeping non-chars out.
1 parent e23e8bc commit a82be82

File tree

6 files changed

+164
-2
lines changed

6 files changed

+164
-2
lines changed

ext/XS-APItest/APItest.xs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5334,6 +5334,13 @@ test_isSTRICT_UTF8_CHAR(char *s, STRLEN len)
53345334
OUTPUT:
53355335
RETVAL
53365336

5337+
STRLEN
5338+
test_isC9_STRICT_UTF8_CHAR(char *s, STRLEN len)
5339+
CODE:
5340+
RETVAL = isC9_STRICT_UTF8_CHAR((U8 *) s, (U8 *) s + len);
5341+
OUTPUT:
5342+
RETVAL
5343+
53375344
IV
53385345
test_is_utf8_valid_partial_char_flags(char *s, STRLEN len, U32 flags)
53395346
CODE:

ext/XS-APItest/t/utf8.t

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,11 @@ for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
423423
}
424424

425425
my $valid_under_strict = 1;
426+
my $valid_under_c9strict = 1;
426427
if ($n > 0x10FFFF) {
427428
$this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
428429
$valid_under_strict = 0;
430+
$valid_under_c9strict = 0;
429431
}
430432
elsif (($n & 0xFFFE) == 0xFFFE) {
431433
$this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
@@ -491,6 +493,27 @@ for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
491493
diag "The warnings were: " . join(", ", @warnings);
492494
}
493495

496+
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
497+
$expected_len = ($valid_under_c9strict) ? $len : 0;
498+
is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes) returns expected length: $len");
499+
500+
unless (is(scalar @warnings, 0,
501+
"Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings"))
502+
{
503+
diag "The warnings were: " . join(", ", @warnings);
504+
}
505+
506+
undef @warnings;
507+
508+
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
509+
is($ret, 0, "Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
510+
511+
unless (is(scalar @warnings, 0,
512+
"Verify isC9_STRICT_UTF8_CHAR() generated no warnings"))
513+
{
514+
diag "The warnings were: " . join(", ", @warnings);
515+
}
516+
494517
undef @warnings;
495518

496519
$ret_ref = test_valid_utf8_to_uvchr($bytes);
@@ -769,6 +792,14 @@ foreach my $test (@malformations) {
769792
diag "The warnings were: " . join(", ", @warnings);
770793
}
771794

795+
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
796+
is($ret, 0, "$testname: isC9_STRICT_UTF8_CHAR returns 0");
797+
unless (is(scalar @warnings, 0,
798+
"$testname: isC9_STRICT_UTF8_CHAR() generated no warnings"))
799+
{
800+
diag "The warnings were: " . join(", ", @warnings);
801+
}
802+
772803
for my $j (1 .. $length - 1) {
773804
my $partial = substr($bytes, 0, $j);
774805

@@ -1294,6 +1325,25 @@ foreach my $test (@tests) {
12941325
diag "The warnings were: " . join(", ", @warnings);
12951326
}
12961327

1328+
undef @warnings;
1329+
$ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
1330+
if ($will_overflow) {
1331+
is($ret, 0, "isC9_STRICT_UTF8_CHAR() $testname: returns 0");
1332+
}
1333+
else {
1334+
my $expected_ret = ( $testname =~ /surrogate/
1335+
|| $allowed_uv > 0x10FFFF)
1336+
? 0
1337+
: $length;
1338+
is($ret, $expected_ret,
1339+
"isC9_STRICT_UTF8_CHAR() $testname: returns expected length: $expected_ret");
1340+
}
1341+
unless (is(scalar @warnings, 0,
1342+
"isC9_STRICT_UTF8_CHAR() $testname: generated no warnings"))
1343+
{
1344+
diag "The warnings were: " . join(", ", @warnings);
1345+
}
1346+
12971347
# Test partial character handling, for each byte not a full character
12981348
for my $j (1.. $length - 1) {
12991349

regcharclass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,6 @@
18761876
* 5c7eb94310e2aaa15702fd6bed24ff0e7ab5448f9a8231d8c49ca96c9e941089 lib/unicore/mktables
18771877
* cdecb300baad839a6f62791229f551a4fa33f3cbdca08e378dc976466354e778 lib/unicore/version
18781878
* 913d2f93f3cb6cdf1664db888bf840bc4eb074eef824e082fceda24a9445e60c regen/charset_translations.pl
1879-
* e3dc81163da3e92f7be01e9b953f6edb548eba93f1abb3d334e3b0469573c46d regen/regcharclass.pl
1879+
* 66e20f857451956f9fc7ad7432de972e84fb857885009838878bcf6f91ffbeef regen/regcharclass.pl
18801880
* 393f8d882713a3ba227351ad0f00ea4839fda74fcf77dcd1cdf31519925adba5 regen/regcharclass_multi_char_folds.pl
18811881
* ex: set ro: */

regen/regcharclass.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,16 @@ sub make_macro {
17041704
#0xF0000 - 0xFFFFD
17051705
#0x100000 - 0x10FFFD
17061706
1707+
#C9_STRICT_UTF8_CHAR: Matches legal Unicode UTF-8 variant code points, no surrogates
1708+
#=> UTF8 :no_length_checks only_ascii_platform
1709+
#0x0080 - 0xD7FF
1710+
#0xE000 - 0x10FFFF
1711+
#
1712+
#C9_STRICT_UTF8_CHAR: Matches legal Unicode UTF-8 variant code points including non-character code points, no surrogates
1713+
#=> UTF8 :no_length_checks only_ebcdic_platform
1714+
#0x00A0 - 0xD7FF
1715+
#0xE000 - 0x10FFFF
1716+
17071717
QUOTEMETA: Meta-characters that \Q should quote
17081718
=> high :fast
17091719
\p{_Perl_Quotemeta}

utf8.h

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,28 @@ C<cp> is Unicode if above 255; otherwise is platform-native.
383383
: 0 ) \
384384
: 0 )
385385

386+
/* Similarly,
387+
C9_STRICT_UTF8_CHAR: Matches legal Unicode UTF-8 variant code
388+
points, no surrogates
389+
0x0080 - 0xD7FF
390+
0xE000 - 0x10FFFF
391+
*/
392+
/*** GENERATED CODE ***/
393+
#define is_C9_STRICT_UTF8_CHAR_utf8_no_length_checks(s) \
394+
( ( 0xC2 <= ((U8*)s)[0] && ((U8*)s)[0] <= 0xDF ) ? \
395+
( LIKELY( ( ((U8*)s)[1] & 0xC0 ) == 0x80 ) ? 2 : 0 ) \
396+
: ( 0xE0 == ((U8*)s)[0] ) ? \
397+
( LIKELY( ( ( ((U8*)s)[1] & 0xE0 ) == 0xA0 ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) ? 3 : 0 )\
398+
: ( ( 0xE1 <= ((U8*)s)[0] && ((U8*)s)[0] <= 0xEC ) || ( ((U8*)s)[0] & 0xFE ) == 0xEE ) ?\
399+
( LIKELY( ( ( ((U8*)s)[1] & 0xC0 ) == 0x80 ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) ? 3 : 0 )\
400+
: ( 0xED == ((U8*)s)[0] ) ? \
401+
( LIKELY( ( ( ((U8*)s)[1] & 0xE0 ) == 0x80 ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) ? 3 : 0 )\
402+
: ( 0xF0 == ((U8*)s)[0] ) ? \
403+
( LIKELY( ( ( 0x90 <= ((U8*)s)[1] && ((U8*)s)[1] <= 0xBF ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) && ( ( ((U8*)s)[3] & 0xC0 ) == 0x80 ) ) ? 4 : 0 )\
404+
: ( 0xF1 <= ((U8*)s)[0] && ((U8*)s)[0] <= 0xF3 ) ? \
405+
( LIKELY( ( ( ( ((U8*)s)[1] & 0xC0 ) == 0x80 ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) && ( ( ((U8*)s)[3] & 0xC0 ) == 0x80 ) ) ? 4 : 0 )\
406+
: LIKELY( ( ( ( 0xF4 == ((U8*)s)[0] ) && ( ( ((U8*)s)[1] & 0xF0 ) == 0x80 ) ) && ( ( ((U8*)s)[2] & 0xC0 ) == 0x80 ) ) && ( ( ((U8*)s)[3] & 0xC0 ) == 0x80 ) ) ? 4 : 0 )
407+
386408
#endif /* EBCDIC vs ASCII */
387409

388410
/* 2**UTF_ACCUMULATION_SHIFT - 1 */
@@ -989,7 +1011,8 @@ be a surrogate nor a non-character code point. Thus this excludes any code
9891011
point from Perl's extended UTF-8.
9901012
9911013
This is used to efficiently decide if the next few bytes in C<s> is
992-
legal Unicode-acceptable UTF-8 for a single character.
1014+
legal Unicode-acceptable UTF-8 for a single character. Use
1015+
C<L</isC9_STRICT_UTF8_CHAR>> to also accept non-character code points.
9931016
9941017
=cut
9951018
*/
@@ -1003,6 +1026,36 @@ legal Unicode-acceptable UTF-8 for a single character.
10031026
? 0 \
10041027
: is_STRICT_UTF8_CHAR_utf8_no_length_checks(s))
10051028

1029+
/*
1030+
1031+
=for apidoc Am|STRLEN|isC9_STRICT_UTF8_CHAR|const U8 *s|const U8 *e
1032+
1033+
Evaluates to non-zero if the first few bytes of the string starting at C<s> and
1034+
looking no further than S<C<e - 1>> are well-formed UTF-8 that represents some
1035+
Unicode non-surrogate code point; otherwise it evaluates to 0. If non-zero,
1036+
the value gives how many bytes starting at C<s> comprise the code point's
1037+
representation.
1038+
1039+
The largest acceptable code point is the Unicode maximum 0x10FFFF. This
1040+
differs from C<L</isSTRICT_UTF8_CHAR>> only in that it accepts non-character
1041+
code points. This corresponds to
1042+
L<Unicode Corrigendum #9|http://www.unicode.org/versions/corrigendum9.html>.
1043+
which said that non-character code points are merely discouraged rather than
1044+
completely forbidden in open interchange. See
1045+
L<perlunicode/Noncharacter code points>.
1046+
1047+
=cut
1048+
*/
1049+
1050+
#define isC9_STRICT_UTF8_CHAR(s, e) \
1051+
(UNLIKELY((e) <= (s)) \
1052+
? 0 \
1053+
: (UTF8_IS_INVARIANT(*s)) \
1054+
? 1 \
1055+
: UNLIKELY(((e) - (s)) < UTF8SKIP(s)) \
1056+
? 0 \
1057+
: is_C9_STRICT_UTF8_CHAR_utf8_no_length_checks(s))
1058+
10061059
/* Do not use; should be deprecated. Use isUTF8_CHAR() instead; this is
10071060
* retained solely for backwards compatibility */
10081061
#define IS_UTF8_CHAR(p, n) (isUTF8_CHAR(p, (p) + (n)) == n)

0 commit comments

Comments
 (0)