Skip to content

Commit 0a4b179

Browse files
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>
1 parent e78db7f commit 0a4b179

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

app/src/main/java/fr/free/nrw/commons/bookmarks/items/BookmarkItemsDao.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,18 @@ class BookmarkItemsDao @Inject constructor(
144144
*/
145145
@SuppressLint("Range")
146146
fun fromCursor(cursor: Cursor) = with(cursor) {
147+
var name = getString(COLUMN_NAME)
148+
if (name == null) {
149+
name = ""
150+
}
151+
152+
var id = getString(COLUMN_ID)
153+
if (id == null) {
154+
id = ""
155+
}
156+
147157
DepictedItem(
148-
getString(COLUMN_NAME),
158+
name,
149159
getString(COLUMN_DESCRIPTION),
150160
getString(COLUMN_IMAGE),
151161
getStringArray(COLUMN_INSTANCE_LIST),
@@ -155,7 +165,7 @@ class BookmarkItemsDao @Inject constructor(
155165
getStringArray(COLUMN_CATEGORIES_THUMBNAIL_LIST)
156166
),
157167
getString(COLUMN_IS_SELECTED).toBoolean(),
158-
getString(COLUMN_ID)
168+
id
159169
)
160170
}
161171

app/src/main/java/fr/free/nrw/commons/bookmarks/pictures/BookmarkPicturesDao.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ class BookmarkPicturesDao @Inject constructor(
128128
}
129129

130130
fun fromCursor(cursor: Cursor): Bookmark {
131-
val fileName = cursor.getString(COLUMN_MEDIA_NAME)
131+
var fileName = cursor.getString(COLUMN_MEDIA_NAME)
132+
if (fileName == null) {
133+
fileName = ""
134+
}
132135
return Bookmark(
133136
fileName, cursor.getString(COLUMN_CREATOR), uriForName(fileName)
134137
)

app/src/main/java/fr/free/nrw/commons/explore/recentsearches/RecentSearchesDao.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,19 @@ class RecentSearchesDao @Inject constructor(
163163
* @param cursor
164164
* @return RecentSearch object
165165
*/
166-
fun fromCursor(cursor: Cursor): RecentSearch = RecentSearch(
167-
uriForId(cursor.getInt(COLUMN_ID)),
168-
cursor.getString(COLUMN_NAME),
169-
Date(cursor.getLong(COLUMN_LAST_USED))
170-
)
166+
fun fromCursor(cursor: Cursor): RecentSearch {
167+
var query = cursor.getString(COLUMN_NAME)
168+
169+
if (query == null) {
170+
query = ""
171+
}
172+
173+
return RecentSearch(
174+
uriForId(cursor.getInt(COLUMN_ID)),
175+
query,
176+
Date(cursor.getLong(COLUMN_LAST_USED))
177+
)
178+
}
171179

172180
/**
173181
* This class contains the database table architechture for recent searches,

app/src/main/java/fr/free/nrw/commons/utils/DatabaseUtils.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ import android.database.Cursor
66
fun Cursor.getStringArray(name: String): List<String> =
77
stringToArray(getString(name))
88

9+
/**
10+
* Gets the String at the current row and specified column.
11+
*
12+
* @param name The name of the column to get the String from.
13+
* @return The String if the column exists. Else, null is returned.
14+
*/
915
@SuppressLint("Range")
10-
fun Cursor.getString(name: String): String =
11-
getString(getColumnIndex(name))
16+
fun Cursor.getString(name: String): String? {
17+
val index = getColumnIndex(name)
18+
if (index == -1) {
19+
return null
20+
}
21+
return getString(index)
22+
}
1223

1324
@SuppressLint("Range")
1425
fun Cursor.getInt(name: String): Int =

0 commit comments

Comments
 (0)