Skip to content

Commit 965631c

Browse files
committed
JSON converter: fix invalid hexa string if an IPFIX field has zero size and FDS_CD2J_OCTETS_NOINT is enabled
Previous (invalid) output: "0x", new output: null
1 parent 52f37c2 commit 965631c

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/converters/json.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,17 @@ to_uint(struct context *buffer, const struct fds_drec_field *field)
227227
static int
228228
to_octet(struct context *buffer, const struct fds_drec_field *field)
229229
{
230-
if (field->size <= 8 && (buffer->flags & FDS_CD2J_OCTETS_NOINT) == 0) {
230+
if (field->size == 0) {
231+
// Empty field cannot be converted -> null
232+
return FDS_ERR_ARG;
233+
}
234+
235+
if ((buffer->flags & FDS_CD2J_OCTETS_NOINT) == 0 && field->size <= 8) {
231236
// Print as unsigned integer
232237
return to_uint(buffer, field);
233238
}
234239

240+
// Print as hexadecimal number
235241
const size_t mem_req = (2 * field->size) + 5U; // "0x" + 2 chars per byte + 2x "\"" + "\0"
236242

237243
int ret_code = buffer_reserve(buffer, buffer_used(buffer) + mem_req);

tests/unit_tests/converters/json.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ TEST_F(Drec_invalid, nullInMulti)
10421042
free(buff);
10431043
}
10441044

1045-
// Test fot string with size 0
1045+
// Test for string with size 0
10461046
TEST_F(Drec_invalid, zeroSizeStr)
10471047
{
10481048
constexpr size_t BSIZE = 2U;
@@ -1063,6 +1063,22 @@ TEST_F(Drec_invalid, zeroSizeStr)
10631063
free(buff);
10641064
}
10651065

1066+
// Test for octetArray with size 0 and disabled integer conversion
1067+
TEST_F(Drec_invalid, zeroSizeOctetArray)
1068+
{
1069+
constexpr size_t BSIZE = 2U;
1070+
char* buff = (char*) malloc(BSIZE);
1071+
uint32_t flags = FDS_CD2J_ALLOW_REALLOC | FDS_CD2J_OCTETS_NOINT; // do not use int conversion!
1072+
size_t buff_size = BSIZE;
1073+
1074+
int rc = fds_drec2json(&m_drec, flags, m_iemgr.get(), &buff, &buff_size);
1075+
ASSERT_GT(rc, 0);
1076+
EXPECT_EQ(size_t(rc), strlen(buff));
1077+
Config cfg = parse_string(buff, JSON, "drec2json");
1078+
EXPECT_TRUE(cfg["en0:id32000"].is_null());
1079+
free(buff);
1080+
}
1081+
10661082
// Testing return of error code FDS_ERR_BUFFER
10671083
TEST_F(Drec_invalid, errorBuff)
10681084
{

0 commit comments

Comments
 (0)