Using substr() on a UTF8 string that contains multibyte characters may produce an invalid UTF8 string.

For example, this code may produce an invalid string (will result in "warning: htmlspecialchars(): Invalid multibyte sequence in argument in /.../bootstrap.inc on line 857."):

    if (strlen($block_description) > 16) {
      $block_description = substr($block_description, 0, 16) . '...';
    }

and can be changed to this to solve the problem:

    if (drupal_strlen($block_description) > 16) {
      $block_description = truncate_utf8($block_description, 16, FALSE, TRUE);
    }

The bug can be reproduced with the following translation:

msgid "None"
msgstr "טרם ניצפה"

The attached patch makes this change for the three instances I could find where this potential problem is apparent.
There are other uses of substr() which are either not operating on a translated strings or which I was not sure about.

CommentFileSizeAuthor
views-6.x-2.12.patch1.67 KBgeneticdrift

Comments

geneticdrift’s picture

Status: Active » Needs review
dawehner’s picture

Sadly this patch doesn't apply anymore to 6.x-3.x, but it definitive seems to make sense.

In general patches like that aren't committed to 6.x-2.x anymore.

damien tournoud’s picture

Status: Needs review » Needs work

You want drupal_strlen() and drupal_substr(), or strlen() and truncate_utf8() but not another mix of them.

truncate_utf8() is designed to truncate a UTF8 string on a specified number of *bytes*. If you want to truncate on a specified number of *characters*, use drupal_substr().

damien tournoud’s picture

Status: Needs work » Needs review

Fact-checking #3, this is not true anymore since Drupal 6. Sorry about the noise.

dawehner’s picture

Version: 6.x-2.12 » 6.x-3.x-dev
Status: Needs review » Needs work
mustanggb’s picture

Issue summary: View changes
Status: Needs work » Closed (won't fix)