Skip to content

Commit 85bb8b9

Browse files
committed
toke.c: Convert some strchr to memchr
This allows things to work properly in the face of embedded NULs. See the branch merge message for more information.
1 parent d77eff5 commit 85bb8b9

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

toke.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ S_missingterm(pTHX_ char *s, const STRLEN len)
563563
bool uni = FALSE;
564564
SV *sv;
565565
if (s) {
566-
char * const nl = strrchr(s,'\n');
566+
char * const nl = (char *) memrchr(s, '\n', len);
567567
if (nl)
568568
*nl = '\0';
569569
uni = UTF;
@@ -585,7 +585,7 @@ S_missingterm(pTHX_ char *s, const STRLEN len)
585585
}
586586
s = tmpbuf;
587587
}
588-
q = strchr(s,'"') ? '\'' : '"';
588+
q = memrchr(s, '"', len) ? '\'' : '"';
589589
sv = sv_2mortal(newSVpv(s,0));
590590
if (uni)
591591
SvUTF8_on(sv);
@@ -1767,7 +1767,7 @@ S_incline(pTHX_ const char *s, const char *end)
17671767
return;
17681768
while (SPACE_OR_TAB(*s))
17691769
s++;
1770-
if (*s == '"' && (t = strchr(s+1, '"'))) {
1770+
if (*s == '"' && (t = (char *) memchr(s+1, '"', end - s))) {
17711771
s++;
17721772
e = t + 1;
17731773
}
@@ -1921,7 +1921,6 @@ STATIC void
19211921
S_check_uni(pTHX)
19221922
{
19231923
const char *s;
1924-
const char *t;
19251924

19261925
if (PL_oldoldbufptr != PL_last_uni)
19271926
return;
@@ -1930,7 +1929,7 @@ S_check_uni(pTHX)
19301929
s = PL_last_uni;
19311930
while (isWORDCHAR_lazy_if_safe(s, PL_bufend, UTF) || *s == '-')
19321931
s += UTF ? UTF8SKIP(s) : 1;
1933-
if ((t = strchr(s, '(')) && t < PL_bufptr)
1932+
if (memchr(s, '(', PL_bufptr - s))
19341933
return;
19351934

19361935
Perl_ck_warner_d(aTHX_ packWARN(WARN_AMBIGUOUS),
@@ -3665,7 +3664,7 @@ S_scan_const(pTHX_ char *start)
36653664
s++;
36663665

36673666
/* If there is no matching '}', it is an error. */
3668-
if (! (e = strchr(s, '}'))) {
3667+
if (! (e = (char *) memchr(s, '}', send - s))) {
36693668
if (! PL_lex_inpat) {
36703669
yyerror("Missing right brace on \\N{}");
36713670
} else {
@@ -4179,7 +4178,7 @@ S_intuit_more(pTHX_ char *s, char *e)
41794178
/* this is terrifying, and it works */
41804179
int weight;
41814180
char seen[256];
4182-
const char * const send = strchr(s,']');
4181+
const char * const send = (char *) memchr(s, ']', e - s);
41834182
unsigned char un_char, last_un_char;
41844183
char tmpbuf[sizeof PL_tokenbuf * 4];
41854184

@@ -5307,7 +5306,11 @@ Perl_yylex(pTHX)
53075306
|| *PL_splitstr == '\''
53085307
|| *PL_splitstr == '"')
53095308
&& strchr(PL_splitstr + 1, *PL_splitstr))
5309+
{
5310+
/* strchr is ok, because -F pattern can't contain
5311+
* embeddded NULs */
53105312
Perl_sv_catpvf(aTHX_ PL_linestr, "our @F=split(%s);", PL_splitstr);
5313+
}
53115314
else {
53125315
/* "q\0${splitstr}\0" is legal perl. Yes, even NUL
53135316
bytes can be used as quoting characters. :-) */
@@ -6443,8 +6446,9 @@ Perl_yylex(pTHX)
64436446
while (s < d) {
64446447
if (*s++ == '\n') {
64456448
incline(s, PL_bufend);
6446-
if (strBEGINs(s,"=cut")) {
6447-
s = strchr(s,'\n');
6449+
if (memBEGINs(s, (STRLEN) (PL_bufend - s), "=cut"))
6450+
{
6451+
s = (char *) memchr(s,'\n', d - s);
64486452
if (s)
64496453
s++;
64506454
else
@@ -6521,7 +6525,7 @@ Perl_yylex(pTHX)
65216525
OPERATOR('!');
65226526
case '<':
65236527
if (PL_expect != XOPERATOR) {
6524-
if (s[1] != '<' && !strchr(s,'>'))
6528+
if (s[1] != '<' && !memchr(s,'>', PL_bufend - s))
65256529
check_uni();
65266530
if (s[1] == '<' && s[2] != '>') {
65276531
if ( (s == PL_linestart || s[-1] == '\n')
@@ -6699,8 +6703,10 @@ Perl_yylex(pTHX)
66996703
else if (*s == '{') {
67006704
char *t;
67016705
PL_tokenbuf[0] = '%';
6702-
if (strEQ(PL_tokenbuf+1, "SIG") && ckWARN(WARN_SYNTAX)
6703-
&& (t = strchr(s, '}')) && (t = strchr(t, '=')))
6706+
if ( strEQ(PL_tokenbuf+1, "SIG")
6707+
&& ckWARN(WARN_SYNTAX)
6708+
&& (t = (char *) memchr(s, '}', PL_bufend - s))
6709+
&& (t = (char *) memchr(t, '=', PL_bufend - t)))
67046710
{
67056711
char tmpbuf[sizeof PL_tokenbuf];
67066712
do {
@@ -9957,7 +9963,7 @@ S_scan_heredoc(pTHX_ char *s)
99579963
len = d - PL_tokenbuf;
99589964

99599965
#ifndef PERL_STRICT_CR
9960-
d = strchr(s, '\r');
9966+
d = (char *) memchr(s, '\r', PL_bufend - s);
99619967
if (d) {
99629968
char * const olds = s;
99639969
s = d;
@@ -10326,7 +10332,7 @@ S_scan_inputsymbol(pTHX_ char *start)
1032610332

1032710333
PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
1032810334

10329-
end = strchr(s, '\n');
10335+
end = (char *) memchr(s, '\n', PL_bufend - s);
1033010336
if (!end)
1033110337
end = PL_bufend;
1033210338
if (s[1] == '<' && s[2] == '>' && s[3] == '>') {

0 commit comments

Comments
 (0)