Skip to content

Commit 1abebdd

Browse files
authored
Few fixes for string type input (#136)
1. If the input string is mentioned as hex, take the hex representation otherwise take the input bytes and convert them to ASCII for further processing. 2. Pass the length of input bytes/string. Use this length to copy and process the string bytes. This will mitigate the risk of buffer overflows, memory corruption or information leakage and other string issues which might occur while processing string without knowning it's length. The fix will also enable to pass and have NULL char and other special chars as part of string. Signed-off-by: Ruchit Gupta <[email protected]>
1 parent 80766c9 commit 1abebdd

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

include/tdi/common/c_frontend/tdi_table_data.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,21 @@ tdi_status_t tdi_data_field_set_string(tdi_table_data_hdl *data_hdl,
174174
const tdi_id_t field_id,
175175
const char *val);
176176

177+
/**
178+
* @brief Set value based on size. Valid only on fields with string type
179+
*
180+
* @param[in] data_hdl Data object handle
181+
* @param[in] field_id Field ID
182+
* @param[in] val String value
183+
* @param[in] s Size of string value
184+
*
185+
* @return Status of the API call
186+
*/
187+
tdi_status_t tdi_data_field_set_string_with_size(tdi_table_data_hdl *data_hdl,
188+
const tdi_id_t field_id,
189+
const char *val,
190+
const size_t s);
191+
177192
/**
178193
* @brief Get value. Only valid on fields of size <= 64 bits
179194
*

include/tdi/common/tdi_table_data.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,26 @@ class TableData {
196196
* @brief Set value. Valid only on fields with string type
197197
*
198198
* @param[in] field_id Field ID
199-
* @param[in] value String value
199+
* @param[in] str String value
200200
*
201201
* @return Status of the API call
202202
*/
203203
virtual tdi_status_t setValue(const tdi_id_t &field_id,
204204
const std::string &str);
205205

206+
/**
207+
* @brief Set value based on size. Valid only on fields with string type
208+
*
209+
* @param[in] field_id Field ID
210+
* @param[in] str String value
211+
* @param[in] s Size of string value
212+
*
213+
* @return Status of the API call
214+
*/
215+
virtual tdi_status_t setValue(const tdi_id_t &field_id,
216+
const std::string &str,
217+
const size_t &size);
218+
206219
/** @} */ // End of group Set APIs
207220

208221
/**

src/c_frontend/tdi_table_data_c.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ tdi_status_t tdi_data_field_set_string(tdi_table_data_hdl *data_hdl,
124124
return data_field->setValue(field_id, str_val);
125125
}
126126

127+
tdi_status_t tdi_data_field_set_string_with_size(tdi_table_data_hdl *data_hdl,
128+
const tdi_id_t field_id,
129+
const char *val,
130+
const size_t s) {
131+
auto data_field = reinterpret_cast<tdi::TableData *>(data_hdl);
132+
std::string str_val = {0};
133+
if (s > 0) {
134+
/* we don't append directly as the data/value would be appended to NULL,
135+
* instead, we want to assign the same value to str_val which val
136+
* holds. */
137+
str_val = val[0];
138+
for (int i = 1; i < (int)s; i++) {
139+
str_val += val[i];
140+
}
141+
}
142+
return data_field->setValue(field_id, str_val, s);
143+
}
144+
127145
tdi_status_t tdi_data_field_get_value(const tdi_table_data_hdl *data_hdl,
128146
const tdi_id_t field_id,
129147
uint64_t *val) {

src/tdi_table_data.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ tdi_status_t TableData::setValue(const tdi_id_t & /*field_id*/,
8282
return TDI_NOT_SUPPORTED;
8383
}
8484

85+
tdi_status_t TableData::setValue(const tdi_id_t & /*field_id*/,
86+
const std::string & /*str*/,
87+
const size_t & /*size*/) {
88+
LOG_ERROR("%s:%d Not supported", __func__, __LINE__);
89+
return TDI_NOT_SUPPORTED;
90+
}
91+
8592
tdi_status_t TableData::getValue(const tdi_id_t & /*field_id*/,
8693
uint64_t * /*value*/) const {
8794
LOG_ERROR("%s:%d Not supported", __func__, __LINE__);

tdi_python/tdiTable.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,13 @@ def _set_data_field(self, content, data_handle, data_fields):
11091109
value = c_bool(content[name])
11101110
sts = self._cintf.get_driver().tdi_data_field_set_bool(data_handle, info.id, value)
11111111
if self.data_type_cls.data_type_str(info.data_type) == "STRING":
1112-
value = c_char_p(content[name].encode('ascii'))
1113-
sts = self._cintf.get_driver().tdi_data_field_set_string(data_handle, info.id, value)
1112+
string = content[name]
1113+
if string[0:2] == "0x":
1114+
bytestr = bytes.fromhex(string[2:])
1115+
else:
1116+
bytestr = string.encode('ascii')
1117+
value = c_char_p(bytestr)
1118+
sts = self._cintf.get_driver().tdi_data_field_set_string_with_size(data_handle, info.id, value, len(bytestr))
11141119
"""
11151120
if self.data_type_cls.data_type_str(info.data_type) == "CONTAINER":
11161121
cont_list_len = len(content[name])

0 commit comments

Comments
 (0)