Skip to content

Commit 712e013

Browse files
authored
String blob bug (#880) (#881)
* fixed segmentation fault bug in _RAI_TensorParseStringsBlob * added test for future similar errors (too many elements in BLOB)
1 parent 482ead2 commit 712e013

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/redis_ai_objects/tensor.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,12 @@ static int _RAI_TensorParseStringsBlob(const char *tensor_blob, size_t blob_len,
147147
// if we encounter null-character, we set the next element offset to the next position
148148
for (size_t i = 0; i < blob_len - 1; i++) {
149149
if (tensor_blob[i] == '\0') {
150-
offsets[elements_counter++] = i + 1;
150+
if (elements_counter < tensor_len) {
151+
offsets[elements_counter++] = i + 1;
152+
} else {
153+
elements_counter++;
154+
break; // No need to continue if tensor_blob contains more elements than expected
155+
}
151156
}
152157
}
153158
if (tensor_blob[blob_len - 1] != '\0' || elements_counter != tensor_len) {

tests/flow/tests_common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ def test_common_tensorset_error_replies(env):
115115
check_error_message(env, con, "Number of string elements in data blob does not match tensor length",
116116
'AI.TENSORSET', 'z{0}', 'STRING', 2, 'BLOB', 'single c-string\0')
117117

118+
# ERR in string tensor blob - number of strings is not compatible with tensor meta data
119+
check_error_message(env, con, "Number of string elements in data blob does not match tensor length",
120+
'AI.TENSORSET', 'z{0}', 'STRING', 2, 'BLOB', 'one\0two\0three\0')
121+
118122
# ERR in string tensor blob - number of strings is not compatible with tensor meta data
119123
check_error_message(env, con, "Number of string elements in data blob does not match tensor length",
120124
'AI.TENSORSET', 'z{0}', 'STRING', 2, 'BLOB', 'C-string\0followed by a non C-string')

0 commit comments

Comments
 (0)