Skip to content

Commit

Permalink
Fix improperly mutable pointers to string constants (#30804)
Browse files Browse the repository at this point in the history
* Fix improperly mutable pointers to string constants

The declaration

  const char * kStringConstant = "value";

declares a mutable pointer to const C string, and so the following
assignment is legal:

  kStringConstant = "other value";

Obviously this is not desired. Declaring as "const char * const" avoids
this, but this declares an unnecessary pointer.

Change these declarations to "const(expr) char []".

These edits were made by the following command:

find . -name .git -prune -o -name .environment -prune -o -name third_party -prune -o -name zzz_generated -prune -o -name out -prune -o -type f \( -name '*.cpp' -o -name '*.h' \) -exec sed -i 's,^\( *\)\(static \|extern \|\)\(inline \|\)\(constexpr \|\)const char *\* * k\([A-Z][^][ ;()]*\)\( \|;\),\1\2\3\4const char k\5[]\6,g; s,^\([^()]*\)constexpr const char k\([^()]*\)\( \|;\),\1constexpr char k\2\3,g; s,^\(  *\)const\(expr\|\) char \([^()]*\)\[\] *= ",\1static const\2 char \3[] = ",g' {} +

with 2 fixes to add "inline" in constants defined in headers.

* Fix more mutable string constants

Fix static variables inside functions not named in kConstantNamingStyle:

find . -name .git -prune -o -name .environment -prune -o -name third_party -prune -o -name zzz_generated -prune -o -name out -prune -o -type f \( -name '*.cpp' -o -name '*.h' \) -exec sed -i 's,^\( *\)\(static \|extern \)\(inline \|\)\(constexpr \|\)const char *\* * \([^][ ;()]*\)\( *= *"\|;\),\1\2\3\4const char \5[]\6,g' {} +

Fix file scoped variables not named named in kConstantNamingStyle:

find . -name .git -prune -o -name .environment -prune -o -name third_party -prune -o -name zzz_generated -prune -o -name out -prune -o -type f \( -name '*.cpp' -o -name '*.h' \) -exec sed -i 's,^\(\)\(static \|extern \|\)\(inline \|\)\(constexpr \|\)const char *\* * \([^][ ;()]*\)\( *= *"\|;\),\1\2\3\4const char \5[]\6,g' {} +

* Also fix objc++

find . -name .git -prune -o -name .environment -prune -o -name third_party -prune -o -name zzz_generated -prune -o -name out -prune -o -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.mm' \) -exec sed -i 's,^\( *\)\(static \|extern \|\)\(inline \|\)\(constexpr \|\)const char *\* * k\([A-Z][^][ ;()]*\)\( \|;\),\1\2\3\4const char k\5[]\6,g; s,^\([^()]*\)constexpr const char k\([^()]*\)\( \|;\),\1constexpr char k\2\3,g; s,^\(  *\)const\(expr\|\) char \([^()]*\)\[\] *= ",\1static const\2 char \3[] = ",g' {} +
  • Loading branch information
mspang authored and pull[bot] committed Jan 17, 2024
1 parent c4fa0f6 commit 1202376
Show file tree
Hide file tree
Showing 160 changed files with 470 additions and 470 deletions.
2 changes: 1 addition & 1 deletion examples/air-purifier-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <ota/OTAInitializer.h>
#endif

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
2 changes: 1 addition & 1 deletion examples/air-quality-sensor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using namespace chip::app;
using namespace chip::app::Clusters;

namespace {
constexpr const char kChipEventFifoPathPrefix[] = "/tmp/chip_air_quality_fifo_";
constexpr char kChipEventFifoPathPrefix[] = "/tmp/chip_air_quality_fifo_";
NamedPipeCommands sChipNamedPipeCommands;
AirQualitySensorAppAttrUpdateDelegate sAirQualitySensorAppCommandDelegate;
} // namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, Att

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
const char SetupUrl[] = "https://example.com";
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::Span<const char>(SetupUrl, strlen(SetupUrl)));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#include "Globals.h"
#include "LEDWidget.h"

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define APP_EVENT_QUEUE_SIZE 10
#define APP_TASK_STACK_SIZE (3072)

