Skip to content

Commit 1b20140

Browse files
committed
toke.c: Convert sizeof() calls to use more mnemonic
The length of a constant literal is STRLENs The end position of a C array is C_ARRAY_END The number of elements in a C array is C_ARRAY_LENGTH
1 parent c3ebe61 commit 1b20140

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

toke.c

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ S_incline(pTHX_ const char *s, const char *end)
19081908
while (SPACE_OR_TAB(*s))
19091909
s++;
19101910
if (memBEGINs(s, (STRLEN) (end - s), "line"))
1911-
s += sizeof("line") - 1;
1911+
s += STRLENs("line");
19121912
else
19131913
return;
19141914
if (SPACE_OR_TAB(*s))
@@ -1961,7 +1961,7 @@ S_incline(pTHX_ const char *s, const char *end)
19611961
char *tmpbuf2;
19621962
GV *gv2;
19631963

1964-
if (tmplen2 + 2 <= sizeof smallbuf)
1964+
if (tmplen2 + 2 <= C_ARRAY_LENGTH(smallbuf))
19651965
tmpbuf2 = smallbuf;
19661966
else
19671967
Newx(tmpbuf2, tmplen2 + 2, char);
@@ -2284,8 +2284,8 @@ S_force_word(pTHX_ char *start, int token, int check_keyword, int allow_pack)
22842284
char *s2 = PL_tokenbuf;
22852285
STRLEN len2 = len;
22862286
if (allow_pack && memBEGINPs(s2, len, "CORE::")) {
2287-
s2 += sizeof("CORE::") - 1;
2288-
len2 -= sizeof("CORE::") - 1;
2287+
s2 += STRLENs("CORE::");
2288+
len2 -= STRLENs("CORE::");
22892289
}
22902290
if (keyword(s2, len2, 0))
22912291
return start;
@@ -4134,15 +4134,15 @@ S_scan_const(pTHX_ char *start)
41344134
char hex_string[4];
41354135
int len =
41364136
my_snprintf(hex_string,
4137-
sizeof(hex_string),
4137+
C_ARRAY_LENGTH(hex_string),
41384138
"%02X.",
41394139

41404140
/* The regex compiler is
41414141
* expecting Unicode, not
41424142
* native */
41434143
NATIVE_TO_LATIN1(*str));
41444144
PERL_MY_SNPRINTF_POST_GUARD(len,
4145-
sizeof(hex_string));
4145+
C_ARRAY_LENGTH(hex_string));
41464146
Copy(hex_string, d, 3, char);
41474147
d += 3;
41484148
str++;
@@ -4169,7 +4169,8 @@ S_scan_const(pTHX_ char *start)
41694169
/* Convert first code point to Unicode hex,
41704170
* including the boiler plate before it. */
41714171
output_length =
4172-
my_snprintf(hex_string, sizeof(hex_string),
4172+
my_snprintf(hex_string,
4173+
C_ARRAY_LENGTH(hex_string),
41734174
"\\N{U+%X",
41744175
(unsigned int) NATIVE_TO_UNI(uv));
41754176

@@ -4192,7 +4193,7 @@ S_scan_const(pTHX_ char *start)
41924193
&char_length);
41934194
output_length =
41944195
my_snprintf(hex_string,
4195-
sizeof(hex_string),
4196+
C_ARRAY_LENGTH(hex_string),
41964197
".%X",
41974198
(unsigned int) NATIVE_TO_UNI(uv));
41984199

