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

Make iconv //IGNORE behavior conform to POSIX and the docs #16846

Closed
wants to merge 3 commits 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
28 changes: 0 additions & 28 deletions ext/iconv/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,6 @@ int main(void) {
AS_VAR_IF([php_cv_iconv_errno], [yes],,
[AC_MSG_FAILURE([The iconv check failed, 'errno' is missing.])])

AC_CACHE_CHECK([if iconv supports //IGNORE], [php_cv_iconv_ignore],
[AC_RUN_IFELSE([AC_LANG_SOURCE([
#include <iconv.h>
#include <stdlib.h>

int main(void) {
iconv_t cd = iconv_open( "UTF-8//IGNORE", "UTF-8" );
if(cd == (iconv_t)-1) {
return 1;
}
char *in_p = "\xC3\xC3\xC3\xB8";
size_t in_left = 4, out_left = 4096;
char *out = malloc(out_left);
char *out_p = out;
size_t result = iconv(cd, (char **) &in_p, &in_left, (char **) &out_p, &out_left);
if(result == (size_t)-1) {
return 1;
}
return 0;
}
])],
[php_cv_iconv_ignore=yes],
[php_cv_iconv_ignore=no],
[php_cv_iconv_ignore=no])])
AS_VAR_IF([php_cv_iconv_ignore], [no],
[AC_DEFINE([ICONV_BROKEN_IGNORE], [1],
[Define to 1 if iconv has broken IGNORE.])])

LIBS=$save_LIBS
CFLAGS=$save_CFLAGS

Expand Down
29 changes: 0 additions & 29 deletions ext/iconv/iconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,24 +415,6 @@ static php_iconv_err_t _php_iconv_appendc(smart_str *d, const char c, iconv_t cd
}
/* }}} */

/* {{{ */
#ifdef ICONV_BROKEN_IGNORE
static int _php_check_ignore(const char *charset)
{
size_t clen = strlen(charset);
if (clen >= 9 && strcmp("//IGNORE", charset+clen-8) == 0) {
return 1;
}
if (clen >= 19 && strcmp("//IGNORE//TRANSLIT", charset+clen-18) == 0) {
return 1;
}
return 0;
}
#else
#define _php_check_ignore(x) (0)
#endif
/* }}} */

/* {{{ php_iconv_string() */
PHP_ICONV_API php_iconv_err_t php_iconv_string(const char *in_p, size_t in_len, zend_string **out, const char *out_charset, const char *in_charset)
{
Expand All @@ -442,7 +424,6 @@ PHP_ICONV_API php_iconv_err_t php_iconv_string(const char *in_p, size_t in_len,
size_t bsz, result = 0;
php_iconv_err_t retval = PHP_ICONV_ERR_SUCCESS;
zend_string *out_buf;
int ignore_ilseq = _php_check_ignore(out_charset);

*out = NULL;

Expand All @@ -466,16 +447,6 @@ PHP_ICONV_API php_iconv_err_t php_iconv_string(const char *in_p, size_t in_len,
result = iconv(cd, (ICONV_CONST char **) &in_p, &in_left, (char **) &out_p, &out_left);
out_size = bsz - out_left;
if (result == (size_t)(-1)) {
if (ignore_ilseq && errno == EILSEQ) {
if (in_left <= 1) {
result = 0;
} else {
errno = 0;
in_p++;
in_left--;
continue;
}
}

if (errno == E2BIG && in_left > 0) {
/* converted string is longer than out buffer */
Expand Down
27 changes: 0 additions & 27 deletions ext/iconv/tests/bug48147.phpt

This file was deleted.

42 changes: 42 additions & 0 deletions ext/iconv/tests/dont-ignore-invalid-inputs.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
iconv with //IGNORE should not ignore invalid input sequences
--EXTENSIONS--
iconv
--SKIPIF--
<?php
// POSIX 2024 standardizes "//IGNORE", but some implementations (like musl)
// do not support it yet. If iconv() doesn't understand "//IGNORE", the
// tests below will still fail, but you'll get an error message about
// finding an unexpected string in your encoding name rather than the
// expected illegal-sequence error.
if (ICONV_IMPL == "unknown") {
die("skip iconv implementation may not understand //IGNORE");
}
?>
--FILE--
<?php
$text = "aa\xC3\xC3\xC3\xB8aa";
var_dump(iconv("UTF-8", "UTF-8", $text));
var_dump(iconv("UTF-8", "UTF-8//IGNORE", $text));
// only invalid
var_dump(iconv("UTF-8", "UTF-8//IGNORE", "\xC3"));
// start invalid
var_dump(iconv("UTF-8", "UTF-8//IGNORE", "\xC3\xC3\xC3\xB8aa"));
// finish invalid
var_dump(iconv("UTF-8", "UTF-8//IGNORE", "aa\xC3\xC3\xC3"));
?>
--EXPECTF--
Notice: iconv(): Detected an illegal character in input string in %s on line %d
bool(false)

Notice: iconv(): Detected an illegal character in input string in %s on line %d
bool(false)

Notice: iconv(): Detected an incomplete multibyte character in input string in %s on line %d
bool(false)

Notice: iconv(): Detected an illegal character in input string in %s on line %d
bool(false)

Notice: iconv(): Detected an incomplete multibyte character in input string in %s on line %d
bool(false)
Loading