Merge pull request #3751 from Icinga/fix/php-does-not-strip-trailing-whitespace-3733

Fix php ini parser does not strip trailing whitespace

(cherry picked from commit f027bc50e8)
Signed-off-by: Johannes Meyer <johannes.meyer@icinga.com>
This commit is contained in:
Johannes Meyer 2019-04-17 10:23:10 +02:00
parent e18c28ab25
commit 892bd13d90
2 changed files with 67 additions and 1 deletions

View File

@ -304,6 +304,7 @@ class IniParser
$str = str_replace('\"', '"', $str);
$str = str_replace('\\\\', '\\', $str);
return $str;
// This replacement is a work-around for PHP bug #76965. Fixed with versions 7.1.24, 7.2.12 and 7.3.0.
return preg_replace('~^([\'"])(.*?)\1\s+$~', '$2', $str);
}
}

View File

@ -170,4 +170,69 @@ EOD;
'IniParser::parseIniFile does resolve environment variables'
);
}
public function testPhpBug76965()
{
$config = <<<'EOD'
[section]
a = "foobar"
b = "foo"bar ""
c = "foobar" ; comment
d =' foo '
e =foo
f = foo
g = ""foo" "; Edge case, see below for more details
EOD;
$parsedConfig = parse_ini_string($config, true, INI_SCANNER_RAW)['section'];
if ($parsedConfig['a'] === 'foobar') {
$this->markTestSkipped('This version of PHP is not affected by bug #76965');
}
$this->assertEquals('"foobar" ', $parsedConfig['a'], 'PHP version affected but bug #76965 not in effect?');
$this->assertEquals('"foo"bar "" ', $parsedConfig['b'], 'PHP version affected but bug #76965 not in effect?');
$this->assertEquals('"foobar" ', $parsedConfig['c'], 'PHP version affected but bug #76965 not in effect?');
$this->assertEquals("' foo ' ", $parsedConfig['d'], 'PHP version affected but bug #76965 not in effect?');
file_put_contents($this->tempFile, $config);
$configObject = IniParser::parseIniFile($this->tempFile);
$this->assertEquals(
'foobar',
$configObject->get('section', 'a'),
'Workaround for PHP bug #76965 missing'
);
$this->assertEquals(
'foo"bar "',
$configObject->get('section', 'b'),
'Workaround for PHP bug #76965 missing'
);
$this->assertEquals(
'foobar',
$configObject->get('section', 'c'),
'Workaround for PHP bug #76965 missing'
);
$this->assertEquals(
' foo ',
$configObject->get('section', 'd'),
'Workaround for PHP bug #76965 missing'
);
$this->assertEquals(
'foo',
$configObject->get('section', 'e'),
'Workaround for PHP bug #76965 missing'
);
$this->assertEquals(
'foo',
$configObject->get('section', 'f'),
'Workaround for PHP bug #76965 missing'
);
// This is an edge case which will fail with the current work-around implementation.
// Though, it's considered a really rare case and as such deliberately ignored.
/*$this->assertEquals(
'"foo" ',
$configObject->get('section', 'g'),
'Workaround for PHP bug #76965 too greedy'
);*/
}
}