@@ -4616,7 +4617,7 @@ S_intuit_more(pTHX_ char *s, char *e)
46164617
* strongly suspect this isn't a character class */
46174618
if (isWORDCHAR_lazy_if_safe(s+1, PL_bufend, UTF)) {
46184619
int len;
4619-
char tmpbuf[sizeof PL_tokenbuf * 4];
4620+
char tmpbuf[ C_ARRAY_LENGTH(PL_tokenbuf) * 4 ];
46204621
scan_ident(s, tmpbuf, C_ARRAY_END(tmpbuf), FALSE);
46214622
len = (int)strlen(tmpbuf);
46224623
if ( len > 1
@@ -4782,7 +4783,7 @@ S_intuit_method(pTHX_ char *start, SV *ioname, CV *cv)
47824783
return 0;
47834784

47844785
char *s = start + (*start == '$');
4785-
char tmpbuf[sizeof PL_tokenbuf];
4786+
char tmpbuf[C_ARRAY_LENGTH(PL_tokenbuf)];
47864787
STRLEN len;
47874788
GV* indirgv;
47884789
/* Mustn't actually add anything to a symbol table.
@@ -5261,7 +5262,7 @@ yyl_sigvar(pTHX_ char *s)
52615262
char *dest = PL_tokenbuf + 1;
52625263
/* read var name, including sigil, into PL_tokenbuf */
52635264
PL_tokenbuf[0] = sigil;
5264-
parse_ident(&s, &dest, dest + sizeof(PL_tokenbuf) - 1,
5265+
parse_ident(&s, &dest, C_ARRAY_END(PL_tokenbuf),
52655266
0, cBOOL(UTF), FALSE);
52665267
*dest = '\0';
52675268
assert(PL_tokenbuf[1]); /* we have a variable name */
@@ -5449,7 +5450,7 @@ yyl_dollar(pTHX_ char *s)
54495450
&& (t = (char *) memchr(s, '}', PL_bufend - s))
54505451
&& (t = (char *) memchr(t, '=', PL_bufend - t)))
54515452
{
5452-
char tmpbuf[sizeof PL_tokenbuf];
5453+
char tmpbuf[C_ARRAY_LENGTH(PL_tokenbuf)];
54535454
do {
54545455
t++;
54555456
} while (isSPACE(*t));
@@ -5485,7 +5486,7 @@ yyl_dollar(pTHX_ char *s)
54855486
PL_expect = XTERM;/* e.g. print $fh &sub */
54865487
}
54875488
else if (isIDFIRST_lazy_if_safe(s, PL_bufend, UTF)) {
5488-
char tmpbuf[sizeof PL_tokenbuf];
5489+
char tmpbuf[C_ARRAY_LENGTH(PL_tokenbuf)];
54895490
int t2;
54905491
STRLEN len;
54915492
scan_word(s, tmpbuf, C_ARRAY_END(tmpbuf), TRUE, &len);
@@ -7086,7 +7087,7 @@ yyl_require(pTHX_ char *s, I32 orig_keyword)
70867087
*PL_tokenbuf = '\0';
70877088
s = force_word(s,BAREWORD,TRUE,TRUE);
70887089
if (isIDFIRST_lazy_if_safe(PL_tokenbuf,
7089-
PL_tokenbuf + sizeof(PL_tokenbuf),
7090+
C_ARRAY_END(PL_tokenbuf),
70907091
UTF))
70917092
{
70927093
gv_stashpvn(PL_tokenbuf, strlen(PL_tokenbuf),
@@ -7268,8 +7269,9 @@ yyl_my(pTHX_ char *s, I32 my)
72687269
char tmpbuf[1024];
72697270
int i;
72707271
PL_bufptr = s;
7271-
i = my_snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", PL_tokenbuf);
7272-
PERL_MY_SNPRINTF_POST_GUARD(i, sizeof(tmpbuf));
7272+
i = my_snprintf(tmpbuf, C_ARRAY_LENGTH(tmpbuf),
7273+
"No such class %.1000s", PL_tokenbuf);
7274+
PERL_MY_SNPRINTF_POST_GUARD(i, C_ARRAY_LENGTH(tmpbuf));
72737275
yyerror_pv(tmpbuf, UTF ? SVf_UTF8 : 0);
72747276
}
72757277
}
@@ -7404,8 +7406,8 @@ yyl_fake_eof(pTHX_ U32 fake_eof, bool bof, char *s)
74047406
#ifdef ALTERNATE_SHEBANG
74057407
else {
74067408
static char const as[] = ALTERNATE_SHEBANG;
7407-
if (*s == as[0] && strnEQ(s, as, sizeof(as) - 1))
7408-
d = s + (sizeof(as) - 1);
7409+
if (*s == as[0] && strnEQ(s, as, C_ARRAY_LENGTH(as) - 1))
7410+
d = s + (C_ARRAY_LENGTH(as) - 1);
74097411
}
74107412
#endif /* ALTERNATE_SHEBANG */
74117413
}
@@ -9082,7 +9084,7 @@ yyl_keylookup(pTHX_ char *s, GV *gv)
90829084

90839085
/* Check for lexical sub */
90849086
if (PL_expect != XOPERATOR) {
9085-
char tmpbuf[sizeof PL_tokenbuf + 1];
9087+
char tmpbuf[C_ARRAY_LENGTH(PL_tokenbuf) + 1];
90869088
*tmpbuf = '&';
90879089
Copy(PL_tokenbuf, tmpbuf+1, len, char);
90889090
c.off = pad_findmy_pvn(tmpbuf, len+1, 0);
@@ -9155,7 +9157,7 @@ yyl_try(pTHX_ char *s)
91559157
STRLEN len;
91569158

91579159
/* Copy the longest sequence of isPLUGINFIX() chars into PL_tokenbuf */
9158-
while(s_end < PL_bufend && d < PL_tokenbuf+sizeof(PL_tokenbuf)-1 && isPLUGINFIX(*s_end))
9160+
while(s_end < PL_bufend && d < C_ARRAY_END(PL_tokenbuf)-1 && isPLUGINFIX(*s_end))
91599161
*d++ = *s_end++;
91609162
*d = '\0';
91619163

@@ -10993,7 +10995,7 @@ S_scan_heredoc(pTHX_ char *s)
1099310995

1099410996
s += 2;
1099510997
d = PL_tokenbuf + 1;
10996-
e = PL_tokenbuf + sizeof PL_tokenbuf - 1;
10998+
e = C_ARRAY_END(PL_tokenbuf);
1099710999
*PL_tokenbuf = '\n';
1099811000
peek = s;
1099911001

@@ -11037,7 +11039,7 @@ S_scan_heredoc(pTHX_ char *s)
1103711039
d += len;
1103811040
}
1103911041

11040-
if (d >= PL_tokenbuf + sizeof PL_tokenbuf - 1)
11042+
if (d >= C_ARRAY_END(PL_tokenbuf) - 1)
1104111043
croak("Delimiter for here document is too long");
1104211044

1104311045
*d++ = '\n';
@@ -11404,7 +11406,7 @@ S_scan_heredoc(pTHX_ char *s)
1140411406
Safefree(indent);
1140511407
SvREFCNT_dec(tmpstr);
1140611408
CopLINE_set(PL_curcop, origline);
11407-
missingterm(PL_tokenbuf + 1, sizeof(PL_tokenbuf) - 1);
11409+
missingterm(PL_tokenbuf + 1, C_ARRAY_LENGTH(PL_tokenbuf) - 1);
1140811410
}
1140911411

1141011412

@@ -11434,7 +11436,7 @@ S_scan_inputsymbol(pTHX_ char *start)
1143411436
I32 len;
1143511437
bool nomagicopen = FALSE;
1143611438
char *d = PL_tokenbuf;/* start of temp holding space */
11437-
const char * const e = PL_tokenbuf + sizeof PL_tokenbuf;/* end of temp holding space */
11439+
const char * const e = C_ARRAY_END(PL_tokenbuf);/* end of temp holding space */
1143811440

1143911441
PERL_ARGS_ASSERT_SCAN_INPUTSYMBOL;
1144011442

@@ -11454,7 +11456,7 @@ S_scan_inputsymbol(pTHX_ char *start)
1145411456
or if it didn't end, or if we see a newline
1145511457
*/
1145611458

11457-
if (len >= (I32)sizeof PL_tokenbuf)
11459+
if (len >= (I32) C_ARRAY_LENGTH(PL_tokenbuf))
1145811460
croak("Excessively long <> operator");
1145911461
if (s >= end)
1146011462
croak("Unterminated <> operator");
@@ -12394,7 +12396,7 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
1239412396
case '6': case '7': case '8': case '9': case '.':
1239512397
decimal:
1239612398
d = PL_tokenbuf;
12397-
e = PL_tokenbuf + sizeof PL_tokenbuf - 6; /* room for various punctuation */
12399+
e = C_ARRAY_END(PL_tokenbuf) - 6; /* room for various punctuation */
1239812400
floatit = FALSE;
1239912401
if (hexfp) {
1240012402
floatit = TRUE;
@@ -13095,7 +13097,7 @@ S_swallow_bom(pTHX_ U8 *s)
1309513097
#ifdef DEBUGGING
1309613098
if (DEBUG_p_TEST || DEBUG_T_TEST) PerlIO_printf(Perl_debug_log, "UTF-8 script encoding (BOM)\n");
1309713099
#endif
13098-
s += sizeof(BOM_UTF8) - 1; /* UTF-8 */
13100+
s += C_ARRAY_LENGTH(BOM_UTF8) - 1; /* UTF-8 */
1309913101
}
1310013102
break;
1310113103
}

0 commit comments

Comments
 (0)