Skip to content

Commit

Permalink
Check for empty string in mb_ord()
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Aug 4, 2017
1 parent aa0e402 commit 6b73b2d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -5074,6 +5074,11 @@ static inline zend_long php_mb_ord(const char* str, size_t str_len, const char*
return -1;
}

if (str_len == 0) {
php_error_docref(NULL, E_WARNING, "Empty string");
return -1;
}

{
long orig_illegalchars = MBSTRG(illegalchars);
MBSTRG(illegalchars) = 0;
Expand Down
9 changes: 8 additions & 1 deletion ext/mbstring/tests/mb_chr_ord.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ mb_chr() and mb_ord()
<?php
ini_set('internal_encoding', 'utf-8');
for($ch = 1; $ch < 80000; $ch++) {
if ($ch !== mb_ord(mb_chr($ch))) echo "ERROR($ch)\n";
$str = mb_chr($ch);
if (false === $str) {
echo "ERROR($ch)\n";
continue;
}
if ($ch != mb_ord($str)) {
echo "REAL ERROR($ch)\n";
}
}
echo 'OK';
--EXPECTF--
Expand Down
6 changes: 5 additions & 1 deletion ext/mbstring/tests/mb_ord.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var_dump(
mb_ord("\u{d800}", "pass"),
mb_ord("\u{d800}", "jis"),
mb_ord("\u{d800}", "cp50222"),
mb_ord("\u{d800}", "utf-7")
mb_ord("\u{d800}", "utf-7"),
mb_ord("")
);
?>
--EXPECTF--
Expand All @@ -33,6 +34,9 @@ Warning: mb_ord(): Unsupported encoding "jis" %s 12
Warning: mb_ord(): Unsupported encoding "cp50222" %s 13

Warning: mb_ord(): Unsupported encoding "utf-7" %s 14

Warning: mb_ord(): Empty string in %s on line %d
bool(false)
bool(false)
bool(false)
bool(false)
Expand Down

0 comments on commit 6b73b2d

Please sign in to comment.