tlv_eeprom: add tlv_copy_string()
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Fri, 14 Nov 2025 16:30:47 +0000 (11:30 -0500)
committerHugo Villeneuve <hvilleneuve@dimonoff.com>
Mon, 17 Nov 2025 15:50:01 +0000 (10:50 -0500)
In preparation for UUID values...

Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
src/tlv_eeprom.c

index fdb8349..25786fd 100644 (file)
@@ -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);