From 8f44fad2dbcdee3bbb66860a378794dfbfeab068 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Fri, 14 Nov 2025 11:30:47 -0500 Subject: [PATCH] tlv_eeprom: add tlv_copy_string() In preparation for UUID values... Signed-off-by: Hugo Villeneuve --- src/tlv_eeprom.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/tlv_eeprom.c b/src/tlv_eeprom.c index fdb8349..25786fd 100644 --- a/src/tlv_eeprom.c +++ b/src/tlv_eeprom.c @@ -520,13 +520,33 @@ bool tlvinfo_delete_tlv(u8 *eeprom, u8 code) return true; } +#define MAX_TLV_VALUE_LEN 256 + +/** + * Copy a TLV string. + */ +static int tlv_copy_string(char *buf, const char *string, int *new_tlv_len) +{ + if (!string) { + printf("ERROR: NULL string passed in\n"); + return -1; + } + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-truncation" + strncpy(buf, string, MAX_TLV_VALUE_LEN); +#pragma GCC diagnostic pop + *new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(string)); + + return 0; +} + /** * tlvinfo_add_tlv * * This function adds a TLV to the EEPROM, converting the value (a string) to * the format in which it will be stored in the EEPROM. */ -#define MAX_TLV_VALUE_LEN 256 bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval) { struct tlvinfo_header *eeprom_hdr = to_header(eeprom); @@ -558,11 +578,7 @@ bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval) case TLV_CODE_VENDOR_NAME: case TLV_CODE_DIAG_VERSION: case TLV_CODE_SERVICE_TAG: -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" - strncpy(data, strval, MAX_TLV_VALUE_LEN); -#pragma GCC diagnostic pop - new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(strval)); + tlv_copy_string(data, strval, &new_tlv_len); break; case TLV_CODE_DEVICE_VERSION: value = simple_strtoul(strval, NULL, 0); -- 2.20.1