Upgrade to StandardCharsets.UTF_8 when minSdkVersion >= 19

NEW
Unassigned

Status

()

Firefox for Android
General
8 months ago
8 months ago

People

(Reporter: ahunt, Unassigned)

Tracking

(Blocks: 1 bug)

Trunk
Points:
---

Firefox Tracking Flags

(firefox55 affected)

Details

(Reporter)

Description

8 months ago
API 19 introduces StandardCharsets, along with StandardCharsets.UTF_8:
https://developer.android.com/reference/java/nio/charset/StandardCharsets.html


Currently we have many variations on the following, which could be made simpler with StandardCharsets:
Charset c = Charset.forName("UTF-8");
Stream s = new SomeStream(new OtherStream(...), c);

(Which in itself is safe enough, but Charset.forName() can throw which needs to be handled.)


Then we also have things like:
Stream s = new SomeStream(new OtherStream(...), Charset.forName("UTF-8"));

That's bad - we'll leak the OtherStream if Charset.forName() throws. (Infer catches that, but we've historically ignored Infer.)


And then we have examples like this:
outputStreamWriter = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");

Also bad, we leak the FileOutputStream if the charset can't be recognised (whereas OutputStreamWriter(OutputStream, Charset) doesn't throw, and can be sued with StandardCharsets).
 

Once we can use API 19, we should just use StandardCharsets everywhere. (The various leaking cases can be tackled as part of the plentiful Infer bugs, see Bug 1175203.)
(Reporter)

Updated

8 months ago
Blocks: 1356640
(Reporter)

Comment 1

8 months ago
It turns out we already have StringUtils.UTF_8 doing pretty much the same thing, we should probably use that for now (and replace it with StandardCharsets once it becomes available):
https://dxr.mozilla.org/mozilla-central/source/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java#28
You need to log in before you can comment on or make changes to this bug.