Skip to content

Commit 419687c

Browse files
committed
Bugfix: use libxml_get_errors instead of looping over libxml_get_last_error
1 parent 7d7dc3d commit 419687c

File tree

3 files changed

+21
-33
lines changed

3 files changed

+21
-33
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"ext-libxml": "*",
3333
"ext-pcre": "*",
3434
"ext-spl": "*",
35-
"ext-xmlreader": "*",
3635

3736
"simplesamlphp/assert": "^1.2",
3837
"simplesamlphp/composer-xmlprovider-installer": "~1.0.0",

src/DOMDocumentFactory.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
namespace SimpleSAML\XML;
66

77
use DOMDocument;
8-
use Exception;
9-
use LibXMLError;
108
use SimpleSAML\Assert\Assert;
119
use SimpleSAML\XML\Exception\IOException;
1210
use SimpleSAML\XML\Exception\RuntimeException;
1311
use SimpleSAML\XML\Exception\SchemaViolationException;
1412
use SimpleSAML\XML\Exception\UnparseableXMLException;
15-
use XMLReader;
1613

1714
use function array_unique;
15+
use function file_exists;
1816
use function file_get_contents;
1917
use function func_num_args;
2018
use function implode;
2119
use function libxml_clear_errors;
22-
use function libxml_get_last_error;
20+
use function libxml_get_errors;
2321
use function libxml_set_external_entity_loader;
2422
use function libxml_use_internal_errors;
2523
use function sprintf;
@@ -150,29 +148,19 @@ public static function schemaValidation(
150148
string $schemaFile,
151149
int $options = self::DEFAULT_OPTIONS,
152150
): void {
153-
$xmlReader = XMLReader::XML($xml, null, $options);
154-
Assert::notFalse($xmlReader, SchemaViolationException::class);
155-
156-
libxml_use_internal_errors(true);
157-
158-
try {
159-
$xmlReader->setSchema($schemaFile);
160-
} catch (Exception) {
161-
$err = libxml_get_last_error();
162-
throw new SchemaViolationException(trim($err->message) . ' on line ' . $err->line);
151+
if (!file_exists($schemaFile)) {
152+
throw new IOException('File not found.');
163153
}
164154

165-
$msgs = [];
166-
while ($xmlReader->read()) {
167-
if (!$xmlReader->isValid()) {
168-
$err = libxml_get_last_error();
169-
if ($err instanceof LibXMLError) {
170-
$msgs[] = trim($err->message) . ' on line ' . $err->line;
171-
}
155+
$document = DOMDocumentFactory::fromString($xml);
156+
$result = $document->schemaValidate($schemaFile);
157+
158+
if ($result === false) {
159+
$msgs = [];
160+
foreach (libxml_get_errors() as $err) {
161+
$msgs[] = trim($err->message) . ' on line ' . $err->line;
172162
}
173-
}
174163

175-
if ($msgs) {
176164
throw new SchemaViolationException(sprintf(
177165
"XML schema validation errors:\n - %s",
178166
implode("\n - ", array_unique($msgs)),

tests/XML/DOMDocumentFactoryTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace SimpleSAML\Test\XML;
66

77
use DOMDocument;
8-
use Exception;
98
use PHPUnit\Framework\Attributes\CoversClass;
109
use PHPUnit\Framework\Attributes\Group;
1110
use PHPUnit\Framework\TestCase;
@@ -15,8 +14,6 @@
1514
use SimpleSAML\XML\Exception\SchemaViolationException;
1615
use SimpleSAML\XML\Exception\UnparseableXMLException;
1716

18-
use function restore_error_handler;
19-
use function set_error_handler;
2017
use function strval;
2118

2219
/**
@@ -145,13 +142,17 @@ public function testSchemaValidationUnknownSchemaFileFails(): void
145142
$file = 'tests/resources/xml/ssp_Chunk.xml';
146143
$schemaFile = 'tests/resources/schemas/doesnotexist.xsd';
147144

148-
// Dirty trick to catch the warning emitted by PHP
149-
set_error_handler(static function (int $errno, string $errstr): never {
150-
restore_error_handler();
151-
throw new Exception($errstr, $errno);
152-
}, E_WARNING);
145+
$this->expectExceptionMessage('File not found.');
146+
DOMDocumentFactory::fromFile($file, $schemaFile);
147+
}
153148

154-
$this->expectExceptionMessage('Failed to locate the main schema resource at');
149+
150+
public function testSchemaValidationInvalidSchemaFileFails(): void
151+
{
152+
$file = 'tests/resources/xml/ssp_Chunk.xml';
153+
$schemaFile = 'resources/schemas/xml.xsd';
154+
155+
$this->expectException(SchemaViolationException::class);
155156
DOMDocumentFactory::fromFile($file, $schemaFile);
156157
}
157158
}

0 commit comments

Comments
 (0)