Skip to content

Commit ba49a3d

Browse files
Address PR feedback
- Added code comment - Renamed hook: bytes2hexstring -> bytes2hex - Improved test case - Use safe length to avoid potential out-of-bounds error
1 parent bffdfa4 commit ba49a3d

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

runtime/strings/bytes.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,36 @@ hook_BYTES_int2bytes(SortInt len, SortInt i, SortEndianness endianness_ptr) {
148148
return result;
149149
}
150150

151-
string *bytes2string(string *b, size_t len) {
152-
auto *result = static_cast<string *>(kore_alloc_token(sizeof(string) + len));
153-
memcpy(result->data, b->data, len);
154-
init_with_len(result, len);
155-
return result;
156-
}
157-
158151
SortString hook_BYTES_bytes2string(SortBytes b) {
159-
return bytes2string(b, len(b));
152+
auto len_b = len(b);
153+
auto *result = static_cast<string *>(kore_alloc_token(sizeof(string) + len_b));
154+
memcpy(result->data, b->data, len_b);
155+
init_with_len(result, len_b);
156+
return result;
160157
}
161158

162159
SortBytes hook_BYTES_string2bytes(SortString s) {
163160
return hook_BYTES_bytes2string(s);
164161
}
165162

166-
string *bytes2hexstring(string *b, size_t len) {
163+
// Convert a bytes array to its hexadecimal string representation
164+
// For example, the bytes array b'\x01\xef' becomes the string "01ef"
165+
// syntax String ::= Bytes2Hex( Bytes )
166+
SortString hook_BYTES_bytes2hex(SortBytes b) {
167167
static const std::array<char, 16> hexchars
168168
= {'0', '1', '2', '3', '4', '5', '6', '7',
169169
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
170+
auto len_b = len(b);
170171
auto *result
171-
= static_cast<string *>(kore_alloc_token(sizeof(string) + len * 2));
172-
for (size_t i = 0; i < len; i++) {
172+
= static_cast<string *>(kore_alloc_token(sizeof(string) + len_b * 2));
173+
for (size_t i = 0; i < len_b; i++) {
173174
result->data[i * 2] = hexchars.at((b->data[i] >> 4) & 0xf);
174175
result->data[i * 2 + 1] = hexchars.at(b->data[i] & 0xf);
175176
}
176-
init_with_len(result, len * 2);
177+
init_with_len(result, len_b * 2);
177178
return result;
178179
}
179180

180-
SortString hook_BYTES_bytes2hexstring(SortBytes b) {
181-
return bytes2hexstring(b, len(b));
182-
}
183-
184181
SortBytes hook_BYTES_substr(SortBytes input, SortInt start, SortInt end) {
185182
uint64_t ustart = GET_UI(start);
186183
uint64_t uend = GET_UI(end);

unittests/runtime-strings/bytestest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mpz_ptr
2020
hook_BYTES_bytes2int(string *b, uint64_t endianness, uint64_t signedness);
2121
string *hook_BYTES_int2bytes(mpz_t len, mpz_t i, uint64_t endianness);
2222
string *hook_BYTES_bytes2string(string *b);
23-
string *hook_BYTES_bytes2hexstring(string *b);
23+
string *hook_BYTES_bytes2hex(string *b);
2424
string *hook_BYTES_string2bytes(string *s);
2525
string *hook_BYTES_substr(string *b, mpz_t start, mpz_t end);
2626
string *hook_BYTES_replaceAt(string *b, mpz_t start, string *b2);
@@ -172,17 +172,17 @@ BOOST_AUTO_TEST_CASE(bytes2string) {
172172
BOOST_CHECK_EQUAL(0, memcmp(_1234->data, "1234", 4));
173173
}
174174

175-
BOOST_AUTO_TEST_CASE(bytes2hexstring) {
175+
BOOST_AUTO_TEST_CASE(bytes2hex) {
176176
auto empty = make_string("");
177-
auto res = hook_BYTES_bytes2hexstring(empty);
177+
auto res = hook_BYTES_bytes2hex(empty);
178178
BOOST_CHECK(res != empty);
179179
BOOST_CHECK_EQUAL(len(res), 0);
180180

181-
auto _00ff = make_string("\x00\xff", 2);
182-
res = hook_BYTES_bytes2hexstring(_00ff);
183-
BOOST_CHECK(res != _00ff);
181+
auto _01ef = make_string("\x01\xef", 2);
182+
res = hook_BYTES_bytes2hex(_01ef);
183+
BOOST_CHECK(res != _01ef);
184184
BOOST_CHECK_EQUAL(len(res), 4);
185-
BOOST_CHECK_EQUAL(0, memcmp(res->data, "00ff", 4));
185+
BOOST_CHECK_EQUAL(0, memcmp(res->data, "01ef", 4));
186186
}
187187

188188
BOOST_AUTO_TEST_CASE(string2bytes) {

0 commit comments

Comments
 (0)