Skip to content

Commit 33a0a43

Browse files
committed
possible fix for #698: making sure all calls to Gson().fromJson are in a try/catch
1 parent 0ce58c8 commit 33a0a43

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

core/src/main/kotlin/org/evomaster/core/EMConfig.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ class EMConfig {
260260
They can be check only once all fields have been updated
261261
*/
262262

263+
if(!blackBox && bbSwaggerUrl.isNotBlank()){
264+
throw IllegalArgumentException("'bbSwaggerUrl' should be set only in black-box mode")
265+
}
266+
263267
if (blackBox && !bbExperiments) {
264268

265269
if(problemType == ProblemType.DEFAULT){

core/src/main/kotlin/org/evomaster/core/output/TestSuiteSplitter.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ object TestSuiteSplitter {
278278
* if it contains the field "errors" in its body.
279279
*/
280280
fun assessFailed(result: GraphQlCallResult): Boolean{
281-
val resultBody = Gson().fromJson(result.getBody(), HashMap::class.java)
281+
val resultBody = try {
282+
Gson().fromJson(result.getBody(), HashMap::class.java)
283+
} catch (e : JsonSyntaxException){
284+
return true //TODO should this be treated specially???
285+
}
282286
val errMsg = resultBody?.get("errors")
283287
return (resultBody!=null && errMsg != null)
284288
}

core/src/main/kotlin/org/evomaster/core/output/oracles/SchemaOracle.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.evomaster.core.output.oracles
22

33
import com.google.gson.Gson
4+
import com.google.gson.JsonSyntaxException
45
import io.swagger.v3.oas.models.media.*
56
import org.evomaster.core.logging.LoggingUtil
67
import org.evomaster.core.output.Lines
@@ -342,7 +343,12 @@ class SchemaOracle : ImplementedOracle() {
342343
var supported = true
343344

344345
if (res.getBodyType()?.isCompatible(MediaType.APPLICATION_JSON_TYPE) == true){
345-
val actualObject = Gson().fromJson(res.getBody(), Object::class.java)
346+
val actualObject = try {
347+
Gson().fromJson(res.getBody(), Object::class.java)
348+
} catch (e: JsonSyntaxException) {
349+
return false
350+
}
351+
346352
if (actualObject is Map<*,*>)
347353
supported = supportedObject(actualObject, call)
348354
else if (actualObject is List<*>

core/src/main/kotlin/org/evomaster/core/output/service/ApiTestCaseWriter.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.evomaster.core.output.service
22

33
import com.google.gson.Gson
4+
import com.google.gson.JsonSyntaxException
45
import org.evomaster.core.database.DbAction
56
import org.evomaster.core.database.DbActionResult
67
import org.evomaster.core.output.CookieWriter
@@ -68,14 +69,22 @@ abstract class ApiTestCaseWriter : TestCaseWriter() {
6869
when (bodyString?.trim()?.first()) {
6970
//TODO this should be handled recursively, and not ad-hoc here...
7071
'[' -> {
72+
try{
7173
// This would be run if the JSON contains an array of objects.
7274
val list = Gson().fromJson(bodyString, List::class.java)
7375
handleAssertionsOnList(list, lines, "", bodyVarName)
76+
} catch (e: JsonSyntaxException) {
77+
lines.add("/* Failed to parse JSON response */")
78+
}
7479
}
7580
'{' -> {
7681
// JSON contains an object
77-
val resContents = Gson().fromJson(bodyString, Map::class.java)
78-
handleAssertionsOnObject(resContents as Map<String, *>, lines, "", bodyVarName)
82+
try {
83+
val resContents = Gson().fromJson(bodyString, Map::class.java)
84+
handleAssertionsOnObject(resContents as Map<String, *>, lines, "", bodyVarName)
85+
} catch (e: JsonSyntaxException) {
86+
lines.add("/* Failed to parse JSON response */")
87+
}
7988
}
8089
'"' -> {
8190
lines.add(bodyIsString(bodyString, GeneUtils.EscapeMode.BODY, bodyVarName))

0 commit comments

Comments
 (0)