static const char * TAG = "app-task";
static const char TAG[] = "app-task";

namespace {
TimerHandle_t sFunctionTimer; // FreeRTOS app sw timer
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/BluetoothWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "ScreenManager.h"
#endif

extern const char * TAG;
extern const char TAG[];

void BluetoothWidget::Init()
{
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <platform/CHIPDeviceLayer.h>
#include <vector>

static const char * TAG = "Button.cpp";
static const char TAG[] = "Button.cpp";

extern Button gButtons[BUTTON_NUMBER];

Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <app-common/zap-generated/ids/Clusters.h>
#endif

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace ::chip::Credentials;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

static const char * TAG = "DeviceWithDisplay";
static const char TAG[] = "DeviceWithDisplay";

#if CONFIG_DEVICE_TYPE_M5STACK

Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/QRCodeScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>

// TODO need sensible library tag when put in library
extern const char * TAG;
extern const char TAG[];

namespace {

Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/WiFiWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "ScreenManager.h"
#endif

extern const char * TAG;
extern const char TAG[];

void WiFiWidget::Init()
{
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ using namespace ::chip::Credentials;
// Used to indicate that an IP address has been added to the QRCode
#define EXAMPLE_VENDOR_TAG_IP 1

const char * TAG = "all-clusters-app";
extern const char TAG[] = "all-clusters-app";

static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ using namespace chip::DeviceLayer;

namespace {

constexpr const char kChipEventFifoPathPrefix[] = "/tmp/chip_all_clusters_fifo_";
constexpr char kChipEventFifoPathPrefix[] = "/tmp/chip_all_clusters_fifo_";
LowPowerManager sLowPowerManager;
NamedPipeCommands sChipNamedPipeCommands;
AllClustersCommandDelegate sAllClustersCommandDelegate;
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-app/nxp/mw320/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ enum
};
static int Matter_Selection = MAX_SELECTION;
#define RUN_RST_LT_DELAY 10
static const char * TAG = "mw320";
static const char TAG[] = "mw320";

/*******************************************************************************
* Variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "Globals.h"
#include "LEDWidget.h"

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-minimal-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#define APP_EVENT_QUEUE_SIZE 10
#define APP_TASK_STACK_SIZE (3072)

static const char * TAG = "app-task";
static const char TAG[] = "app-task";

namespace {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "ScreenManager.h"
#endif

extern const char * TAG;
extern const char TAG[];

void BluetoothWidget::Init()
{
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-minimal-app/esp32/main/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <platform/CHIPDeviceLayer.h>
#include <vector>

static const char * TAG = "Button.cpp";
static const char TAG[] = "Button.cpp";

extern Button gButtons[BUTTON_NUMBER];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <app-common/zap-generated/ids/Clusters.h>
#endif

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace ::chip::Credentials;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;

static const char * TAG = "DeviceWithDisplay";
static const char TAG[] = "DeviceWithDisplay";

#if CONFIG_DEVICE_TYPE_M5STACK

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>

// TODO need sensible library tag when put in library
extern const char * TAG;
extern const char TAG[];

namespace {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#if CONFIG_HAVE_DISPLAY
#include "ScreenManager.h"
#endif
extern const char * TAG;
extern const char TAG[];

void WiFiWidget::Init()
{
Expand Down
2 changes: 1 addition & 1 deletion examples/all-clusters-minimal-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ using namespace ::chip::DeviceManager;
// Used to indicate that an IP address has been added to the QRCode
#define EXAMPLE_VENDOR_TAG_IP 1

const char * TAG = "all-clusters-minimal-app";
extern const char TAG[] = "all-clusters-minimal-app";

static AppDeviceCallbacks EchoCallbacks;
static AppDeviceCallbacksDelegate sAppDeviceCallbacksDelegate;
Expand Down
2 changes: 1 addition & 1 deletion examples/bridge-app/asr/src/bridged-actions-stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, Att

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
const char SetupUrl[] = "https://example.com";
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::Span<const char>(SetupUrl, strlen(SetupUrl)));
}

Expand Down
4 changes: 2 additions & 2 deletions examples/bridge-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "DeviceCallbacks.h"

static const char * TAG = "bridge-devicecallbacks";
static const char TAG[] = "bridge-devicecallbacks";

using namespace ::chip;
using namespace ::chip::app;
Expand Down Expand Up @@ -78,7 +78,7 @@ CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, Att

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
const char SetupUrl[] = "https://example.com";
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::CharSpan::fromCharString(SetupUrl));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/bridge-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ chip::DeviceLayer::DeviceInfoProviderImpl gExampleDeviceInfoProvider;
#endif // CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER
} // namespace

const char * TAG = "bridge-app";
extern const char TAG[] = "bridge-app";

using namespace ::chip;
using namespace ::chip::DeviceManager;
Expand Down
2 changes: 1 addition & 1 deletion examples/bridge-app/linux/bridged-actions-stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, Att

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
const char SetupUrl[] = "https://example.com";
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::CharSpan::fromCharString(SetupUrl));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/bridge-app/telink/src/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CHIP_ERROR ActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, Att

CHIP_ERROR ActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
const char SetupUrl[] = "https://example.com";
static const char SetupUrl[] = "https://example.com";
return aEncoder.Encode(chip::CharSpan::fromCharString(SetupUrl));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/chef/ameba/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "Globals.h"
#include "LEDWidget.h"

static const char * TAG = "app-devicecallbacks";
static const char TAG[] = "app-devicecallbacks";

using namespace ::chip;
using namespace ::chip::Inet;
Expand Down
2 changes: 1 addition & 1 deletion examples/chef/esp32/main/QRCodeScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>

// TODO need sensible library tag when put in library
extern const char * TAG;
extern const char TAG[];

namespace {

Expand Down
2 changes: 1 addition & 1 deletion examples/chef/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
ChipLogProgress(Shell, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
}

const char * TAG = "chef-app";
extern const char TAG[] = "chef-app";

#if CONFIG_HAVE_DISPLAY
void printQRCode()
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/clusters/ComplexArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

#include "JsonParser.h"

inline constexpr uint8_t kMaxLabelLength = UINT8_MAX;
inline constexpr const char kNullString[] = "null";
inline constexpr uint8_t kMaxLabelLength = UINT8_MAX;
inline constexpr char kNullString[] = "null";

class ComplexArgumentParser
{
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/clusters/CustomArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ class CustomArgument
CHIP_ERROR Parse(const char * label, const char * json)
{
Json::Value value;
constexpr const char kHexNumPrefix[] = "0x";
constexpr size_t kHexNumPrefixLen = ArraySize(kHexNumPrefix) - 1;
static constexpr char kHexNumPrefix[] = "0x";
constexpr size_t kHexNumPrefixLen = ArraySize(kHexNumPrefix) - 1;
if (strncmp(json, kPayloadHexPrefix, kPayloadHexPrefixLen) == 0 ||
strncmp(json, kPayloadSignedPrefix, kPayloadSignedPrefixLen) == 0 ||
strncmp(json, kPayloadUnsignedPrefix, kPayloadUnsignedPrefixLen) == 0 ||
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include "DataModelLogger.h"
#include "ModelCommand.h"

inline constexpr const char * kWriteCommandKey = "write";
inline constexpr const char * kWriteByIdCommandKey = "write-by-id";
inline constexpr const char * kForceWriteCommandKey = "force-write";
inline constexpr char kWriteCommandKey[] = "write";
inline constexpr char kWriteByIdCommandKey[] = "write-by-id";
inline constexpr char kForceWriteCommandKey[] = "force-write";

enum class WriteCommandType
{
Expand Down
14 changes: 7 additions & 7 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ std::set<CHIPCommand *> CHIPCommand::sDeferredCleanups;

using DeviceControllerFactory = chip::Controller::DeviceControllerFactory;

constexpr chip::FabricId kIdentityNullFabricId = chip::kUndefinedFabricId;
constexpr chip::FabricId kIdentityAlphaFabricId = 1;
constexpr chip::FabricId kIdentityBetaFabricId = 2;
constexpr chip::FabricId kIdentityGammaFabricId = 3;
constexpr chip::FabricId kIdentityOtherFabricId = 4;
constexpr const char * kPAATrustStorePathVariable = "CHIPTOOL_PAA_TRUST_STORE_PATH";
constexpr const char * kCDTrustStorePathVariable = "CHIPTOOL_CD_TRUST_STORE_PATH";
constexpr chip::FabricId kIdentityNullFabricId = chip::kUndefinedFabricId;
constexpr chip::FabricId kIdentityAlphaFabricId = 1;
constexpr chip::FabricId kIdentityBetaFabricId = 2;
constexpr chip::FabricId kIdentityGammaFabricId = 3;
constexpr chip::FabricId kIdentityOtherFabricId = 4;
constexpr char kPAATrustStorePathVariable[] = "CHIPTOOL_PAA_TRUST_STORE_PATH";
constexpr char kCDTrustStorePathVariable[] = "CHIPTOOL_CD_TRUST_STORE_PATH";

const chip::Credentials::AttestationTrustStore * CHIPCommand::sTrustStore = nullptr;
chip::Credentials::GroupDataProviderImpl CHIPCommand::sGroupDataProvider{ kMaxGroupsPerFabric, kMaxGroupKeysPerFabric };
Expand Down
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@

#pragma once

inline constexpr const char kIdentityAlpha[] = "alpha";
inline constexpr const char kIdentityBeta[] = "beta";
inline constexpr const char kIdentityGamma[] = "gamma";
inline constexpr char kIdentityAlpha[] = "alpha";
inline constexpr char kIdentityBeta[] = "beta";
inline constexpr char kIdentityGamma[] = "gamma";
// The null fabric commissioner is a commissioner that isn't on a fabric.
// This is a legal configuration in which the commissioner delegates
// operational communication and invocation of the commssioning complete
Expand All @@ -46,7 +46,7 @@ inline constexpr const char kIdentityGamma[] = "gamma";
// commissioner portion of such an architecture. The null-fabric-commissioner
// can carry a commissioning flow up until the point of operational channel
// (CASE) communcation.
inline constexpr const char kIdentityNull[] = "null-fabric-commissioner";
inline constexpr char kIdentityNull[] = "null-fabric-commissioner";

class CHIPCommand : public Command
{
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <lib/support/StringSplitter.h>
#include <lib/support/logging/CHIPLogging.h>

constexpr const char * kOptionalArgumentPrefix = "--";
constexpr char kOptionalArgumentPrefix[] = "--";
constexpr size_t kOptionalArgumentPrefixLength = 2;

bool Command::InitArguments(int argc, char ** argv)
Expand Down Expand Up @@ -347,8 +347,8 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
// By default the parameter separator is ";" in order to not collapse with the argument itself if it contains commas
// (e.g a struct argument with multiple fields). In case one needs to use ";" it can be overriden with the following
// environment variable.
constexpr const char * kSeparatorVariable = "CHIPTOOL_CUSTOM_ARGUMENTS_SEPARATOR";
char * getenvSeparatorVariableResult = getenv(kSeparatorVariable);
static constexpr char kSeparatorVariable[] = "CHIPTOOL_CUSTOM_ARGUMENTS_SEPARATOR";
char * getenvSeparatorVariableResult = getenv(kSeparatorVariable);
getline(ss, valueAsString, getenvSeparatorVariableResult ? getenvSeparatorVariableResult[0] : ';');

CustomArgument * customArgument = new CustomArgument();
Expand Down
Loading

0 comments on commit 1202376

Please sign in to comment.