diff --git a/src/app/clusters/basic/basic.cpp b/src/app/clusters/basic/basic.cpp index a0645513ab6150..db602f5393fcb6 100644 --- a/src/app/clusters/basic/basic.cpp +++ b/src/app/clusters/basic/basic.cpp @@ -277,10 +277,12 @@ CHIP_ERROR BasicAttrAccess::ReadLocation(AttributeValueEncoder & aEncoder) if ((err != CHIP_NO_ERROR) || (codeLen == 0)) { strncpy(&location[0], "XX", kMaxLen + 1); - err = CHIP_NO_ERROR; + codeLen = strnlen(location, kMaxLen); + err = CHIP_NO_ERROR; } - return EncodeStringOnSuccess(err, aEncoder, location, kMaxLen); + ReturnErrorOnFailure(err); + return encoder.Encode(chip::CharSpan(buf, codeLen)); } CHIP_ERROR BasicAttrAccess::Write(const ConcreteDataAttributePath & aPath, AttributeValueDecoder & aDecoder) diff --git a/src/platform/Darwin/PosixConfig.cpp b/src/platform/Darwin/PosixConfig.cpp index 5215d7bbf997da..ee549d43a935d9 100644 --- a/src/platform/Darwin/PosixConfig.cpp +++ b/src/platform/Darwin/PosixConfig.cpp @@ -71,6 +71,7 @@ const PosixConfig::Key PosixConfig::kConfigKey_Breadcrumb = { kConfigNam const char PosixConfig::kGroupKeyNamePrefix[] = "gk-"; uint16_t PosixConfig::mPosixSetupDiscriminator = 0xF00; // CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR +char PosixConfig::mPosixCountryCode[3] = "XX"; CHIP_ERROR PosixConfig::Init() { @@ -111,9 +112,17 @@ CHIP_ERROR PosixConfig::ReadConfigValue(Key key, uint64_t & val) CHIP_ERROR PosixConfig::ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen) { CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; - SuccessOrExit(err); -exit: + if (key == kConfigKey_CountryCode) + { + // Do no assume null-termination on read-out + constexpr size_t kMaxLen = sizeof(PosixConfig::mPosixCountryCode) - 1; + outLen = strnlen(PosixConfig::mPosixCountryCode, kMaxLen); + VerifyOrReturnError(bufSize >= kMaxLen, CHIP_ERROR_BUFFER_TOO_SMALL); + memcpy(&buf[0], &PosixConfig::mPosixCountryCode[0], outLen); + return CHIP_NO_ERROR; + } + return err; } @@ -167,6 +176,15 @@ CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str) CHIP_ERROR PosixConfig::WriteConfigValueStr(Key key, const char * str, size_t strLen) { + if (key == kConfigKey_CountryCode) + { + VerifyOrReturnError(strLen < sizeof(PosixConfig::mPosixCountryCode), CHIP_ERROR_INVALID_ARGUMENT); + memcpy(&PosixConfig::mPosixCountryCode[0], str, strLen); + // Internally null-terminate so we may be able to log later. Don't assume we got null-termination on input. + PosixConfig::mPosixCountryCode[strLen] = '\0'; + return CHIP_NO_ERROR; + } + CHIP_ERROR err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; SuccessOrExit(err); diff --git a/src/platform/Darwin/PosixConfig.h b/src/platform/Darwin/PosixConfig.h index f45026905d6edd..052a564da82d1d 100644 --- a/src/platform/Darwin/PosixConfig.h +++ b/src/platform/Darwin/PosixConfig.h @@ -104,6 +104,7 @@ class PosixConfig private: // TODO: This is temporary until Darwin implements a proper ReadConfigValue static uint16_t mPosixSetupDiscriminator; + static char mPosixCountryCode[2 + 1]; }; struct PosixConfig::Key