Skip to content

Commit 085abe9

Browse files
authored
Pass hash table directly in tokenizer (php#18650)
This avoids dereferences by Z_ARRVAL_P().
1 parent 7f59fcc commit 085abe9

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

ext/tokenizer/tokenizer.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ static zend_string *make_str(unsigned char *text, size_t leng, HashTable *intern
280280
}
281281

282282
static void add_token(
283-
zval *return_value, int token_type, unsigned char *text, size_t leng, int lineno,
283+
HashTable *return_value_ht, int token_type, unsigned char *text, size_t leng, int lineno,
284284
zend_class_entry *token_class, HashTable *interned_strings) {
285285
zval token;
286286
if (token_class) {
@@ -315,7 +315,7 @@ static void add_token(
315315
} else {
316316
ZVAL_STR(&token, make_str(text, leng, interned_strings));
317317
}
318-
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &token);
318+
zend_hash_next_index_insert_new(return_value_ht, &token);
319319
}
320320

321321
static bool tokenize(zval *return_value, zend_string *source, zend_class_entry *token_class)
@@ -337,11 +337,13 @@ static bool tokenize(zval *return_value, zend_string *source, zend_class_entry *
337337
zend_hash_init(&interned_strings, 0, NULL, NULL, 0);
338338
array_init(return_value);
339339

340+
HashTable *return_value_ht = Z_ARRVAL_P(return_value);
341+
340342
while ((token_type = lex_scan(&token, NULL))) {
341343
ZEND_ASSERT(token_type != T_ERROR);
342344

343345
add_token(
344-
return_value, token_type, zendtext, zendleng, token_line,
346+
return_value_ht, token_type, zendtext, zendleng, token_line,
345347
token_class, &interned_strings);
346348

347349
if (Z_TYPE(token) != IS_UNDEF) {
@@ -358,7 +360,7 @@ static bool tokenize(zval *return_value, zend_string *source, zend_class_entry *
358360
/* fetch the rest into a T_INLINE_HTML */
359361
if (zendcursor < zendlimit) {
360362
add_token(
361-
return_value, T_INLINE_HTML, zendcursor, zendlimit - zendcursor,
363+
return_value_ht, T_INLINE_HTML, zendcursor, zendlimit - zendcursor,
362364
token_line, token_class, &interned_strings);
363365
}
364366
break;
@@ -383,7 +385,7 @@ static bool tokenize(zval *return_value, zend_string *source, zend_class_entry *
383385
}
384386

385387
struct event_context {
386-
zval *tokens;
388+
HashTable *tokens;
387389
zend_class_entry *token_class;
388390
};
389391

@@ -428,7 +430,7 @@ static void on_event(
428430
ctx->tokens, token, (unsigned char *) text, length, line, ctx->token_class, NULL);
429431
break;
430432
case ON_FEEDBACK: {
431-
HashTable *tokens_ht = Z_ARRVAL_P(ctx->tokens);
433+
HashTable *tokens_ht = ctx->tokens;
432434
zval *token_zv, *id_zv = NULL;
433435
ZEND_HASH_REVERSE_FOREACH_VAL(tokens_ht, token_zv) {
434436
id_zv = extract_token_id_to_replace(token_zv, text, length);
@@ -469,7 +471,7 @@ static bool tokenize_parse(
469471
zend_prepare_string_for_scanning(&source_zval, ZSTR_EMPTY_ALLOC());
470472
array_init(&token_stream);
471473

472-
ctx.tokens = &token_stream;
474+
ctx.tokens = Z_ARRVAL(token_stream);
473475
ctx.token_class = token_class;
474476

475477
CG(ast) = NULL;

0 commit comments

Comments
 (0)