Closed
Bug 1466088
Opened 7 years ago
Closed 7 years ago
Android strings.xml support should check for formatter errors
Categories
(Localization Infrastructure and Tools :: compare-locales, enhancement)
Localization Infrastructure and Tools
compare-locales
Tracking
(Not tracked)
RESOLVED
FIXED
People
(Reporter: Pike, Unassigned)
References
()
Details
Attachments
(1 file)
Java/Android have their own variant of a printf formatter, documented at https://developer.android.com/reference/java/util/Formatter.
To quote:
Formatted printing for the Java language is heavily inspired by C's printf. Although the format strings are similar to C, some customizations have been made to accommodate the Java language and exploit some of its features. Also, Java formatting is more strict than C's; for example, if a conversion is incompatible with a flag, an exception will be thrown. In C inapplicable flags are silently ignored. The format strings are thus intended to be recognizable to C programmers but not necessarily completely compatible with those in C.
Kinda like gecko's, just the other way around now (we don't throw at all, we just use the conversion that's good for the type).
Reporter | ||
Comment 1•7 years ago
|
||
Notes for posterity from a playful little android app I do:
Wrong formatter sometimes crashes. %d instead of %s crashes, %s instead of %d works. Yeeeeehaw.
Out-of-bounds printf crashes (%3$s for two args)
Sparse usage of params works (Just using %2$s, without %1$s)
Double usage of %2$s works.
Non-numbered arguments are incremented independently of numbered ones, and can be mixed.
Reporter | ||
Comment 2•7 years ago
|
||
This is tricky, 'cause both Java and maybe Android have weird opinions.
Generally speaking, though, these strings are only affecting the running build if the string is actually passed to the Formatter. I wonder if we should explicitly flag that in strings.xml.
Sebastian, what do you think? Also, what's a good way to drive conclusions on questions like this?
Proposed example:
...
<!--
@param %1$s: "pretty"
@checks: printf
-->
<string>This is %1$s funny.</string
Flags: needinfo?(s.kaspari)
Comment 3•7 years ago
|
||
We talked about this in SF. One important conclusion was that all our apps only use a very small subset of the available options (basically only %X$s and %X$d with X ∈ ℕ). So for now we could enforce much stricter rules and decide to add broader support once actually needed (unlikely). The same is true for plurals which are not used by any our apps so far.
Flags: needinfo?(s.kaspari)
Reporter | ||
Comment 4•7 years ago
|
||
With that reduced feature set, I think we can do proper detection for just %s and %d.
That won't catch abuse of other formats in en-US, but that's OK, says Sebastian.
Reporter | ||
Comment 5•7 years ago
|
||
Compare reference for each localized parameter.
If there's no reference found, error, as an out-of-bounds parameter
crashes.
This assumes that all parameters are actually used in the Reference,
which should be OK.
If there's a mismatch in the formatter, error.
As in other parts of Android, don't bother about the source
position.
Comment 6•7 years ago
|
||
Comment on attachment 8999899 [details]
bug 1466088, check for printfs in Android, just %s and %d, r=sebastian
Sebastian Kaspari (:sebastian) has approved the revision.
Attachment #8999899 -
Flags: review+
Reporter | ||
Comment 7•7 years ago
|
||
Thanks, landed as https://hg.mozilla.org/l10n/compare-locales/rev/2c7e6c887628.
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
You need to log in
before you can comment on or make changes to this bug.
Description
•