From f136211bad2d0b78e31ce032282c04c1249e10f3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 6 Apr 2022 10:21:36 +0000 Subject: [PATCH] fix TDB encode and decode bug --- source/libs/tdb/src/inc/tdbUtil.h | 7 +++++-- source/libs/tdb/test/CMakeLists.txt | 6 +++++- source/libs/tdb/test/tdbUtilTest.cpp | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 source/libs/tdb/test/tdbUtilTest.cpp diff --git a/source/libs/tdb/src/inc/tdbUtil.h b/source/libs/tdb/src/inc/tdbUtil.h index 042026d0a4..7085e71567 100644 --- a/source/libs/tdb/src/inc/tdbUtil.h +++ b/source/libs/tdb/src/inc/tdbUtil.h @@ -84,15 +84,18 @@ static inline int tdbPutVarInt(u8 *p, int v) { static inline int tdbGetVarInt(const u8 *p, int *v) { int n = 0; int tv = 0; + int t; for (;;) { if (p[n] <= 0x7f) { - tv = (tv << 7) | p[n]; + t = p[n]; + tv |= (t << (7 * n)); n++; break; } - tv = (tv << 7) | (p[n] & 0x7f); + t = p[n] & 0x7f; + tv |= (t << (7 * n)); n++; } diff --git a/source/libs/tdb/test/CMakeLists.txt b/source/libs/tdb/test/CMakeLists.txt index 2d77c1f4e9..b2c8aaf9bc 100644 --- a/source/libs/tdb/test/CMakeLists.txt +++ b/source/libs/tdb/test/CMakeLists.txt @@ -1,3 +1,7 @@ # tdbTest add_executable(tdbTest "tdbTest.cpp") -target_link_libraries(tdbTest tdb gtest gtest_main) \ No newline at end of file +target_link_libraries(tdbTest tdb gtest gtest_main) + +# tdbUtilTest +add_executable(tdbUtilTest "tdbUtilTest.cpp") +target_link_libraries(tdbUtilTest tdb gtest gtest_main) \ No newline at end of file diff --git a/source/libs/tdb/test/tdbUtilTest.cpp b/source/libs/tdb/test/tdbUtilTest.cpp new file mode 100644 index 0000000000..5fd45daa3e --- /dev/null +++ b/source/libs/tdb/test/tdbUtilTest.cpp @@ -0,0 +1,20 @@ +#include + +#include "tdbInt.h" + +#include + +TEST(tdb_util_test, simple_test) { + int vEncode = 5000; + int vDecode; + int nEncode; + int nDecode; + u8 buffer[128]; + + nEncode = tdbPutVarInt(buffer, vEncode); + + nDecode = tdbGetVarInt(buffer, &vDecode); + + GTEST_ASSERT_EQ(nEncode, nDecode); + GTEST_ASSERT_EQ(vEncode, vDecode); +} \ No newline at end of file -- GitLab