Skip to content

Stdlib: htmlspecialchars()/htmlentities() — scalar-to-string coercion for $string (ext/standard/html.c parity) #4310

Description

@PurHur

Category

stdlib

Problem

htmlspecialchars() and htmlentities() require TYPE_STRING for argument #1 ($string). Zend coerces int/float/bool (and objects with __toString) via zend_parse_parameter / convert_to_string before escaping.

This compiler throws LogicException: ... only supports strings in this compiler build (ext/standard/htmlspecialchars.php, ext/standard/htmlentities.php).

Related: #4296 (closed) fixed null $encoding; this issue is argument #1 coercion only.

php-src reference

Repro (failure today)

<?php
echo htmlspecialchars(42), "\n";
echo htmlentities(true), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php -r "echo htmlspecialchars(42), \"\\n\";"
php bin/vm.php -r "echo htmlspecialchars(42), \"\\n\";"
'
Runtime Output
Zend PHP 42 then 1
bin/vm.php LogicException: htmlspecialchars() only supports strings...

double_encode bool coercion is separate (existing PHPT covers bool); align flags/encoding with #4130 when touching VmString::htmlspecialchars.

Scope

Module Path
VM ext/standard/htmlspecialchars.php, htmlentities.php — use shared scalar→string helper (match nl2br() / preg_quote() parity issues)
Core ext/standard/VmString.phphtmlspecialchars() / htmlentities() entry
JIT JitStringArg / JitHtmlspecialchars — coerce non-string args like other string builtins
Tests test/compliance/cases/stdlib/htmlspecialchars_scalar_coerce.phpt

Done when

  • htmlspecialchars(42) and htmlentities(true) match Zend on VM
  • Invalid types (array/resource) raise TypeError like Zend, not LogicException
  • JIT path coerces scalars when not compile-time literal
  • ./script/ci-fast.sh --filter htmlspecialchars_scalar green

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions