From 5f0188bb8ce28e4dd8a21987928fe7473224d7d2 Mon Sep 17 00:00:00 2001 From: m30063213 Date: Mon, 23 Dec 2024 15:30:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E9=95=BF=E5=BA=A6=20Signed-o?= =?UTF-8?q?ff-by:=20maqianli=20<15735184237@163.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/tlv_utils/include/tlv_utils.h | 2 +- utils/tlv_utils/src/tlv_utils.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/utils/tlv_utils/include/tlv_utils.h b/utils/tlv_utils/include/tlv_utils.h index 389a5248c..299987a64 100644 --- a/utils/tlv_utils/include/tlv_utils.h +++ b/utils/tlv_utils/include/tlv_utils.h @@ -72,7 +72,7 @@ public: private: static uint8_t *GetNextTlv(const uint8_t *buffer); static uint8_t *ParseTlv(const uint8_t *buffer, TlvCommon *tlv, const uint8_t *boundary, uint32_t *retCode); - static uint8_t *AppendTlv(uint8_t *buffer, const TlvCommon *tlv, const uint8_t *boundary, uint32_t *retCode); + static uint8_t *AppendTlv(uint8_t *buffer, const TlvCommon *tlv, const uint32_t maxBuffSize, uint32_t *retCode); static uint32_t Serialize(const TlvCommon *tlv, uint32_t tlvCount, uint8_t *buff, uint32_t maxBuffSize, uint32_t *buffSize); static uint32_t Deserialize(const uint8_t *buff, uint32_t buffSize, TlvCommon *tlv, uint32_t maxTlvCount, diff --git a/utils/tlv_utils/src/tlv_utils.cpp b/utils/tlv_utils/src/tlv_utils.cpp index feccfa315..7a4f1d402 100644 --- a/utils/tlv_utils/src/tlv_utils.cpp +++ b/utils/tlv_utils/src/tlv_utils.cpp @@ -50,20 +50,20 @@ uint8_t *TlvUtils::ParseTlv(const uint8_t *buffer, TlvCommon *tlv, const uint8_t return GetNextTlv(buffer); } -uint8_t *TlvUtils::AppendTlv(uint8_t *buffer, const TlvCommon *tlv, const uint8_t *boundary, uint32_t *retCode) +uint8_t *TlvUtils::AppendTlv(uint8_t *buffer, const TlvCommon *tlv, const uint32_t maxBuffSize, uint32_t *retCode) { - if (buffer >= boundary) { + if (maxBuffSize <= 0) { *retCode = TLV_ERR_BUFF_NO_ENOUGH; return nullptr; } - if (buffer + (reinterpret_cast(const_cast(buffer)))->len_ + TLV_TLV_HEAD_LEN >= boundary) { + if ((reinterpret_cast(const_cast(buffer)))->len_ + TLV_TLV_HEAD_LEN >= maxBuffSize) { *retCode = TLV_ERR_BUFF_NO_ENOUGH; return nullptr; } (reinterpret_cast(const_cast(buffer)))->tag_ = tlv->tag_; (reinterpret_cast(const_cast(buffer)))->len_ = tlv->len_; if (tlv->len_ != 0 && tlv->value_ != nullptr) { - if (memcpy_s(buffer + TLV_TLV_HEAD_LEN, boundary - buffer - TLV_TLV_HEAD_LEN, tlv->value_, tlv->len_) != + if (memcpy_s(buffer + TLV_TLV_HEAD_LEN, maxBuffSize - TLV_TLV_HEAD_LEN, tlv->value_, tlv->len_) != EOK) { *retCode = TLV_ERR_BUFF_NO_ENOUGH; return nullptr; @@ -80,11 +80,10 @@ uint32_t TlvUtils::Serialize(const TlvCommon *tlv, uint32_t tlvCount, uint8_t *b return TLV_ERR_INVALID_PARA; } uint8_t *curr = buff; - uint8_t *boundary = buff + maxBuffSize; uint32_t retCode = TLV_OK; for (uint32_t index = 0; index < tlvCount; index++) { - curr = AppendTlv(curr, &tlv[index], boundary, &retCode); + curr = AppendTlv(curr, &tlv[index], maxBuffSize, &retCode); if (curr == nullptr || retCode != TLV_OK) { return retCode; } -- Gitee