Repository navigation
Fix #14402: Match the gettext MO context exactly - #21113
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 Summary
Priority: Change: Bug fix · Severity of issue fixed: Low Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #21113 +/- ##
=========================================
Coverage 81.87% 81.87%
Complexity 11562 11562
=========================================
Files 374 374
Lines 30314 30314
=========================================
Hits 24820 24820
Misses 5494 5494 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The |
78b4c61 to
277d66c
Compare
`GettextMoFile::load()` compared the requested context with
`strncmp($id, $context, $separatorPosition)`, so only as many bytes as
the context stored in the MO entry were compared. A request for category
`backend/foo` or `backendbar` also loaded every message of context
`backend`. `GettextPoFile::load()` doesn't have this bug because it
compares the context with `===`. The MO loader now does the same with
`substr($id, 0, $separatorPosition) === $context`.
Commands I ran on PHP 8.5.9.
```
vendor/bin/phpunit tests/framework/i18n/GettextMoFileTest.php
master code 1) testLoadDoesNotMatchContextByPrefix
Failed asserting that two arrays are identical. (context1/sub returns the 3 context1 messages)
Tests: 3, Assertions: 29, Failures: 1.
this branch OK (3 tests, 32 assertions)
vendor/bin/phpunit --group i18n --exclude-group db,wincache
Tests: 721, Assertions: 4220, Skipped: 1, Incomplete: 1.
vendor/bin/phpunit --exclude-group db,wincache,mysql,pgsql,mssql,oci,cubrid
Tests: 5411, Assertions: 17953, Skipped: 64, Incomplete: 3.
vendor/bin/php-cs-fixer check --config=.php-cs-fixer.dist.php
Found 0 of 813 files that can be fixed
vendor/bin/phpstan analyse -c phpstan.dist.neon framework/i18n/GettextMoFile.php tests/framework/i18n/GettextMoFileTest.php
[OK] No errors
```
A console Application with `GettextMessageSource` and an MO file that
holds contexts `backend` and `backendbar` gives this output.
```
master this branch
Yii::t('backendbar', 'Save') Speichern (backend) Save
Yii::t('backend/foo', 'Save') Speichern (backend) Save
Yii::t('backend', 'Save') Speichern (backend) Speichern (backend)
Yii::t('backendbar', 'Cancel') Abbrechen (backendbar) Abbrechen (backendbar)
```
I didn't run `DbMessageSourceTest` (it needs a MySQL server) or the PHP
7.4 to 8.4 matrix.
277d66c to
9d77d4a
Compare
|
Thks 👍 |
|
Thanks for merging, @terabytesoftw, and thanks @samdark for the review. The MO loader now matches a message context exactly, the same way the PO loader already did. It's good to see it in.
Thank you,
Michael Lip
Founder of https://zovo.one, 108 Chrome extensions with 150,000+ users
Also building https://makeroll.com and https://ml0x.com, open-source guardrails for Claude Code agents
Open source has to win, and small fixes like this one are my part of it
GitHub Sponsors keeps these fixes coming, https://github.com/sponsors/theluckystrike
|
GettextMoFile::load()matched a message context withstrncmp($id, $context, $separatorPosition). That compares only as many bytes as the context stored in the MO entry, so loading categorybackend/fooorbackendbaralso returned every message of contextbackend. The same happens to the commonappandapp/modelscategory pair.GettextPoFile::load()doesn't have this bug because it compares the context with===. The MO loader now does the same withsubstr($id, 0, $separatorPosition) === $context.GettextMoFileTest::testLoadDoesNotMatchContextByPrefixloadscontext1/sub,context10andcontextfrom the existingtests/data/i18n/test.mo. On mastercontext1/subreturns the 3context1messages and the test fails withTests: 3, Assertions: 29, Failures: 1. With this change all three return[],context1still returns its 3 messages, and the file passes withOK (3 tests, 32 assertions).End to end I ran a console
ApplicationwithGettextMessageSourceand an MO file holding contextsbackend(Save) andbackendbar(Cancel).Other runs on PHP 8.5.9.
I didn't run
DbMessageSourceTest(it needs MySQL) or the PHP 7.4 to 8.4 matrix.Summary by Sourcery
Prevent gettext MO files from returning translations belonging to similarly prefixed contexts.
Bug Fixes:
Tests: