Skip to content

Commit 61361a0

Browse files
authored
Merge pull request #552 from ooni/fix-null-test-keys-crash
Handle invalid test_keys values. Do not store empty test_keys map.
2 parents 4048877 + 23ca4b5 commit 61361a0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

composeApp/src/commonMain/kotlin/org/ooni/probe/data/repositories/MeasurementRepository.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.ooni.probe.data.repositories
22

33
import app.cash.sqldelight.coroutines.asFlow
44
import app.cash.sqldelight.coroutines.mapToList
5+
import co.touchlab.kermit.Logger
56
import kotlinx.coroutines.flow.Flow
67
import kotlinx.coroutines.flow.map
78
import kotlinx.coroutines.withContext
@@ -180,7 +181,7 @@ class MeasurementRepository(
180181
id = MeasurementModel.Id(id),
181182
resultId = result_id?.let(ResultModel::Id) ?: return null,
182183
testName = test_name,
183-
testKeys = test_keys?.let { json.decodeFromString<TestKeys>(it) },
184+
testKeys = test_keys?.let(::decodeTestKeys),
184185
descriptorName = descriptor_name,
185186
descriptorRunId = descriptor_runId?.let(InstalledTestDescriptorModel::Id),
186187
)
@@ -191,9 +192,19 @@ class MeasurementRepository(
191192
id = MeasurementModel.Id(id),
192193
resultId = result_id?.let(ResultModel::Id) ?: return null,
193194
testName = test_name,
194-
testKeys = test_keys?.let { json.decodeFromString<TestKeys>(it) },
195+
testKeys = test_keys?.let(::decodeTestKeys),
195196
descriptorName = descriptor_name,
196197
descriptorRunId = descriptor_runId?.let(InstalledTestDescriptorModel::Id),
197198
)
198199
}
200+
201+
private fun decodeTestKeys(value: String): TestKeys? {
202+
if (value == "null") return null // Due to a past bug
203+
return try {
204+
json.decodeFromString<TestKeys>(value)
205+
} catch (e: Exception) {
206+
Logger.e("Invalid stored test_keys", e)
207+
null
208+
}
209+
}
199210
}

composeApp/src/commonMain/kotlin/org/ooni/probe/domain/RunNetTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,11 @@ class RunNetTest(
178178
event.result.testKeys?.let {
179179
val testKeys = extractTestKeysPropertiesToJson(it)
180180
measurement = measurement.copy(
181-
testKeys = json.encodeToString(testKeys),
181+
testKeys = if (testKeys.isEmpty()) {
182+
null
183+
} else {
184+
json.encodeToString(testKeys)
185+
},
182186
)
183187
}
184188

0 commit comments

Comments
 (0)