Permalink
Browse files

PEAR 18251: fix for checking the record length

Apply Christian Rishøj's patch for checking the record length to avoid
using the result of the regex if it doesn't match.

In addition, prefer the actual length of the record to the declared
length of the record, and add a warning when that condition occurs.


git-svn-id: https://svn.php.net/repository/pear/packages/File_MARC/trunk@308144 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent d44164b commit 1e68e8f57fb8539e9400cb722edb984b20bb09ef Dan Scott committed Feb 8, 2011
Showing with 12 additions and 10 deletions.
  1. +12 −10 File/MARC.php
View
@@ -258,20 +258,22 @@ private function _decode($text)
{
$marc = new File_MARC_Record($this);
+ // fallback on the actual byte length
+ $record_length = strlen($text);
+
$matches = array();
- if (!preg_match("/^(\d{5})/", $text, $matches)) {
+ if (preg_match("/^(\d{5})/", $text, $matches)) {
+ // Store record length
+ $record_length = $matches[1];
+ if ($record_length != strlen($text)) {
+ $marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INCORRECT_LENGTH], array("record_length" => $record_length, "actual" => strlen($text))));
+ // Real beats declared byte length
+ $record_length = strlen($text);
+ }
+ } else {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_NONNUMERIC_LENGTH], array("record_length" => substr($text, 0, 5))));
}
- // Store record length
- $record_length = $matches[1];
-
- if ($record_length != strlen($text)) {
- $marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INCORRECT_LENGTH], array("record_length" => $record_length, "actual" => strlen($text))));
- // give up and set the record length to the actual byte length
- $record_length = strlen($text);
- }
-
if (substr($text, -1, 1) != File_MARC::END_OF_RECORD)
throw new File_MARC_Exception(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_TERMINATOR], File_MARC_Exception::ERROR_INVALID_TERMINATOR);

0 comments on commit 1e68e8f

Please sign in to comment.