From 2231472749cb9c0803e3e774254b9e9b7f7eed2d Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 7 Feb 2022 18:29:53 -0500 Subject: [PATCH] Ensure buff size constraints in chip logging module name fetch (#14844) * Update logging logic a bit * Enforce buffer size at compile time * Place more logging bits into anon namespace to make them static. Help the linker out a bit --- src/lib/support/logging/CHIPLogging.cpp | 97 +++++++++++++------------ src/lib/support/logging/CHIPLogging.h | 6 -- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/lib/support/logging/CHIPLogging.cpp b/src/lib/support/logging/CHIPLogging.cpp index 57dacbe3f1b9f0..4b3a5142b8d524 100644 --- a/src/lib/support/logging/CHIPLogging.cpp +++ b/src/lib/support/logging/CHIPLogging.cpp @@ -45,8 +45,6 @@ namespace { std::atomic sLogRedirectCallback{ nullptr }; -} - /* * Array of strings containing the names for each of the chip log * modules. @@ -57,57 +55,64 @@ std::atomic sLogRedirectCallback{ nullptr }; * necessary. * */ -static const char ModuleNames[] = "-\0\0" // None - "IN\0" // Inet - "BLE" // BLE - "ML\0" // MessageLayer - "SM\0" // SecurityManager - "EM\0" // ExchangeManager - "TLV" // TLV - "ASN" // ASN1 - "CR\0" // Crypto - "CTL" // Controller - "AL\0" // Alarm - "SC\0" // SecureChannel - "BDX" // BulkDataTransfer - "DMG" // DataManagement - "DC\0" // DeviceControl - "DD\0" // DeviceDescription - "ECH" // Echo - "FP\0" // FabricProvisioning - "NP\0" // NetworkProvisioning - "SD\0" // ServiceDirectory - "SP\0" // ServiceProvisioning - "SWU" // SoftwareUpdate - "TP\0" // TokenPairing - "TS\0" // TimeServices - "HB\0" // Heartbeat - "CSL" // chipSystemLayer - "EVL" // Event Logging - "SPT" // Support - "TOO" // chipTool - "ZCL" // Zcl - "SH\0" // Shell - "DL\0" // DeviceLayer - "SPL" // SetupPayload - "SVR" // AppServer - "DIS" // Discovery - "IM\0" // InteractionModel - "TST" // Test - "ODP" // OperationalDeviceProxy - "ATM" // Automation +const char ModuleNames[] = "-\0\0" // None + "IN\0" // Inet + "BLE" // BLE + "ML\0" // MessageLayer + "SM\0" // SecurityManager + "EM\0" // ExchangeManager + "TLV" // TLV + "ASN" // ASN1 + "CR\0" // Crypto + "CTL" // Controller + "AL\0" // Alarm + "SC\0" // SecureChannel + "BDX" // BulkDataTransfer + "DMG" // DataManagement + "DC\0" // DeviceControl + "DD\0" // DeviceDescription + "ECH" // Echo + "FP\0" // FabricProvisioning + "NP\0" // NetworkProvisioning + "SD\0" // ServiceDirectory + "SP\0" // ServiceProvisioning + "SWU" // SoftwareUpdate + "TP\0" // TokenPairing + "TS\0" // TimeServices + "HB\0" // Heartbeat + "CSL" // chipSystemLayer + "EVL" // Event Logging + "SPT" // Support + "TOO" // chipTool + "ZCL" // Zcl + "SH\0" // Shell + "DL\0" // DeviceLayer + "SPL" // SetupPayload + "SVR" // AppServer + "DIS" // Discovery + "IM\0" // InteractionModel + "TST" // Test + "ODP" // OperationalDeviceProxy + "ATM" // Automation ; #define ModuleNamesCount ((sizeof(ModuleNames) - 1) / chip::Logging::kMaxModuleNameLen) -void GetModuleName(char * buf, uint8_t bufSize, uint8_t module) +void GetModuleName(char (&buf)[chip::Logging::kMaxModuleNameLen + 1], uint8_t module) { - const char * moduleNamePtr = ModuleNames + ((module < ModuleNamesCount) ? module * chip::Logging::kMaxModuleNameLen : 0); - snprintf(buf, bufSize, "%s", moduleNamePtr); - buf[chip::Logging::kMaxModuleNameLen] = 0; + const char * module_name = ModuleNames; + if (module < ModuleNamesCount) + { + module_name += module * chip::Logging::kMaxModuleNameLen; + } + + memcpy(buf, module_name, chip::Logging::kMaxModuleNameLen); + buf[chip::Logging::kMaxModuleNameLen] = 0; // ensure null termination } +} // namespace + void SetLogRedirectCallback(LogRedirectCallback_t callback) { sLogRedirectCallback.store(callback); @@ -183,7 +188,7 @@ void LogV(uint8_t module, uint8_t category, const char * msg, va_list args) } char moduleName[chip::Logging::kMaxModuleNameLen + 1]; - GetModuleName(moduleName, sizeof(moduleName), module); + GetModuleName(moduleName, module); LogRedirectCallback_t redirect = sLogRedirectCallback.load(); diff --git a/src/lib/support/logging/CHIPLogging.h b/src/lib/support/logging/CHIPLogging.h index fa48aa487c2041..491e139388d407 100644 --- a/src/lib/support/logging/CHIPLogging.h +++ b/src/lib/support/logging/CHIPLogging.h @@ -204,7 +204,6 @@ static constexpr uint16_t kMaxMessagePadding = (chip::Logging::kMaxPrefixLen + c chip::Logging::kMaxSeparatorLen + chip::Logging::kMaxTrailerLen); void GetMessageWithPrefix(char * buf, uint8_t bufSize, uint8_t module, const char * msg); -void GetModuleName(char * buf, uint8_t bufSize, uint8_t module); #else @@ -213,11 +212,6 @@ static inline void GetMessageWithPrefix(char * buf, uint8_t bufSize, uint8_t mod return; } -static inline void GetModuleName(char * buf, uint8_t bufSize, uint8_t module) -{ - return; -} - #endif // _CHIP_USE_LOGGING bool IsCategoryEnabled(uint8_t category);