From 9b2fa4511fad4ca8617d5c6f65734b8f41e6ff63 Mon Sep 17 00:00:00 2001 From: kanjoe24 <165808281+kanjoe24@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:59:54 +0000 Subject: [PATCH] Fixing the negative scenarios in ut_kvp_getListCount() #42 --- .vscode/launch.json | 6 +++--- src/ut_kvp.c | 16 +++++++++------- tests/src/ut_test_kvp.c | 3 +++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f25126c..a18bfce 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,9 +8,9 @@ "args": [], "stopAtEntry": false, "externalConsole": false, - "cwd": "${workspaceFolder}/tests/bin/", - "program": "${workspaceFolder}/tests/bin/ut_control_test", - "environment": [ {"name": "LD_LIBRARY_PATH", "value":"${workspaceFolder}/tests/bin/"} ], + "cwd": "${workspaceFolder}/tests/build/bin/", + "program": "${workspaceFolder}/tests/build/bin/ut_control_test", + "environment": [ {"name": "LD_LIBRARY_PATH", "value":"${workspaceFolder}/tests/build/bin/"} ], "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/src/ut_kvp.c b/src/ut_kvp.c index 9a1aa0d..814cb09 100644 --- a/src/ut_kvp.c +++ b/src/ut_kvp.c @@ -597,14 +597,16 @@ uint32_t ut_kvp_getListCount( ut_kvp_instance_t *pInstance, const char *pszKey) return 0; } - if (fy_node_is_sequence(node)) + if (!fy_node_is_sequence(node)) { - count = fy_node_sequence_item_count(node); - if (count == -1) - { - UT_LOG_ERROR("fy_node_sequence_item_count() returned error\n "); - return 0; - } + return 0; // Early return on negative check + } + + count = fy_node_sequence_item_count(node); + if (count == -1) + { + UT_LOG_ERROR("fy_node_sequence_item_count() returned error\n"); + return 0; } return count; diff --git a/tests/src/ut_test_kvp.c b/tests/src/ut_test_kvp.c index be6e888..4cdf438 100644 --- a/tests/src/ut_test_kvp.c +++ b/tests/src/ut_test_kvp.c @@ -305,6 +305,9 @@ void test_ut_kvp_list(void) uint32_t result; int count; + count = ut_kvp_getListCount(gpMainTestInstance, "decodeTest/checkFloat"); + UT_ASSERT( count == 0 ); + count = ut_kvp_getListCount(gpMainTestInstance, "decodeTest/checkStringList"); UT_ASSERT( count == 3 );