mbstring_check_encoding() in cache database backend doesn't actually work

Created on 30 August 2015, almost 10 years ago
Updated 10 August 2025, 13 days ago

Problem/Motivation

When installing Drupal 8 on a system that does not have mbstring support, a fatal error is thrown after the database has been created, as the cache module uses mb_check_encoding in the normalizeCid function, without checking for existence of that function.

Fatal error: Call to undefined function Drupal\Core\Cache\mb_check_encoding() in core/lib/Drupal/Core/Cache/DatabaseBackend.php on line 410

As mbstring is not a requirement (the installer system check gives a warning if mbstring is not present, not an error), the module should either check to see if it's present, or not use it at all.

Steps to reproduce:

  • make sure mbstring is disabled
  • follow install.php
  • after entering the database details, the error is thrown

Proposed resolution

Add a method in DatabaseBackend to check for the existence of mbstring, and use a regular expression match to determine if the CID is ASCII instead. This allows for the non-mbstring functionality to be tested on testing infrastructure with mbstring installed. The DatabaseBackend unit tests should be expanded to cover this case.

Remaining tasks

  • Write patch with tests in DatabaseBackendUnitTest
  • Review patch
  • (done)

User interface changes

None

API changes

An internal method is added to the cache DatabaseBackend.

Data model changes

None.

Beta phase evaluation

<!--Uncomment the relevant rows for the issue. -->
🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

cache system

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • I just upgraded from Drupal 10 to Drupal 11.2.3, and I still get the error:

    Deprecated function: mb_check_encoding(): Calling mb_check_encoding() without argument is deprecated in Drupal\Core\Cache\DatabaseBackend->normalizeCid() (line 501 of core/lib/Drupal/Core/Cache/DatabaseBackend.php).
    Drupal\Core\Cache\DatabaseBackend->normalizeCid() (Line: 120)
    Drupal\Core\Cache\DatabaseBackend->getMultiple() (Line: 110)
    Drupal\Core\Cache\DatabaseBackend->get() (Line: 326)
    Drupal\Core\Cache\CacheCollector->lazyLoadCache() (Line: 144)
    Drupal\Core\Cache\CacheCollector->get() (Line: 112)
    
  • The patch for Drupal 11.2.3, possible Drupal 11.x

  • 🇬🇧United Kingdom catch

    mb_check_encoding() shouldn't be called without arguments - that suggests NULL is being passed as a cache ID?

Production build 0.71.5 2024