Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Trying to access array offset on value of type null" #228

Merged
merged 1 commit into from
Feb 12, 2020

Conversation

dryabov
Copy link
Contributor

@dryabov dryabov commented Dec 31, 2019

Added a check that $previous is not null (i.e. previous token exists)

Added a check that `$previous` is not null (i.e. previous token exists)
@GrahamCampbell
Copy link
Contributor

When could it be null?

@dryabov
Copy link
Contributor Author

dryabov commented Jan 2, 2020

It's hard t say, I've found it in log file of a laravel-based project:

[2019-12-31 00:08:44] production.ERROR: Trying to access array offset on value of type null {"exception":"[object] (ErrorException(code: 0): Trying to access array offset on value of type null at _/vendor/egulias/email-validator/EmailValidator/Parser/Parser.php:147)
[stacktrace]
#0 _/vendor/egulias/email-validator/EmailValidator/Parser/Parser.php(147): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 _/vendor/egulias/email-validator/EmailValidator/Parser/Parser.php(127): Egulias\\EmailValidator\\Parser\\Parser->escaped()
#2 _/vendor/egulias/email-validator/EmailValidator/Parser/LocalPart.php(57): Egulias\\EmailValidator\\Parser\\Parser->isFWS()
#3 _/vendor/egulias/email-validator/EmailValidator/EmailParser.php(48): Egulias\\EmailValidator\\Parser\\LocalPart->parse()
#4 _/vendor/egulias/email-validator/EmailValidator/Validation/RFCValidation.php(30): Egulias\\EmailValidator\\EmailParser->parse()
#5 _/vendor/egulias/email-validator/EmailValidator/EmailValidator.php(37): Egulias\\EmailValidator\\Validation\\RFCValidation->isValid()
#6 _/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/IdentificationHeader.php(182): Egulias\\EmailValidator\\EmailValidator->isValid()
...

All input emails in isValid look like '[email protected]' or '[email protected]'.

@egulias
Copy link
Owner

egulias commented Jan 2, 2020

That's rare, yes. If "previous" is null, it would have generated an error earlier.
Can you please provide a test that reproduces the error?

@dryabov
Copy link
Contributor Author

dryabov commented Jan 2, 2020

It looks like to be environment-dependent. I've added

if ($previous === null) {
    file_put_contents(__FILE__ . '.log',
                      var_export($this->lexer, true) . "\n\n\n",
                      FILE_APPEND);
}

inside of Egulias\EmailValidator\Parser\Parser::escaped and got following state:

Egulias\EmailValidator\EmailLexer::__set_state(array(
   'charValue' => 
  array (
    '(' => 49,
    ')' => 261,
    '<' => 272,
    '>' => 273,
    '[' => 262,
    ']' => 263,
    ':' => 265,
    ';' => 275,
    '@' => 64,
    '\\' => 92,
    '/' => 278,
    ',' => 274,
    '.' => 46,
    '"' => 34,
    '-' => 264,
    '::' => 266,
    ' ' => 267,
    '	' => 268,
    '
' => 269,
    '
' => 270,
    '
' => 301,
    'IPv6' => 271,
    '{' => 276,
    '}' => 277,
    '' => NULL,
    '\\0' => 0,
  ),
   'hasInvalidTokens' => false,
   'previous' => NULL,
   'input' => '[email protected]',
   'tokens' => 
  array (
    0 => 
    array (
      'value' => 'a',
      'type' => 300,
      'position' => 0,
    ),
    1 => 
    array (
      'value' => '2',
      'type' => 300,
      'position' => 1,
    ),
    2 => 
    array (
      'value' => 'cf',
      'type' => 300,
      'position' => 2,
    ),
    3 => 
    array (
      'value' => '7',
      'type' => 300,
      'position' => 4,
    ),
    4 => 
    array (
      'value' => 'd',
      'type' => 300,
      'position' => 5,
    ),
    5 => 
    array (
      'value' => '22',
      'type' => 300,
      'position' => 6,
    ),
    6 => 
    array (
      'value' => 'c',
      'type' => 300,
      'position' => 8,
    ),
    7 => 
    array (
      'value' => '7',
      'type' => 300,
      'position' => 9,
    ),
    8 => 
    array (
      'value' => 'f',
      'type' => 300,
      'position' => 10,
    ),
    9 => 
    array (
      'value' => '734',
      'type' => 300,
      'position' => 11,
    ),
    10 => 
    array (
      'value' => 'febfa',
      'type' => 300,
      'position' => 14,
    ),
    11 => 
    array (
      'value' => '5',
      'type' => 300,
      'position' => 19,
    ),
    12 => 
    array (
      'value' => 'c6',
      'type' => 300,
      'position' => 20,
    ),
    13 => 
    array (
      'value' => 'f',
      'type' => 300,
      'position' => 22,
    ),
    14 => 
    array (
      'value' => '7105',
      'type' => 300,
      'position' => 23,
    ),
    15 => 
    array (
      'value' => 'c',
      'type' => 300,
      'position' => 27,
    ),
    16 => 
    array (
      'value' => '3062',
      'type' => 300,
      'position' => 28,
    ),
    17 => 
    array (
      'value' => '@',
      'type' => 64,
      'position' => 32,
    ),
    18 => 
    array (
      'value' => 'swift',
      'type' => 300,
      'position' => 33,
    ),
    19 => 
    array (
      'value' => '.',
      'type' => 46,
      'position' => 38,
    ),
    20 => 
    array (
      'value' => 'generated',
      'type' => 300,
      'position' => 39,
    ),
  ),
   'position' => 2,
   'peek' => 0,
   'lookahead' => 
  array (
    'value' => '2',
    'type' => 300,
    'position' => 1,
  ),
   'token' => 
  array (
    'value' => 'a',
    'type' => 300,
    'position' => 0,
  ),
))

Unfortunately my attempt to reproduce the issue via a simple script has been failed (i.e. no error is triggered):

<?php
error_reporting(-1);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
$emailValidator = new \Egulias\EmailValidator\EmailValidator();
$emailValidator->isValid(
        '[email protected]',
        new \Egulias\EmailValidator\Validation\RFCValidation()
);

Not sure what I could check additionally.

@dryabov
Copy link
Contributor Author

dryabov commented Jan 2, 2020

According to composer.lock, the installed version is 2.1.6. Maybe the issue has been fixed in 2.1.10, but unfortunately I'm not directly involved into that project and cannot update it to check.

@egulias
Copy link
Owner

egulias commented Jan 5, 2020

@dryabov maybe you can check Swiftmailer's email generator to see which chars they use and then explore with the lexer test (https://github.com/egulias/EmailValidator/blob/master/Tests/EmailValidator/EmailLexerTest.php#L126) to see if you are able to produce a 'null' element.

@ricardo118
Copy link

Ive also started getting this error recently, it was working fine. Maybe related to updating to php7.4?

@egulias
Copy link
Owner

egulias commented Jan 18, 2020

Thanks @ricardo118 . Are you able to reproduce it with a test?

@cetetesoft
Copy link

Ive also started getting this error recently, it was working fine. Maybe related to updating to php7.4?

This is indeed due to upgrading to PHP 7.4. Its first backward incompatible change is:

Trying to use values of type null, bool, int, float or resource as an array (such as $null["key"]) will now generate a notice.

It seems to work fine after replacing line 147 of Parser.php with:
if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH

@egulias
Copy link
Owner

egulias commented Feb 7, 2020

Hi @cetetesoft good to hear. However, the mistery is under which circumstances $previous can be null. I'd like to have a test reproducing the bug.

@cetetesoft
Copy link

@egulias, any email address seems to trigger the error:
(new EmailValidator)->isValid('[email protected]',new RFCValidation);

@egulias egulias mentioned this pull request Feb 12, 2020
@egulias
Copy link
Owner

egulias commented Feb 12, 2020

@cetetesoft however, all test pass in PHP 7.4. I'm investigating the issue.

@egulias
Copy link
Owner

egulias commented Feb 12, 2020

I'll merge for now and find later how this can happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants