#61794 closed defect (bug) (fixed)
Bugfix in class-wp-translation-file-mo for "PHP Warning: Undefined array key 1 in"
| Reported by: | pander | Owned by: | swissspidy |
|---|---|---|---|
| Priority: | low | Milestone: | 6.7 |
| Component: | I18N | Version: | 6.5 |
| Severity: | minor | Keywords: | has-patch commit has-unit-tests |
| Cc: | Focuses: |
Description
The warning "PHP Warning: Undefined array key 1 in" is flooding the error log file of my web service running WordPress.
I think the plugin that is the cause of this warning for me is https://github.com/qtranslate/qtranslate-xt/ or https://icalevents.com/ but I have seen it being reported for many different plugins.
Line 161 of wp-includes/l10n/class-wp-translation-file-mo.php is triggering this, is
<?php list( $name, $value ) = array_map( 'trim', explode( ':', $meta_line, 2 ) );
To prevent the warning, I have changed line 157 from
<?php if ( '' === $meta_line ) {
to
<?php if ( '' === $meta_line || strpos($meta_line, ':') === false ) {
I know the warning is valid and should be fixed elsewhere, but my knowledge of WordPress is limited and this fix prevents that performance is impeded. Perhaps the warning should be caught in a different way, but this fixed it for now for me.
If there are way to pinpoint better what is causing this, please let me know. I can help to locate it but need some guidance.
Change History (12)
#1
@
2 years ago
- Component General → I18N
- Focuses performance removed
- Owner set to
- Status new → assigned
#2
@
2 years ago
The fix sounds reasonable to me, it adds some hardening.
If there are way to pinpoint better what is causing this, please let me know. I can help to locate it but need some guidance.
One of your plugins has an invalid MO file. You can try a `var_dump( $this->file ) on line 160 to find the culprit.
#3
@
2 years ago
- Keywords has-patch added
- Milestone Awaiting Review → 6.7
- Priority normal → low
- Version 6.6.1 → 6.5
This ticket was mentioned in PR #7115 on WordPress/wordpress-develop by @swissspidy.
2 years ago
#4
Trac ticket: https://core.trac.wordpress.org/ticket/61794
This ticket was mentioned in PR #7116 on WordPress/wordpress-develop by @narenin.
2 years ago
#5
Trac ticket: https://core.trac.wordpress.org/ticket/61794
@mukesh27 commented on PR #7116:
2 years ago
#6
@swissspidy Already opened #7115
@swissspidy commented on PR #7115:
2 years ago
#9
#10
@
2 years ago
Thanks. The culprit is
string(97) "/var/www/DOMAINNAME/wp-content/plugins/amr-ical-events-list/lang/amr-ical-events-list-nl_NL.mo"
and that was repeated 1142 times.
I regenerated the MO file with
cd /var/www/DOMAINNAME/wp-content/plugins/amr-ical-events-list/lang mv amr-ical-events-list-nl_NL.mo /tmp msgfmt amr-ical-events-list-nl_NL.po -o amr-ical-events-list-nl_NL.mo
and this fixed it.
This ticket was mentioned in PR #13850 on WordPress/wordpress-develop by AbdiTolesa.
6 weeks ago
#12
- Keywords has-unit-tests added
Summary
WP_Translation_File_MO::parse_file() already validates individual metadata lines (see #61794), but the offsets and lengths taken from the MO header (originals_addr, translations_addr, hash_addr) were not consistently validated before being used to calculate table boundaries and read strings.
Specifically:
hash_addrwas never checked against the file length (onlyoriginals_addrandtranslations_addrwere).- There was no check that
originals_addr <= translations_addr <= hash_addr, so a malformed header could make the computed table lengths (translations_addr - originals_addr,hash_addr - translations_addr) negative. A negative length passed tosubstr()doesn't error, but silently slices the wrong region of the file instead of the intended table. - Individual originals/translations table entries'
pos/lengthpairs were used directly insubstr()without checking they described a range within the file.
A malformed or truncated .mo file could therefore be parsed without error while producing unexpected/garbage data, instead of being cleanly rejected.
Changes
- In
class-wp-translation-file-mo.php, added a bounds/ordering check on the header addresses (originals_addr <= translations_addr <= hash_addr <= file_length), returning the existing'Invalid data'error, consistent with the neighboring checks. - Skip individual originals/translations table entries whose
pos/lengthdescribe a range outside the file, rather than reading whatever happens to be there. - Added test cases to
data_invalid_filesinwpTranslationsConvert.phpcovering:originals_addraftertranslations_addr,translations_addrafterhash_addr, and an out-of-boundshash_addr.
Test plan
- [x]
php -lon both changed files. - [x] Reproduced the issue against the pre-fix code with a standalone script constructing malformed headers matching the new test cases — confirmed they parsed with
error() === null(i.e. silently treated as valid) before this fix, and now correctly return'Invalid data'. - [x] Verified all existing
.mofixtures undertests/phpunit/data/l10n/still parse with no errors and unchanged header counts. - [ ] Could not run the full PHPUnit suite in this environment (no database available) — recommend running
phpunit --filter test_invalid_files tests/phpunit/tests/l10n/wpTranslationsConvert.phpbefore merging.
🤖 Generated with Claude Code
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Removing performance focus as this is not related to performance.