Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d298bfa authored by Chalard Jean's avatar Chalard Jean Committed by Automerger Merge Worker
Browse files

Merge "Fix cursor leaks into IPMS database" am: 885aeb89 am: f00e59d8 am:...

Merge "Fix cursor leaks into IPMS database" am: 885aeb89 am: f00e59d8 am: 7e58b7df am: b29783ec am: 2a0fccf6

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1332173

Change-Id: I3fca903fd3cab88aecdbf73a7d8ea4505fd00ac9
parents 299ed6f6 2a0fccf6
Loading
Loading
Loading
Loading
+59 −67
Original line number Original line Diff line number Diff line
@@ -335,21 +335,19 @@ public class IpMemoryStoreDatabase {
    // Returns the expiry date of the specified row, or one of the error codes above if the
    // Returns the expiry date of the specified row, or one of the error codes above if the
    // row is not found or some other error
    // row is not found or some other error
    static long getExpiry(@NonNull final SQLiteDatabase db, @NonNull final String key) {
    static long getExpiry(@NonNull final SQLiteDatabase db, @NonNull final String key) {
        final Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
        try (Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
                EXPIRY_COLUMN, // columns
                EXPIRY_COLUMN, // columns
                SELECT_L2KEY, // selection
                SELECT_L2KEY, // selection
                new String[] { key }, // selectionArgs
                new String[] { key }, // selectionArgs
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                null // orderBy
                null)) { // orderBy
        );
            // L2KEY is the primary key ; it should not be possible to get more than one
            // L2KEY is the primary key ; it should not be possible to get more than one
            // result here. 0 results means the key was not found.
            // result here. 0 results means the key was not found.
            if (cursor.getCount() != 1) return EXPIRY_ERROR;
            if (cursor.getCount() != 1) return EXPIRY_ERROR;
            cursor.moveToFirst();
            cursor.moveToFirst();
        final long result = cursor.getLong(0); // index in the EXPIRY_COLUMN array
            return cursor.getLong(0); // index in the EXPIRY_COLUMN array
        cursor.close();
        }
        return result;
    }
    }


    static final int RELEVANCE_ERROR = -1; // Legal values for relevance are positive
    static final int RELEVANCE_ERROR = -1; // Legal values for relevance are positive
