tlv_eeprom: add decode_tlv_value()
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Thu, 6 Nov 2025 16:17:33 +0000 (11:17 -0500)
committerHugo Villeneuve <hvilleneuve@dimonoff.com>
Mon, 17 Nov 2025 15:36:11 +0000 (10:36 -0500)
Signed-off-by: Hugo Villeneuve <hvilleneuve@dimonoff.com>
src/tlv_eeprom.c

index 135803a..bea6209 100644 (file)
@@ -291,13 +291,14 @@ static inline const char *tlv_type2name(u8 type)
  */
 #define DECODE_VALUE_MAX    ((5 * TLV_VALUE_MAX_LEN) + 1)
 
-static void decode_tlv(struct tlvinfo_tlv *tlv)
+static void decode_tlv_value(struct tlvinfo_tlv *tlv, char *value, int value_len)
 {
-       char name[DECODE_NAME_MAX];
-       char value[DECODE_VALUE_MAX];
        int i;
 
-       strncpy(name, tlv_type2name(tlv->type), DECODE_NAME_MAX);
+       if (value_len < tlv->length) {
+               printf("ERROR: destination buffer too short for key length\n");
+               return;
+       }
 
        switch (tlv->type) {
        case TLV_CODE_PRODUCT_NAME:
@@ -317,8 +318,8 @@ static void decode_tlv(struct tlvinfo_tlv *tlv)
                break;
        case TLV_CODE_MAC_BASE:
                sprintf(value, "%02X:%02X:%02X:%02X:%02X:%02X",
-                       tlv->value[0], tlv->value[1], tlv->value[2],
-                       tlv->value[3], tlv->value[4], tlv->value[5]);
+                        tlv->value[0], tlv->value[1], tlv->value[2],
+                        tlv->value[3], tlv->value[4], tlv->value[5]);
                break;
        case TLV_CODE_DEVICE_VERSION:
                sprintf(value, "%u", tlv->value[0]);
@@ -346,8 +347,18 @@ static void decode_tlv(struct tlvinfo_tlv *tlv)
                }
                break;
        }
+}
 
+static void decode_tlv(struct tlvinfo_tlv *tlv)
+{
+       char name[DECODE_NAME_MAX];
+       char value[DECODE_VALUE_MAX];
+
+       strncpy(name, tlv_type2name(tlv->type), DECODE_NAME_MAX);
        name[DECODE_NAME_MAX - 1] = 0;
+
+       decode_tlv_value(tlv, value, sizeof(value));
+
        printf("%-20s 0x%02X %3d %s\n", name, tlv->type, tlv->length, value);
 }