Skip to content

Commit

Permalink
util: Fix JsonOutStream to disallow invalid Float literals
Browse files Browse the repository at this point in the history
  • Loading branch information
afrankvt committed Mar 19, 2024
1 parent 25fd019 commit e752743
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/util/fan/JsonOutStream.fan
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ class JsonOutStream : OutStream
**
This writeJson(Obj? obj)
{
if (obj is Str) writeJsonStr(obj)
else if (obj is Num) writeJsonNum(obj)
else if (obj is Bool) writeJsonBool(obj)
else if (obj is Map) writeJsonMap(obj)
else if (obj is List) writeJsonList(obj)
else if (obj == null) writeJsonNull
if (obj is Str) writeJsonStr(obj)
else if (obj is Map) writeJsonMap(obj)
else if (obj is List) writeJsonList(obj)
else if (obj is Float) writeJsonFloat(obj)
else if (obj is Num) writeJsonNum(obj)
else if (obj is Bool) writeJsonBool(obj)
else if (obj == null) writeJsonNull
else writeJsonObj(obj)
return this
}
Expand Down Expand Up @@ -159,6 +160,15 @@ class JsonOutStream : OutStream
writeChar(JsonToken.quote)
}

private Void writeJsonFloat(Float float)
{
// check for unsupported literals
if (float.isNaN || float == Float.posInf || float == Float.negInf)
throw IOErr("Unsupported JSON float literal: '${float}'")

print(float)
}

private Void writeJsonNum(Num num)
{
print(num)
Expand Down
6 changes: 5 additions & 1 deletion src/util/test/JsonTest.fan
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class JsonTest : Test
verifyWrite(["key":"val"], Str<|{"key":"val"}|>)
verifyWrite(["key":"val\\\"ue"], Str<|{"key":"val\\\"ue"}|>)


// simples
verifyWrite(5min, Str<|"5min"|>)
verifyWrite(`/some/uri/`, Str<|"/some/uri/"|>)
Expand All @@ -128,6 +127,11 @@ class JsonTest : Test
verifyWrite(SerialA(),
Str<|{"b":true,"i":7,"f":5.0,"s":"string\n","ints":[1,2,3]}|>)

// invalid float literals
verifyErr(IOErr#) { verifyWrite(Float.nan, "") }
verifyErr(IOErr#) { verifyWrite(Float.posInf, "") }
verifyErr(IOErr#) { verifyWrite(Float.negInf, "") }

// errors
verifyErr(IOErr#) { verifyWrite(Buf(), "") }
verifyErr(IOErr#) { verifyWrite(Str#.pod, "") }
Expand Down

0 comments on commit e752743

Please sign in to comment.