mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 04:13:53 +01:00
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:
parent
e78db7fa08
commit
0a4b179db5
4 changed files with 42 additions and 10 deletions
|
|
@ -144,8 +144,18 @@ class BookmarkItemsDao @Inject constructor(
|
|||
*/
|
||||
@SuppressLint("Range")
|
||||
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(
|
||||
getString(COLUMN_NAME),
|
||||
name,
|
||||
getString(COLUMN_DESCRIPTION),
|
||||
getString(COLUMN_IMAGE),
|
||||
getStringArray(COLUMN_INSTANCE_LIST),
|
||||
|
|
@ -155,7 +165,7 @@ class BookmarkItemsDao @Inject constructor(
|
|||
getStringArray(COLUMN_CATEGORIES_THUMBNAIL_LIST)
|
||||
),
|
||||
getString(COLUMN_IS_SELECTED).toBoolean(),
|
||||
getString(COLUMN_ID)
|
||||
id
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -128,7 +128,10 @@ class BookmarkPicturesDao @Inject constructor(
|
|||
}
|
||||
|
||||
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(
|
||||
fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -163,11 +163,19 @@ class RecentSearchesDao @Inject constructor(
|
|||
* @param cursor
|
||||
* @return RecentSearch object
|
||||
*/
|
||||
fun fromCursor(cursor: Cursor): RecentSearch = RecentSearch(
|
||||
uriForId(cursor.getInt(COLUMN_ID)),
|
||||
cursor.getString(COLUMN_NAME),
|
||||
Date(cursor.getLong(COLUMN_LAST_USED))
|
||||
)
|
||||
fun fromCursor(cursor: Cursor): RecentSearch {
|
||||
var query = cursor.getString(COLUMN_NAME)
|
||||
|
||||
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,
|
||||
|
|
|
|||
|
|
@ -6,9 +6,20 @@ import android.database.Cursor
|
|||
fun Cursor.getStringArray(name: String): List<String> =
|
||||
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")
|
||||
fun Cursor.getString(name: String): String =
|
||||
getString(getColumnIndex(name))
|
||||
fun Cursor.getString(name: String): String? {
|
||||
val index = getColumnIndex(name)
|
||||
if (index == -1) {
|
||||
return null
|
||||
}
|
||||
return getString(index)
|
||||
}
|
||||
|
||||
@SuppressLint("Range")
|
||||
fun Cursor.getInt(name: String): Int =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue