Skip to content

Commit

Permalink
Fixed potential scope issues for $php_errormsg
Browse files Browse the repository at this point in the history
Simply replacing any usage of $php_errormsg (as this has been removed in
PHP8.0) with error_get_last() could potentially lead to fetching an
error that has nothing to do with the code we actually want to inspect.
Thus error_clear_last() is called before executing the lines prone to
cause an error. As error_clear_last() is PHP7+ Symfony's polyfill was
added as a dependency for backward compatbility.
The return value of error_get_last() is checked in accordance with the
recommendation of the polyfill.
  • Loading branch information
Alexander Wozniak committed Feb 4, 2021
1 parent 83d3004 commit 359b9b8
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"ext-simplexml": "*",
"ext-spl": "*",
"ext-xml": "*",
"ext-zlib": "*"
"ext-zlib": "*",
"symfony/polyfill-php70": "^1.19"
},
"require-dev": {
"zf1s/phpunit": "3.7.39",
Expand Down
3 changes: 2 additions & 1 deletion packages/zend-feed/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"zf1s/zend-loader": "^1.13.4",
"zf1s/zend-uri": "^1.13.4",
"zf1s/zend-version": "^1.13.4",
"zf1s/zend-xml": "^1.13.4"
"zf1s/zend-xml": "^1.13.4",
"symfony/polyfill-php70": "^1.19"
},
"autoload": {
"psr-0": {
Expand Down
6 changes: 4 additions & 2 deletions packages/zend-feed/library/Zend/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,15 @@ public static function importString($string)
*/
public static function importFile($filename)
{
error_clear_last();
$feed = @file_get_contents($filename);
if ($feed === false) {
/**
* @see Zend_Feed_Exception
*/
// require_once 'Zend/Feed/Exception.php';
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
throw new Zend_Feed_Exception("File could not be loaded: $phpErrormsg");
}
return self::importString($feed);
Expand Down Expand Up @@ -297,6 +298,7 @@ public static function findFeeds($uri)
$contents = $response->getBody();

// Parse the contents for appropriate <link ... /> tags
error_clear_last();
$pattern = '~(<link[^>]+)/?>~i';
$result = @preg_match_all($pattern, $contents, $matches);
if ($result === false) {
Expand All @@ -305,7 +307,7 @@ public static function findFeeds($uri)
*/
// require_once 'Zend/Feed/Exception.php';
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
throw new Zend_Feed_Exception("Internal error: $phpErrormsg");
}

Expand Down
3 changes: 2 additions & 1 deletion packages/zend-feed/library/Zend/Feed/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ public function __construct($uri = null, $string = null, Zend_Feed_Builder_Inter
*/
public function __wakeup()
{
error_clear_last();
$doc = new DOMDocument;
$doc = @Zend_Xml_Security::scan($this->_element, $doc);

if (!$doc) {
$err = error_get_last();
$phpErrormsg = isset($err) ? $err['message'] : null;
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// prevent the class to generate an undefined variable notice (ZF-2590)
if (!isset($phpErrormsg)) {
if (function_exists('xdebug_is_enabled')) {
Expand Down
1 change: 1 addition & 0 deletions packages/zend-feed/library/Zend/Feed/Entry/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function __construct($uri = null, $element = null)
if (!($element instanceof DOMElement)) {
if ($element) {
// Load the feed as an XML DOMDocument object
error_clear_last();
$doc = new DOMDocument();
$doc = @Zend_Xml_Security::scan($element, $doc);

Expand Down
3 changes: 2 additions & 1 deletion packages/zend-feed/library/Zend/Feed/Entry/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ public function save($postUri = null)
}

// Update internal properties using $client->responseBody;
error_clear_last();
$newEntry = new DOMDocument;
$newEntry = @Zend_Xml_Security::scan($response->getBody(), $newEntry);

if (!$newEntry) {
$err = error_get_last();
$phpErrormsg = isset($err) ? $err['message'] : null;
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// prevent the class to generate an undefined variable notice (ZF-2590)
if (!isset($phpErrormsg)) {
if (function_exists('xdebug_is_enabled')) {
Expand Down
4 changes: 3 additions & 1 deletion packages/zend-feed/library/Zend/Feed/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public static function importString($string)
*/
public static function importFile($filename)
{
error_clear_last();
$feed = @file_get_contents($filename);
if ($feed === false) {
/**
Expand Down Expand Up @@ -454,6 +455,7 @@ public static function detectType($feed, $specOnly = false)
} elseif($feed instanceof DOMDocument) {
$dom = $feed;
} elseif(is_string($feed) && !empty($feed)) {
error_clear_last();
//$oldValue = libxml_disable_entity_loader(true);
$dom = new DOMDocument;
try {
Expand All @@ -467,7 +469,7 @@ public static function detectType($feed, $specOnly = false)
//libxml_disable_entity_loader($oldValue);
if (!$dom) {
$err = error_get_last();
$phpErrormsg = isset($err) ? $err['message'] : null;
$phpErrormsg = isset($err['messsage'][0]) ? $err['message'] : null;
if (!isset($phpErrormsg)) {
if (function_exists('xdebug_is_enabled')) {
$phpErrormsg = '(error message not available, when XDebug is running)';
Expand Down
3 changes: 2 additions & 1 deletion packages/zend-gdata/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"zf1s/zend-http": "^1.13.4",
"zf1s/zend-mime": "^1.13.4",
"zf1s/zend-version": "^1.13.4",
"zf1s/zend-xml": "^1.13.4"
"zf1s/zend-xml": "^1.13.4",
"symfony/polyfill-php70": "^1.19"
},
"autoload": {
"psr-0": {
Expand Down
6 changes: 3 additions & 3 deletions packages/zend-gdata/library/Zend/Gdata/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,13 @@ public static function importString($string,
* @throws Zend_Gdata_App_Exception
* @return Zend_Gdata_App_Feed
*/
public static function importFile($filename,
$className='Zend_Gdata_App_Feed', $useIncludePath = false)
public static function importFile($filename, $className='Zend_Gdata_App_Feed', $useIncludePath = false)
{
error_clear_last();
$feed = @file_get_contents($filename, $useIncludePath);
if ($feed === false) {
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Gdata/App/Exception.php';
throw new Zend_Gdata_App_Exception(
"File could not be loaded: $phpErrormsg");
Expand Down
3 changes: 2 additions & 1 deletion packages/zend-gdata/library/Zend/Gdata/App/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,13 @@ public function transferFromDOM($node)
public function transferFromXML($xml)
{
if ($xml) {
error_clear_last();
// Load the feed as an XML DOMDocument object
$doc = new DOMDocument();
$doc = @Zend_Xml_Security::scan($xml, $doc);
if (!$doc) {
$err = error_get_last();
$phpErrormsg = isset($err) ? $err['message'] : '';
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Gdata/App/Exception.php';
throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $phpErrormsg");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,15 @@ public function hasError($errorCode) {
*/
public function importFromString($string) {
if ($string) {
// Check to see if an AppsForYourDomainError exists
//
// track_errors is temporarily enabled so that if an error
// occurs while parsing the XML we can append it to an
// exception by referencing $php_errormsg
error_clear_last();

$doc = new DOMDocument();
$doc = @Zend_Xml_Security::scan($string, $doc);

$err = error_get_last();
$phpErrormsg = isset($err) ? $err['message'] : '';
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
if (!$doc) {
// require_once 'Zend/Gdata/App/Exception.php';
// $php_errormsg is automatically generated by PHP if
// an error occurs while calling loadXML(), above.
throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $phpErrormsg");
}

Expand Down
3 changes: 2 additions & 1 deletion packages/zend-pdf/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"ext-zlib": "*",
"zf1s/zend-exception": "^1.13.4",
"zf1s/zend-log": "^1.13.4",
"zf1s/zend-memory": "^1.13.4"
"zf1s/zend-memory": "^1.13.4",
"symfony/polyfill-php70": "^1.19"
},
"autoload": {
"psr-0": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public static function encode($data, $params = null)
}

if (extension_loaded('zlib')) {

error_clear_last();
if (($output = @gzcompress($data)) === false) {
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception($phpErrormsg);
}
Expand All @@ -73,9 +73,10 @@ public static function encode($data, $params = null)
public static function decode($data, $params = null)
{
if (extension_loaded('zlib')) {
error_clear_last();
if (($output = @gzuncompress($data)) === false) {
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Pdf/Exception.php';
throw new Zend_Pdf_Exception($phpErrormsg);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/zend-search-lucene/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"ext-iconv": "*",
"zf1s/zend-exception": "^1.13.4",
"zf1s/zend-search": "^1.13.4",
"zf1s/zend-xml": "^1.13.4"
"zf1s/zend-xml": "^1.13.4",
"symfony/polyfill-php70": "^1.19"
},
"autoload": {
"psr-0": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ public function deleteFile($filename)
}
unset($this->_fileHandlers[$filename]);

error_clear_last();
if (!@unlink($this->_dirPath . '/' . $filename)) {
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Search/Lucene/Exception.php';
throw new Zend_Search_Lucene_Exception('Can\'t delete file: ' . $phpErrormsg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public function __construct($filename, $mode='r+b')
throw new Zend_Search_Lucene_Exception('File \'' . $filename . '\' is not readable.');
}


error_clear_last();
$this->_fileHandle = @fopen($filename, $mode);

if ($this->_fileHandle === false) {
$err = error_get_last();
$phpErrormsg = $err['message'];
$phpErrormsg = isset($err['message'][0]) ? $err['message'] : null;
// require_once 'Zend/Search/Lucene/Exception.php';
throw new Zend_Search_Lucene_Exception($phpErrormsg);
}
Expand Down

0 comments on commit 359b9b8

Please sign in to comment.