Skip to content

Commit 72feb69

Browse files
Merge branch '4.3' into 4.4
* 4.3: [Console] Constant STDOUT might be undefined. Allow returning null from NormalizerInterface::normalize [Security\Core] throw AccessDeniedException when switch user fails [Mime] fix guessing mime-types of files with leading dash [HttpFoundation] fix guessing mime-types of files with leading dash [VarExporter] fix exporting some strings [Cache] forbid serializing AbstractAdapter and TagAwareAdapter instances Use constant time comparison in UriSigner
2 parents 9fa6028 + 097aa4c commit 72feb69

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

Internal/Exporter.php

+19-18
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,28 @@ public static function export($value, $indent = '')
212212
$subIndent = $indent.' ';
213213

214214
if (\is_string($value)) {
215-
$code = var_export($value, true);
216-
217-
if (false !== strpos($value, "\n") || false !== strpos($value, "\r")) {
218-
$code = strtr($code, [
219-
"\r\n" => "'.\"\\r\\n\"\n".$subIndent.".'",
220-
"\r" => "'.\"\\r\"\n".$subIndent.".'",
221-
"\n" => "'.\"\\n\"\n".$subIndent.".'",
222-
]);
223-
}
215+
$code = sprintf("'%s'", addcslashes($value, "'\\"));
224216

225-
if (false !== strpos($value, "\0")) {
226-
$code = str_replace('\' . "\0" . \'', '\'."\0".\'', $code);
227-
$code = str_replace('".\'\'."', '', $code);
228-
}
217+
$code = preg_replace_callback('/([\0\r\n]++)(.)/', function ($m) use ($subIndent) {
218+
$m[1] = sprintf('\'."%s".\'', str_replace(
219+
["\0", "\r", "\n", '\n\\'],
220+
['\0', '\r', '\n', '\n"'."\n".$subIndent.'."\\'],
221+
$m[1]
222+
));
229223

230-
if (false !== strpos($code, "''.")) {
231-
$code = str_replace("''.", '', $code);
232-
}
224+
if ("'" === $m[2]) {
225+
return substr($m[1], 0, -2);
226+
}
227+
228+
if ('n".\'' === substr($m[1], -4)) {
229+
return substr_replace($m[1], "\n".$subIndent.".'".$m[2], -2);
230+
}
231+
232+
return $m[1].$m[2];
233+
}, $code, -1, $count);
233234

234-
if (".''" === substr($code, -3)) {
235-
$code = rtrim(substr($code, 0, -3));
235+
if ($count && 0 === strpos($code, "''.")) {
236+
$code = substr($code, 3);
236237
}
237238

238239
return $code;

Tests/Fixtures/lf-ending-string.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
return '\'BOOM\''."\n"
4+
.'.var_dump(123)//\'';

Tests/Fixtures/multiline-string.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
return [
44
"\0\0\r\n"
5-
.'A' => 'B'."\r"
6-
.'C'."\n"
5+
.'A' => 'B'."\r".'C'."\n"
76
."\n",
87
];

Tests/VarExporterTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function testExport(string $testName, $value, bool $staticValueExpected =
112112
public function provideExport()
113113
{
114114
yield ['multiline-string', ["\0\0\r\nA" => "B\rC\n\n"], true];
115+
yield ['lf-ending-string', "'BOOM'\n.var_dump(123)//'", true];
115116

116117
yield ['bool', true, true];
117118
yield ['simple-array', [123, ['abc']], true];

0 commit comments

Comments
 (0)