@@ -399,20 +397,19 @@ public class IpMemoryStoreDatabase {
    @Nullable
    @Nullable
    static NetworkAttributes retrieveNetworkAttributes(@NonNull final SQLiteDatabase db,
    static NetworkAttributes retrieveNetworkAttributes(@NonNull final SQLiteDatabase db,
            @NonNull final String key) {
            @NonNull final String key) {
        final Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
        try (Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
                null, // columns, null means everything
                null, // columns, null means everything
                NetworkAttributesContract.COLNAME_L2KEY + " = ?", // selection
                NetworkAttributesContract.COLNAME_L2KEY + " = ?", // selection
                new String[] { key }, // selectionArgs
                new String[] { key }, // selectionArgs
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                null); // orderBy
                null)) { // orderBy
            // L2KEY is the primary key ; it should not be possible to get more than one
            // L2KEY is the primary key ; it should not be possible to get more than one
            // result here. 0 results means the key was not found.
            // result here. 0 results means the key was not found.
            if (cursor.getCount() != 1) return null;
            if (cursor.getCount() != 1) return null;
            cursor.moveToFirst();
            cursor.moveToFirst();
        final NetworkAttributes attributes = readNetworkAttributesLine(cursor);
            return readNetworkAttributesLine(cursor);
        cursor.close();
        }
        return attributes;
    }
    }


    private static final String[] DATA_COLUMN = new String[] {
    private static final String[] DATA_COLUMN = new String[] {
@@ -422,7 +419,7 @@ public class IpMemoryStoreDatabase {
    @Nullable
    @Nullable
    static byte[] retrieveBlob(@NonNull final SQLiteDatabase db, @NonNull final String key,
    static byte[] retrieveBlob(@NonNull final SQLiteDatabase db, @NonNull final String key,
            @NonNull final String clientId, @NonNull final String name) {
            @NonNull final String clientId, @NonNull final String name) {
        final Cursor cursor = db.query(PrivateDataContract.TABLENAME,
        try (Cursor cursor = db.query(PrivateDataContract.TABLENAME,
                DATA_COLUMN, // columns
                DATA_COLUMN, // columns
                PrivateDataContract.COLNAME_L2KEY + " = ? AND " // selection
                PrivateDataContract.COLNAME_L2KEY + " = ? AND " // selection
                + PrivateDataContract.COLNAME_CLIENT + " = ? AND "
                + PrivateDataContract.COLNAME_CLIENT + " = ? AND "
@@ -430,14 +427,13 @@ public class IpMemoryStoreDatabase {
                new String[] { key, clientId, name }, // selectionArgs
                new String[] { key, clientId, name }, // selectionArgs
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                null); // orderBy
                null)) { // orderBy
        // The query above is querying by (composite) primary key, so it should not be possible to
            // The query above is querying by (composite) primary key, so it should not be possible
        // get more than one result here. 0 results means the key was not found.
            // to get more than one result here. 0 results means the key was not found.
            if (cursor.getCount() != 1) return null;
            if (cursor.getCount() != 1) return null;
            cursor.moveToFirst();
            cursor.moveToFirst();
        final byte[] result = cursor.getBlob(0); // index in the DATA_COLUMN array
            return cursor.getBlob(0); // index in the DATA_COLUMN array
        cursor.close();
        }
        return result;
    }
    }


    /**
    /**
@@ -449,7 +445,7 @@ public class IpMemoryStoreDatabase {
            try {
            try {
                db.delete(NetworkAttributesContract.TABLENAME, null, null);
                db.delete(NetworkAttributesContract.TABLENAME, null, null);
                db.delete(PrivateDataContract.TABLENAME, null, null);
                db.delete(PrivateDataContract.TABLENAME, null, null);
                final Cursor cursorNetworkAttributes = db.query(
                try (Cursor cursorNetworkAttributes = db.query(
                        // table name
                        // table name
                        NetworkAttributesContract.TABLENAME,
                        NetworkAttributesContract.TABLENAME,
                        // column name
                        // column name
@@ -459,13 +455,10 @@ public class IpMemoryStoreDatabase {
                        null, // groupBy
                        null, // groupBy
                        null, // having
                        null, // having
                        null, // orderBy
                        null, // orderBy
                        "1"); // limit
                        "1")) { // limit
                if (0 != cursorNetworkAttributes.getCount()) {
                    if (0 != cursorNetworkAttributes.getCount()) continue;
                    cursorNetworkAttributes.close();
                    continue;
                }
                }
                cursorNetworkAttributes.close();
                try (Cursor cursorPrivateData = db.query(
                final Cursor cursorPrivateData = db.query(
                        // table name
                        // table name
                        PrivateDataContract.TABLENAME,
                        PrivateDataContract.TABLENAME,
                        // column name
                        // column name
@@ -475,14 +468,10 @@ public class IpMemoryStoreDatabase {
                        null, // groupBy
                        null, // groupBy
                        null, // having
                        null, // having
                        null, // orderBy
                        null, // orderBy
                        "1"); // limit
                        "1")) { // limit
                if (0 != cursorPrivateData.getCount()) {
                    if (0 != cursorPrivateData.getCount()) continue;
                    cursorPrivateData.close();
                    continue;
                }
                }
                cursorPrivateData.close();
                db.setTransactionSuccessful();
                db.setTransactionSuccessful();
                return;
            } catch (SQLiteException e) {
            } catch (SQLiteException e) {
                Log.e(TAG, "Could not wipe the data in database", e);
                Log.e(TAG, "Could not wipe the data in database", e);
            } finally {
            } finally {
@@ -575,7 +564,7 @@ public class IpMemoryStoreDatabase {


        final String selection = NetworkAttributesContract.COLNAME_EXPIRYDATE + " > ? AND ("
        final String selection = NetworkAttributesContract.COLNAME_EXPIRYDATE + " > ? AND ("
                + sj.toString() + ")";
                + sj.toString() + ")";
        final Cursor cursor = db.queryWithFactory(new CustomCursorFactory(args),
        try (Cursor cursor = db.queryWithFactory(new CustomCursorFactory(args),
                false, // distinct
                false, // distinct
                NetworkAttributesContract.TABLENAME,
                NetworkAttributesContract.TABLENAME,
                null, // columns, null means everything
                null, // columns, null means everything
@@ -584,11 +573,12 @@ public class IpMemoryStoreDatabase {
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                null, // orderBy
                null, // orderBy
                null); // limit
                null)) { // limit
            if (cursor.getCount() <= 0) return null;
            if (cursor.getCount() <= 0) return null;
            cursor.moveToFirst();
            cursor.moveToFirst();
            String bestKey = null;
            String bestKey = null;
        float bestMatchConfidence = GROUPCLOSE_CONFIDENCE; // Never return a match worse than this.
            float bestMatchConfidence =
                    GROUPCLOSE_CONFIDENCE; // Never return a match worse than this.
            while (!cursor.isAfterLast()) {
            while (!cursor.isAfterLast()) {
                final NetworkAttributes read = readNetworkAttributesLine(cursor);
                final NetworkAttributes read = readNetworkAttributesLine(cursor);
                final float confidence = read.getNetworkGroupSamenessConfidence(attr);
                final float confidence = read.getNetworkGroupSamenessConfidence(attr);
@@ -598,9 +588,9 @@ public class IpMemoryStoreDatabase {
                }
                }
                cursor.moveToNext();
                cursor.moveToNext();
            }
            }
        cursor.close();
            return bestKey;
            return bestKey;
        }
        }
    }


    /**
    /**
     * Delete a single entry by key.
     * Delete a single entry by key.
@@ -695,20 +685,21 @@ public class IpMemoryStoreDatabase {
        }
        }


        // Queries number of NetworkAttributes that start from the lowest expiryDate.
        // Queries number of NetworkAttributes that start from the lowest expiryDate.
        final Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
        final long expiryDate;
        try (Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
                new String[] {NetworkAttributesContract.COLNAME_EXPIRYDATE}, // columns
                new String[] {NetworkAttributesContract.COLNAME_EXPIRYDATE}, // columns
                null, // selection
                null, // selection
                null, // selectionArgs
                null, // selectionArgs
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                NetworkAttributesContract.COLNAME_EXPIRYDATE, // orderBy
                NetworkAttributesContract.COLNAME_EXPIRYDATE, // orderBy
                Integer.toString(number)); // limit
                Integer.toString(number))) { // limit
            if (cursor == null || cursor.getCount() <= 0) return Status.ERROR_GENERIC;
            if (cursor == null || cursor.getCount() <= 0) return Status.ERROR_GENERIC;
            cursor.moveToLast();
            cursor.moveToLast();


            // Get the expiryDate from last record.
            // Get the expiryDate from last record.
        final long expiryDate = getLong(cursor, NetworkAttributesContract.COLNAME_EXPIRYDATE, 0);
            expiryDate = getLong(cursor, NetworkAttributesContract.COLNAME_EXPIRYDATE, 0);
        cursor.close();
        }


        db.beginTransaction();
        db.beginTransaction();
        try {
        try {
@@ -736,16 +727,17 @@ public class IpMemoryStoreDatabase {


    static int getTotalRecordNumber(@NonNull final SQLiteDatabase db) {
    static int getTotalRecordNumber(@NonNull final SQLiteDatabase db) {
        // Query the total number of NetworkAttributes
        // Query the total number of NetworkAttributes
        final Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
        try (Cursor cursor = db.query(NetworkAttributesContract.TABLENAME,
                new String[] {"COUNT(*)"}, // columns
                new String[] {"COUNT(*)"}, // columns
                null, // selection
                null, // selection
                null, // selectionArgs
                null, // selectionArgs
                null, // groupBy
                null, // groupBy
                null, // having
                null, // having
                null); // orderBy
                null)) { // orderBy
            cursor.moveToFirst();
            cursor.moveToFirst();
            return cursor == null ? 0 : cursor.getInt(0);
            return cursor == null ? 0 : cursor.getInt(0);
        }
        }
    }


    // Helper methods
    // Helper methods
    private static String getString(final Cursor cursor, final String columnName) {
    private static String getString(final Cursor cursor, final String columnName) {