Skip to content

resource leak: database cursor not closed #4591

@andrew659

Description

@andrew659

Dear developers,

I found several potential leaks of database cursor in your code.

(1) In the getLastRecordedVideoUri() method of MediaUtils class:

https://github.com/wordpress-mobile/WordPress-Android/blob/develop/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/MediaUtils.java

public static Uri getLastRecordedVideoUri(Activity activity) {
        String[] proj = { MediaStore.Video.Media._ID };
        Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String sortOrder = MediaStore.Video.VideoColumns.DATE_TAKEN + " DESC";
        CursorLoader loader = new CursorLoader(activity, contentUri, proj, null, null, sortOrder);
        Cursor cursor = loader.loadInBackground();
        cursor.moveToFirst();

        return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0));
    }

The cursor is not closed.

(2) In the uploadImage() method of the PostUploadService class at line 530:

https://github.com/wordpress-mobile/WordPress-Android/blob/develop/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostUploadService.java

Cursor cur = mContext.getContentResolver().query(imageUri, projection, null, null, null);
cur is not closed

(3) In the uploadVideo() method of the PostUploadService class at line 677:

Cursor cur = mContext.getContentResolver().query(videoUri, projection, null, null, null);
cur is not closed

(4) In the getDefaultCategory() method of the SiteSettingsInterface class:

https://github.com/wordpress-mobile/WordPress-Android/blob/develop/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsInterface.java

starting from line 154:

Cursor cursor = SiteSettingsTable.getCategory(id);
            if (cursor != null && cursor.moveToFirst()) {
                category.deserializeFromDatabase(cursor);
                return category.name;
            }

the cursor is not closed

(5) In the getPathFromContentUri() method of the EditPostActivity class:

https://github.com/wordpress-mobile/WordPress-Android/blob/develop/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java

starting from line 1525:

Cursor cur = getContentResolver().query(imageUri, projection, null, null, null);
        if (cur != null && cur.moveToFirst()) {
            int dataColumn = cur.getColumnIndex(MediaStore.Images.Media.DATA);
            path = cur.getString(dataColumn);
            cur.close();
        }
        return path;

If the cursor is empty (moveToFirst() will return false) the cursor will be left unclosed. In the above code, the cur.close() will only execute for non-empty cursors when moveToFirst() returns true.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions