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

Add safe inet_pton #428

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
'simplexml_load_file',
'simplexml_load_string',
'fgetcsv', // This function need to return false when iterating on an end of file.
'inet_pton',
];
17 changes: 17 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Safe;

use Safe\Exceptions\FilesystemException;
use Safe\Exceptions\NetworkException;
use function error_clear_last;
use const PREG_NO_ERROR;

use Safe\Exceptions\MiscException;
Expand Down Expand Up @@ -440,3 +442,18 @@ function fgetcsv($stream, int $length = null, string $separator = ",", string $e
}
return $safeResult;
}

/**
* @param string $ip A human readable IPv4 or IPv6 address.
* @return string Returns the in_addr representation of the given ip,
* @throws NetworkException
*/
function inet_pton(string $ip): string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know why we need a special case here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"returns false on error" is calculated by a bunch of regexes matching against the human-readable text of the descriptions D:

#498 adds this case into the regex rather than manually adding a special case

{
error_clear_last();
$safeResult = \inet_pton($ip);
if ($safeResult === false) {
throw NetworkException::createFromPhpError();
}
return $safeResult;
}
1 change: 1 addition & 0 deletions rector-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@
'imap_unsubscribe' => 'Safe\imap_unsubscribe',
'imap_utf8_to_mutf7' => 'Safe\imap_utf8_to_mutf7',
'inet_ntop' => 'Safe\inet_ntop',
'inet_pton' => 'Safe\inet_pton',
'inflate_add' => 'Safe\inflate_add',
'inflate_get_read_len' => 'Safe\inflate_get_read_len',
'inflate_get_status' => 'Safe\inflate_get_status',
Expand Down
Loading