Closed
Bug 1247557
Opened 10 years ago
Closed 10 years ago
[Static Analysis][Resource leak] In function query (Uri mainUri, Uri fallbackUri, String condition)
Categories
(Firefox for Android Graveyard :: Settings and Preferences, defect)
Firefox for Android Graveyard
Settings and Preferences
Tracking
(firefox47 fixed)
RESOLVED
FIXED
Firefox 47
| Tracking | Status | |
|---|---|---|
| firefox47 | --- | fixed |
People
(Reporter: andi, Assigned: mcomella)
References
(Blocks 1 open bug)
Details
(Keywords: coverity, Whiteboard: CID 121161)
Attachments
(1 file, 1 obsolete file)
The Static Analysis tool Coverity added that a memory leak can occur in the following code:
>> Cursor cursor = mCr.query(mainUri, null, condition, null, null);
>>
>> if (Build.MANUFACTURER.equals(SAMSUNG_MANUFACTURER) && (cursor == null || cursor.getCount() == 0)) {
>> cursor = mCr.query(fallbackUri, null, null, null, null);
>> }
I think this is possible only if cursor is a valid object. We should do a check inside the if branch for cursor validity and if so close it.
| Reporter | ||
Comment 1•10 years ago
|
||
Review commit: https://reviewboard.mozilla.org/r/34487/diff/#index_header
See other reviews: https://reviewboard.mozilla.org/r/34487/
Attachment #8718281 -
Flags: review?(mark.finkle)
Comment 2•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
Passing to Mike. He's a better reviewer for this.
Attachment #8718281 -
Flags: review?(mark.finkle) → review?(michael.l.comella)
| Assignee | ||
Comment 3•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
https://reviewboard.mozilla.org/r/34487/#review31791
Looks good – just a small style nit.
Thanks for the patch!
::: mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java:158
(Diff revision 1)
> + if ( cursor != null )
> + cursor.close();
nit: Our style is `if (expression) {`. Also, we always brace if statements.
Attachment #8718281 -
Flags: review?(michael.l.comella) → review+
| Assignee | ||
Comment 4•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
https://reviewboard.mozilla.org/r/34487/#review31795
Actually, this should really be in a `try {} finally { Cursor.close(); }` since it's safer. Can you figure out a way to shoe-horn the code into that? Perhaps with some temp variables.
Attachment #8718281 -
Flags: review+
| Assignee | ||
Updated•10 years ago
|
Blocks: fennec-coverity
| Reporter | ||
Comment 5•10 years ago
|
||
Hello Michael, i'm not a java expert but as far as i can see query doesn't throw any exception so i don't see the point of using try {} finally {}. The exceptions that can be triggered inside query are handled locally by that function.
If there is something that i didn't get let me know.
Flags: needinfo?(michael.l.comella)
| Assignee | ||
Comment 6•10 years ago
|
||
(In reply to Andi-Bogdan Postelnicu from comment #5)
> Hello Michael, i'm not a java expert but as far as i can see query doesn't
> throw any exception so i don't see the point of using try {} finally {}. The
> exceptions that can be triggered inside query are handled locally by that
> function.
> If there is something that i didn't get let me know.
It's a defensive coding technique – there are no Exceptions thrown at the moment but will the code throw Exceptions after a future developer adds code? Additionally, the code can still throw RuntimeExceptions that don't need to be explicitly rethrown by the method (e.g. NullPointerException), which the calling code can (questionably) catch and recover from, in which case the resource would be left open.
Flags: needinfo?(michael.l.comella)
| Reporter | ||
Comment 7•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
Review request updated; see interdiff: https://reviewboard.mozilla.org/r/34487/diff/1-2/
Attachment #8718281 -
Flags: review?(mark.finkle)
| Reporter | ||
Comment 8•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
Review request updated; see interdiff: https://reviewboard.mozilla.org/r/34487/diff/2-3/
Attachment #8718281 -
Attachment description: MozReview Request: Bug 1247557 - close cursor if it's valid before doing a new query. r?mfinkle → MozReview Request: Bug 1247557 - avoid memory leak by closing cursorFirst at the final of the function if cursorSecond has been created. r?mcomella
Attachment #8718281 -
Flags: review?(mark.finkle) → review?(michael.l.comella)
| Reporter | ||
Updated•10 years ago
|
Attachment #8718281 -
Flags: review?(s.kaspari)
Updated•10 years ago
|
Attachment #8718281 -
Flags: review?(s.kaspari)
Comment 9•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
https://reviewboard.mozilla.org/r/34487/#review32091
::: mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java:161
(Diff revision 3)
> - cursor = mCr.query(fallbackUri, null, null, null, null);
> + cursorSecond = mCr.query(fallbackUri, null, null, null, null);
Isn't this the only situation where you need to close the previous cursor if it is not null? The code might be simpler if you close it here.
::: mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java:164
(Diff revision 3)
> + catch (Exception e) {
> + Log.e(LOGTAG, "Exception while querying db: ", e);
> + }
You can have try { } finaly { } blocks without catching an exception.
| Reporter | ||
Comment 10•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
Review request updated; see interdiff: https://reviewboard.mozilla.org/r/34487/diff/3-4/
| Reporter | ||
Comment 11•10 years ago
|
||
Comment on attachment 8718281 [details]
MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
Review request updated; see interdiff: https://reviewboard.mozilla.org/r/34487/diff/4-5/
Attachment #8718281 -
Attachment description: MozReview Request: Bug 1247557 - avoid memory leak by closing cursorFirst at the final of the function if cursorSecond has been created. r?mcomella → MozReview Request: Bug 1247557 - avoid memory leak by closing cursor before being initialized for the 2nd time. r?mcomella
| Assignee | ||
Comment 12•10 years ago
|
||
Hey Andi-Bogdan.
bug 1247636 and this one are some pretty complex error cases and, as you mentioned you were not a Java expert, I'd like to avoid the back-and-forth review cycle if it bothers you. Would you like me to take over finishing these bugs?
In any case, I really appreciate your help and your watch over our Coverity defects! :)
Flags: needinfo?(bogdan.postelnicu)
| Reporter | ||
Comment 13•10 years ago
|
||
Hey Michael,
No problem for me, do as you consider it's best.
Flags: needinfo?(bogdan.postelnicu)
| Assignee | ||
Comment 14•10 years ago
|
||
Review commit: https://reviewboard.mozilla.org/r/35897/diff/#index_header
See other reviews: https://reviewboard.mozilla.org/r/35897/
Attachment #8722200 -
Flags: review?(s.kaspari)
| Assignee | ||
Updated•10 years ago
|
Assignee: bogdan.postelnicu → michael.l.comella
| Assignee | ||
Updated•10 years ago
|
Attachment #8718281 -
Attachment is obsolete: true
Attachment #8718281 -
Flags: review?(michael.l.comella)
Comment 15•10 years ago
|
||
Comment on attachment 8722200 [details]
MozReview Request: Bug 1247557 - Close Cursors in AndroidImport. r=sebastian
https://reviewboard.mozilla.org/r/35897/#review32663
I don't think the catch is needed, see below. Without that r+. :)
::: mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java:165
(Diff revision 1)
> + } catch (final Exception e) {
> + if (cursor != null) {
> + cursor.close();
> + }
> + throw e;
> + }
Isn't this already (and shouldn't this) be handled by the callers? They all wrap their calls to query() in try..finally and always close the cursor.
Attachment #8722200 -
Flags: review?(s.kaspari) → review+
| Assignee | ||
Comment 16•10 years ago
|
||
https://reviewboard.mozilla.org/r/35897/#review32663
> Isn't this already (and shouldn't this) be handled by the callers? They all wrap their calls to query() in try..finally and always close the cursor.
But if `query` throws, the `Cursor` will not be returned so the callers can't close it.
| Assignee | ||
Comment 17•10 years ago
|
||
https://reviewboard.mozilla.org/r/35897/#review32663
> But if `query` throws, the `Cursor` will not be returned so the callers can't close it.
Spoke with rnewman – the catch complicates the code path so much more than the probability that someone will come along and change the code, miss the leaked Cursor in review, and we won't catch it again in static analysis that it's not worth adding in the extra lines.
| Assignee | ||
Comment 18•10 years ago
|
||
https://hg.mozilla.org/integration/fx-team/rev/7a2db11cc81934507f730ae24429a41be6202438
Bug 1247557 - Close Cursors in AndroidImport. r=sebastian
Comment 19•10 years ago
|
||
| bugherder | ||
Status: NEW → RESOLVED
Closed: 10 years ago
status-firefox47:
--- → fixed
Resolution: --- → FIXED
Target Milestone: --- → Firefox 47
Updated•5 years ago
|
Product: Firefox for Android → Firefox for Android Graveyard
You need to log in
before you can comment on or make changes to this bug.
Description
•