From d4c394b72add61e37817851327eacb2b9724bb5a Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Fri, 27 Sep 2019 14:12:48 -0700 Subject: [PATCH] GH-606: Output nulls into json data if a field isn't set --- src/Val.cc | 6 +++++- src/threading/formatters/JSON.cc | 4 ++++ .../scripts.base.frameworks.logging.ascii-json/ssh.log | 2 +- testing/btest/Baseline/scripts.base.utils.json/output | 1 + .../btest/scripts/base/frameworks/logging/ascii-json.zeek | 6 ++++++ testing/btest/scripts/base/utils/json.test | 4 ++++ 6 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Val.cc b/src/Val.cc index 6a27dbfbc7..a5f838dafd 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -458,6 +458,10 @@ TableVal* Val::GetRecordFields() // This is a static method in this file to avoid including json.hpp in Val.h since it's huge. static ZeekJson BuildJSON(Val* val, bool only_loggable=false, RE_Matcher* re=new RE_Matcher("^_")) { + // If the value wasn't set, return a nullptr. This will get turned into a 'null' in the json output. + if ( ! val ) + return nullptr; + ZeekJson j; BroType* type = val->Type(); switch ( type->Tag() ) @@ -2869,7 +2873,7 @@ int VectorVal::AddTo(Val* val, int /* is_first_init */) const Val* VectorVal::Lookup(unsigned int index) const { if ( index >= val.vector_val->size() ) - return 0; + return nullptr; return (*val.vector_val)[index]; } diff --git a/src/threading/formatters/JSON.cc b/src/threading/formatters/JSON.cc index 58b1ffd779..b5d9c9904b 100644 --- a/src/threading/formatters/JSON.cc +++ b/src/threading/formatters/JSON.cc @@ -73,6 +73,10 @@ threading::Value* JSON::ParseValue(const string& s, const string& name, TypeTag ZeekJson JSON::BuildJSON(Value* val, const string& name) const { + // If the value wasn't set, return a nullptr. This will get turned into a 'null' in the json output. + if ( ! val->present ) + return nullptr; + ZeekJson j; switch ( val->type ) { diff --git a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-json/ssh.log b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-json/ssh.log index 52c5e4856c..99f3925b2e 100644 --- a/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-json/ssh.log +++ b/testing/btest/Baseline/scripts.base.frameworks.logging.ascii-json/ssh.log @@ -1 +1 @@ -{"b":true,"i":-42,"e":"SSH::LOG","c":21,"p":123,"sn":"10.0.0.0/24","a":"1.2.3.4","d":3.14,"t":1215620010.54321,"iv":100.0,"s":"hurz","sc":[2,4,1,3],"ss":["BB","AA","CC"],"se":[],"vc":[10,20,30],"ve":[],"f":"SSH::foo\n{ \nif (0 < SSH::i) \n\treturn (Foo);\nelse\n\treturn (Bar);\n\n}"} +{"b":true,"i":-42,"e":"SSH::LOG","c":21,"p":123,"sn":"10.0.0.0/24","a":"1.2.3.4","d":3.14,"t":1215620010.54321,"iv":100.0,"s":"hurz","sc":[2,4,1,3],"ss":["BB","AA","CC"],"se":[],"vc":[10,20,30],"ve":[],"vn":[0,null,2],"f":"SSH::foo\n{ \nif (0 < SSH::i) \n\treturn (Foo);\nelse\n\treturn (Bar);\n\n}"} diff --git a/testing/btest/Baseline/scripts.base.utils.json/output b/testing/btest/Baseline/scripts.base.utils.json/output index 2d2e56253f..e41f12eaec 100644 --- a/testing/btest/Baseline/scripts.base.utils.json/output +++ b/testing/btest/Baseline/scripts.base.utils.json/output @@ -28,6 +28,7 @@ true ["1.2.3.4"] [[true,false]] [{"s":"test"}] +[0,null,2] [] [2,1] ["1.2.3.4"] diff --git a/testing/btest/scripts/base/frameworks/logging/ascii-json.zeek b/testing/btest/scripts/base/frameworks/logging/ascii-json.zeek index ab88225d97..6991e1336a 100644 --- a/testing/btest/scripts/base/frameworks/logging/ascii-json.zeek +++ b/testing/btest/scripts/base/frameworks/logging/ascii-json.zeek @@ -28,6 +28,7 @@ export { se: set[string]; vc: vector of count; ve: vector of string; + vn: vector of count; f: function(i: count) : string; } &log; } @@ -47,6 +48,10 @@ event zeek_init() local empty_set: set[string]; local empty_vector: vector of string; + local vector_with_null: vector of count; + vector_with_null[0] = 0; + vector_with_null[2] = 2; + Log::write(SSH::LOG, [ $b=T, $i=-42, @@ -64,6 +69,7 @@ event zeek_init() $se=empty_set, $vc=vector(10, 20, 30), $ve=empty_vector, + $vn=vector_with_null, $f=foo ]); } diff --git a/testing/btest/scripts/base/utils/json.test b/testing/btest/scripts/base/utils/json.test index 6e7854b744..07d7789b67 100644 --- a/testing/btest/scripts/base/utils/json.test +++ b/testing/btest/scripts/base/utils/json.test @@ -93,11 +93,15 @@ event zeek_init() local ve3: vector of addr = vector(1.2.3.4); local ve4: vector of set[bool] = vector(set(T, F)); local ve5: vector of myrec1 = vector(myrec1($s="test", $c=2)); + local ve6: vector of count; + ve6[0] = 0; + ve6[2] = 2; print to_json(ve1); print to_json(ve2); print to_json(ve3); print to_json(ve4); print to_json(ve5, T); + print to_json(ve6); # Sets local st1: set[count] = set();