I am dealing with a module (CMIS) that is trying to insert content into the database with the wrong charset. I was getting the following two errors:

    Warning: htmlspecialchars(): Invalid multibyte sequence in argument in check_plain() (line 1545 of /var/www/drupal/includes/bootstrap.inc).
    PDOException: in field_sql_storage_field_storage_write() (line 448 of /var/www/drupal/modules/field/modules/field_sql_storage/field_sql_storage.module).

However, the exception handling code in errors.inc combined with check_plain() means that the error message is completely lost in the PDOException. Note that the exception above has no message or trace.

That is, in includes/errors.inc#_drupal_decode_exception(), if $exception->args() contains invalid characters such as from file content, the displayed exception in the logs is simply:

PDOException: in field_sql_storage_field_storage_write()

If I remove print_r($exception->args, TRUE) from the $message, then the exception becomes (infinitely more useful):

PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xD0\xCF\x11\xE0\xA1\xB1...' for column 'body_value' at row 1: INSERT INTO {field_data_body} (entity_type, entity_id, revision_id, bundle, delta, language, body_value, body_summary, body_format) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); in field_sql_storage_field_storage_write() (line 448 of C:\hubnet\drupal\modules\field\modules\field_sql_storage\field_sql_storage.module).

This is definitely due to the if ($exception instanceof PDOException) block, because if I remove it, the exception becomes (still useful):

PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xD0\xCF\x11\xE0\xA1\xB1...' for column 'body_value' at row 1 in PDOStatement->execute() (line 2139 of C:\hubnet\drupal\includes\database\database.inc).

I'm not sure what the best solution is, since in most cases you'd want to keep the arguments. Is there a way to check that a given string is valid w.r.t. a given charset before passing it to check_plain() - perhaps with drupal_validate_utf8()?

This seems to almost be a duplicate of "Error messages not generated in UTF8 in some situations" - but this is occurring on two different platforms (Linux, Windows) with MySQL.

Issue fork drupal-1929862

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

soundasleep’s picture

StatusFileSize
new595 bytes

Attached is a proposed patch that replaces invalid UTF-8 with simply [invalid UTF-8].

David_Rothstein’s picture

Version: 7.20 » 7.x-dev
Status: Active » Needs review
StatusFileSize
new628 bytes

This looks like a useful patch for debugging these kinds of situations. I guess it's kind of a hack but I can't think of anything better at the moment...

Here is a reroll that applies correctly from the Drupal root.

Would need to go in Drupal 8 too if it's actually going to be committed, but I needed the Drupal 7 patch myself now and only had time to reroll that one.

David_Rothstein’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Status: Needs review » Needs work

The last submitted patch, pdo-exception-invalid-utf8-1929862-2.patch, failed testing.

David_Rothstein’s picture

Version: 8.x-dev » 7.x-dev
Status: Needs work » Needs review

Oh, I think it failed because I put the issue against Drupal 8. Moving back to Drupal 7 (temporarily) to let the testbot run.

David_Rothstein’s picture

David_Rothstein’s picture

Version: 7.x-dev » 8.x-dev
StatusFileSize
new648 bytes

Tests pass - here's a completely untested Drupal 8 version.

mskicker’s picture

#7 work for me
really tanx and very imp please add to dev ASAP

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch no longer applies. Would it be possible to add a test for this?

nitesh sethia’s picture

Status: Needs work » Needs review
StatusFileSize
new2.97 KB

Rerolled the patch as per the latest release.

daffie’s picture

Status: Needs review » Needs work

@Nitesh Sethia: How is this just a reroll of the previous patch? The previous patch is 648 bytes and your patch is 2970 bytes?

sharique’s picture

Issue tags: +Needs manual testing

The file error.inc is changed very much, perhaps re-written completely, so this issue need to be tested manually to see if it is still an issue or it is resolved.

rpayanm’s picture

Issue tags: -Needs reroll
rpayanm’s picture

Status: Needs work » Needs review
StatusFileSize
new675 bytes
dalin’s picture

Status: Needs review » Needs work

Great to see a reroll, but it looks like we still need some unit tests.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sahil.goyal’s picture

StatusFileSize
new793 bytes
new944 bytes

Hi, Seen that last patch was too much old and i tried as well to apply on my local environment 9.4.x but it doesn't get apply properly so making a reroll patch for the same and adding a reroll file.

sahil.goyal’s picture

Status: Needs work » Needs review
rpayanm’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

In addition to the manual testing this will need a test case to show the issue.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Believe this change now qualifies for the new policy of being small enough to not warrant tests. but definitely needs an issue summary update.

Anonymous’s picture

tpham1023 made their first commit to this issue’s fork.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.