Skip to content

Commit

Permalink
Few fixes for string type input
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ruchitg committed Jul 17, 2024
1 parent 80766c9 commit 891fc4b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion include/tdi/common/c_frontend/tdi_table_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ tdi_status_t tdi_data_field_set_bool(tdi_table_data_hdl *data_hdl,
*/
tdi_status_t tdi_data_field_set_string(tdi_table_data_hdl *data_hdl,
const tdi_id_t field_id,
const char *val);
const char *val,
const size_t s);

/**
* @brief Get value. Only valid on fields of size <= 64 bits
Expand Down
4 changes: 3 additions & 1 deletion include/tdi/common/tdi_table_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ class TableData {
*
* @param[in] field_id Field ID
* @param[in] value String value
* @param[in] length of the input string
*
* @return Status of the API call
*/
virtual tdi_status_t setValue(const tdi_id_t &field_id,
const std::string &str);
const std::string &str,
const size_t &size);

/** @} */ // End of group Set APIs

Expand Down
13 changes: 10 additions & 3 deletions src/c_frontend/tdi_table_data_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,17 @@ tdi_status_t tdi_data_field_set_bool(tdi_table_data_hdl *data_hdl,

tdi_status_t tdi_data_field_set_string(tdi_table_data_hdl *data_hdl,
const tdi_id_t field_id,
const char *val) {
const char *val,
const size_t s) {
auto data_field = reinterpret_cast<tdi::TableData *>(data_hdl);
const std::string str_val(val);
return data_field->setValue(field_id, str_val);
std::string str_val = {0};
if ((int)s > 0) {
str_val = val[0];
for (int i = 1; i < (int)s; i++) {
str_val += val[i];
}
}
return data_field->setValue(field_id, str_val, s);
}

tdi_status_t tdi_data_field_get_value(const tdi_table_data_hdl *data_hdl,
Expand Down
3 changes: 2 additions & 1 deletion src/tdi_table_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ tdi_status_t TableData::setValue(
}

tdi_status_t TableData::setValue(const tdi_id_t & /*field_id*/,
const std::string & /*str*/) {
const std::string & /*str*/,
const size_t & /*size*/) {
LOG_ERROR("%s:%d Not supported", __func__, __LINE__);
return TDI_NOT_SUPPORTED;
}
Expand Down
9 changes: 7 additions & 2 deletions tdi_python/tdiTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,8 +1109,13 @@ def _set_data_field(self, content, data_handle, data_fields):
value = c_bool(content[name])
sts = self._cintf.get_driver().tdi_data_field_set_bool(data_handle, info.id, value)
if self.data_type_cls.data_type_str(info.data_type) == "STRING":
value = c_char_p(content[name].encode('ascii'))
sts = self._cintf.get_driver().tdi_data_field_set_string(data_handle, info.id, value)
string = content[name]
if string[0:2] =="0x":
bytestr = bytes.fromhex(string[2:])
else:
bytestr = string.encode('ascii')
value = c_char_p(bytestr)
sts = self._cintf.get_driver().tdi_data_field_set_string(data_handle, info.id, value, len(bytestr))
"""
if self.data_type_cls.data_type_str(info.data_type) == "CONTAINER":
cont_list_len = len(content[name])
Expand Down

0 comments on commit 891fc4b

Please sign in to comment.