Fixes Issue 6436: getString(...) must not be null (#6474)

* DatabaseUtils.kt: change getString() to allow null returns

Before this change, a call to getString() would assume that the specified column
name actually exists. A bad String input would cause a null value to be returned
to getString(), which would then throw a NPE because getString() can only return
non null Strings.

This change expands the getString() method to check if the column name exists.
If it does exist, the String is retrieved normally. Else, a null value is
returned. The method signature is changed to allow null return values.

* *Dao.kt: change some usages of getString()

Before this change, the getString() method in DatabaseUtils.kt was changed
to allow returning a null value upon method failure. All usages of getString()
were not changed.

This change updates all usages of getString() which require non null return
values. If null is returned, an empty string is used instead.

---------

Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
Jason-Whitmore 2025-10-10 22:12:19 -07:00 committed by GitHub
parent e78db7fa08
commit 0a4b179db5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 10 deletions

View file

@ -144,8 +144,18 @@ class BookmarkItemsDao @Inject constructor(
*/ */
@SuppressLint("Range") @SuppressLint("Range")
fun fromCursor(cursor: Cursor) = with(cursor) { fun fromCursor(cursor: Cursor) = with(cursor) {
var name = getString(COLUMN_NAME)
if (name == null) {
name = ""
}
var id = getString(COLUMN_ID)
if (id == null) {
id = ""
}
DepictedItem( DepictedItem(
getString(COLUMN_NAME), name,
getString(COLUMN_DESCRIPTION), getString(COLUMN_DESCRIPTION),
getString(COLUMN_IMAGE), getString(COLUMN_IMAGE),
getStringArray(COLUMN_INSTANCE_LIST), getStringArray(COLUMN_INSTANCE_LIST),
@ -155,7 +165,7 @@ class BookmarkItemsDao @Inject constructor(
getStringArray(COLUMN_CATEGORIES_THUMBNAIL_LIST) getStringArray(COLUMN_CATEGORIES_THUMBNAIL_LIST)
), ),
getString(COLUMN_IS_SELECTED).toBoolean(), getString(COLUMN_IS_SELECTED).toBoolean(),
getString(COLUMN_ID) id
) )
} }

View file

@ -128,7 +128,10 @@ class BookmarkPicturesDao @Inject constructor(
} }
fun fromCursor(cursor: Cursor): Bookmark { fun fromCursor(cursor: Cursor): Bookmark {
val fileName = cursor.getString(COLUMN_MEDIA_NAME) var fileName = cursor.getString(COLUMN_MEDIA_NAME)
if (fileName == null) {
fileName = ""
}
return Bookmark( return Bookmark(
fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName) fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName)
) )

View file

@ -163,11 +163,19 @@ class RecentSearchesDao @Inject constructor(
* @param cursor * @param cursor
* @return RecentSearch object * @return RecentSearch object
*/ */
fun fromCursor(cursor: Cursor): RecentSearch = RecentSearch( fun fromCursor(cursor: Cursor): RecentSearch {
uriForId(cursor.getInt(COLUMN_ID)), var query = cursor.getString(COLUMN_NAME)
cursor.getString(COLUMN_NAME),
Date(cursor.getLong(COLUMN_LAST_USED)) if (query == null) {
) query = ""
}
return RecentSearch(
uriForId(cursor.getInt(COLUMN_ID)),
query,
Date(cursor.getLong(COLUMN_LAST_USED))
)
}
/** /**
* This class contains the database table architechture for recent searches, * This class contains the database table architechture for recent searches,

View file

@ -6,9 +6,20 @@ import android.database.Cursor
fun Cursor.getStringArray(name: String): List<String> = fun Cursor.getStringArray(name: String): List<String> =
stringToArray(getString(name)) stringToArray(getString(name))
/**
* Gets the String at the current row and specified column.
*
* @param name The name of the column to get the String from.
* @return The String if the column exists. Else, null is returned.
*/
@SuppressLint("Range") @SuppressLint("Range")
fun Cursor.getString(name: String): String = fun Cursor.getString(name: String): String? {
getString(getColumnIndex(name)) val index = getColumnIndex(name)
if (index == -1) {
return null
}
return getString(index)
}
@SuppressLint("Range") @SuppressLint("Range")
fun Cursor.getInt(name: String): Int = fun Cursor.getInt(name: String): Int =