Skip to content

Commit

Permalink
[action] [PR:836] add support for binary data read for Table::get() (s…
Browse files Browse the repository at this point in the history
…onic-net#836)

In the current implementation, the data from redis is passed directly
to the std::string(char*) constructor which truncates the data on the null
byte.
To support binary data that can contain null bytes, the redis reply
*str* is passed along with its *len* into the std::string(char*,size_t)
constructor that supports the input size.

Co-authored-by: Yakiv Huryk <[email protected]>
Co-authored-by: Ze Gan <[email protected]>
  • Loading branch information
3 people authored May 30, 2024
1 parent 9d70e50 commit 96ad341
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool Table::get(const string &key, vector<FieldValueTuple> &values)
for (unsigned int i = 0; i < reply->elements; i += 2)
{
values.emplace_back(stripSpecialSym(reply->element[i]->str),
reply->element[i + 1]->str);
string(reply->element[i + 1]->str, reply->element[i + 1]->len));
}

return true;
Expand Down
29 changes: 29 additions & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "common/table.h"
#include "common/dbinterface.h"
#include "common/sonicv2connector.h"
#include "common/redisutility.h"

using namespace std;
using namespace swss;
Expand Down Expand Up @@ -845,6 +846,34 @@ TEST(Table, ttl_test)
cout << "Done." << endl;
}

TEST(Table, binary_data_get)
{
DBConnector db("TEST_DB", 0, true);
Table table(&db, "binary_data");

const char* bindata1 = "\x11\x00\x22\x33\x44";
const char* bindata2 = "\x11\x22\x33\x00\x44";
auto v1 = std::string(bindata1, sizeof(bindata1));
auto v2 = std::string(bindata2, sizeof(bindata2));
vector<FieldValueTuple> values_set = {
{"f1", v1},
{"f2", v2},
};

table.set("k1", values_set);

vector<FieldValueTuple> values_get;
EXPECT_TRUE(table.get("k1", values_get));

auto f1 = swss::fvsGetValue(values_get, "f1");
auto f2 = swss::fvsGetValue(values_get, "f2");
EXPECT_TRUE(f1);
EXPECT_TRUE(f2);

EXPECT_EQ(*f1, v1);
EXPECT_EQ(*f2, v2);
}

TEST(ProducerConsumer, Prefix)
{
std::string tableName = "tableName";
Expand Down

0 comments on commit 96ad341

Please sign in to comment.