diff --git a/src/CompilerBase.php b/src/CompilerBase.php index d8dd53d6..7b1cba50 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -1312,12 +1312,23 @@ class CompilerBase implements PropertyAccessContext protected function getLiteralString(string $string): string { if ($this->noLiteralStrings) { - return '"' . $this->escapeString($string) . '"'; + return $this->getInlineString($string); } $index = $this->literalStrings[$string] ?? $this->addLiteralString($string); return self::LITERAL_STRINGS . '[' . $index . ']'; } + /** + * Generates a PHP string value without adding it to the literal-string table. + * + * A C++ string literal may contain an embedded NUL, but passing it as a + * const char* would truncate it at that byte. ZEND_STRL preserves its length. + */ + protected function getInlineString(string $string): string + { + return self::TYPE_STR . '{ZEND_STRL(' . $this->genCharPtr($string, true) . ')}'; + } + protected function parseScalar(Node\Scalar $expr): string { $type = $expr->getType(); @@ -1338,7 +1349,7 @@ class CompilerBase implements PropertyAccessContext } return $this->parseScalarFloat($expr); case 'Scalar_String': - return $expr->hasAttribute('noLiteralString') ? $this->genCharPtr($expr->value, true) : $this->getLiteralString($expr->value); + return $expr->hasAttribute('noLiteralString') ? $this->getInlineString($expr->value) : $this->getLiteralString($expr->value); default: abort($expr); break; diff --git a/tests/aot/basic/embedded-null-string.phpt b/tests/aot/basic/embedded-null-string.phpt new file mode 100644 index 00000000..6131c00f --- /dev/null +++ b/tests/aot/basic/embedded-null-string.phpt @@ -0,0 +1,22 @@ +--TEST-- +Embedded NUL bytes in string literals +--FILE-- + +--EXPECT-- +int(13) +string(26) "68656c6c6f200020776f726c64" +bool(true) +bool(true) +bool(true) diff --git a/tests/std/strings/trim.phpt b/tests/std/strings/trim.phpt index 91f8caa3..ade3a8ab 100644 --- a/tests/std/strings/trim.phpt +++ b/tests/std/strings/trim.phpt @@ -2,7 +2,7 @@ trim(), rtrim() and ltrim() functions --SKIPIF-- --FILE--