From 4f226b72b0702d69baa830eb0adea44a6e1ed279 Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Thu, 6 Nov 2025 11:17:33 -0500 Subject: [PATCH] tlv_eeprom: add decode_tlv_value() Signed-off-by: Hugo Villeneuve --- src/tlv_eeprom.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/tlv_eeprom.c b/src/tlv_eeprom.c index 135803a..bea6209 100644 --- a/src/tlv_eeprom.c +++ b/src/tlv_eeprom.c @@ -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); } -- 2.20.1