Skip to content

Commit

Permalink
Fix improperly mutable pointers to string constants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mspang committed Dec 4, 2023
1 parent b9ff894 commit c0a120c
Show file tree
Hide file tree
Showing 90 changed files with 345 additions and 345 deletions.
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/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/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
2 changes: 1 addition & 1 deletion examples/bridge-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
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/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
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
10 changes: 5 additions & 5 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ namespace {

char kInteractiveModeName[] = "";
constexpr size_t kInteractiveModeArgumentsMaxLength = 32;
constexpr const char * kOptionalArgumentPrefix = "--";
constexpr const char * kJsonClusterKey = "cluster";
constexpr const char * kJsonCommandKey = "command";
constexpr const char * kJsonCommandSpecifierKey = "command_specifier";
constexpr const char * kJsonArgumentsKey = "arguments";
constexpr char kOptionalArgumentPrefix[] = "--";
constexpr char kJsonClusterKey[] = "cluster";
constexpr char kJsonCommandKey[] = "command";
constexpr char kJsonCommandSpecifierKey[] = "command_specifier";
constexpr char kJsonArgumentsKey[] = "arguments";

#if !CHIP_DISABLE_PLATFORM_KVS
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/DeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ using namespace chip::Dnssd;

#if CONFIG_NETWORK_LAYER_BLE
using namespace chip::Ble;
constexpr const char * kBleKey = "BLE";
constexpr char kBleKey[] = "BLE";
#endif // CONFIG_NETWORK_LAYER_BLE

CHIP_ERROR DeviceScanner::Start()
Expand Down
30 changes: 15 additions & 15 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
#include <lib/support/SafeInt.h>
#include <lib/support/jsontlv/TlvJson.h>

constexpr const char * kEventNumberKey = "eventNumber";
constexpr const char * kDataVersionKey = "dataVersion";
constexpr const char * kClusterIdKey = "clusterId";
constexpr const char * kEndpointIdKey = "endpointId";
constexpr const char * kAttributeIdKey = "attributeId";
constexpr const char * kEventIdKey = "eventId";
constexpr const char * kCommandIdKey = "commandId";
constexpr const char * kErrorIdKey = "error";
constexpr const char * kClusterErrorIdKey = "clusterError";
constexpr const char * kValueKey = "value";
constexpr const char * kNodeIdKey = "nodeId";
constexpr const char * kNOCKey = "NOC";
constexpr const char * kICACKey = "ICAC";
constexpr const char * kRCACKey = "RCAC";
constexpr const char * kIPKKey = "IPK";
constexpr char kEventNumberKey[] = "eventNumber";
constexpr char kDataVersionKey[] = "dataVersion";
constexpr char kClusterIdKey[] = "clusterId";
constexpr char kEndpointIdKey[] = "endpointId";
constexpr char kAttributeIdKey[] = "attributeId";
constexpr char kEventIdKey[] = "eventId";
constexpr char kCommandIdKey[] = "commandId";
constexpr char kErrorIdKey[] = "error";
constexpr char kClusterErrorIdKey[] = "clusterError";
constexpr char kValueKey[] = "value";
constexpr char kNodeIdKey[] = "nodeId";
constexpr char kNOCKey[] = "NOC";
constexpr char kICACKey[] = "ICAC";
constexpr char kRCACKey[] = "RCAC";
constexpr char kIPKKey[] = "IPK";

namespace {
RemoteDataModelLoggerDelegate * gDelegate;
Expand Down
12 changes: 6 additions & 6 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

#include <editline.h>

constexpr const char * kInteractiveModePrompt = ">>> ";
constexpr const char * kInteractiveModeHistoryFilePath = "/tmp/chip_tool_history";
constexpr const char * kInteractiveModeStopCommand = "quit()";
constexpr const char * kCategoryError = "Error";
constexpr const char * kCategoryProgress = "Info";
constexpr const char * kCategoryDetail = "Debug";
constexpr char kInteractiveModePrompt[] = ">>> ";
constexpr char kInteractiveModeHistoryFilePath[] = "/tmp/chip_tool_history";
constexpr char kInteractiveModeStopCommand[] = "quit()";
constexpr char kCategoryError[] = "Error";
constexpr char kCategoryProgress[] = "Info";
constexpr char kCategoryDetail[] = "Debug";

namespace {

Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/pairing/ToTLVCert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include <credentials/CHIPCert.h>
#include <lib/support/Base64.h>

constexpr const char kBase64Header[] = "base64:";
constexpr size_t kBase64HeaderLen = ArraySize(kBase64Header) - 1;
constexpr char kBase64Header[] = "base64:";
constexpr size_t kBase64HeaderLen = ArraySize(kBase64Header) - 1;

CHIP_ERROR ToBase64(const chip::ByteSpan & input, std::string & outputAsPrefixedBase64)
{
Expand Down
36 changes: 18 additions & 18 deletions examples/common/tracing/TraceDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ namespace trace {
namespace {

// Json keys
constexpr const char * kProtocolIdKey = "protocol_id";
constexpr const char * kProtocolCodeKey = "protocol_opcode";
constexpr const char * kSessionIdKey = "session_id";
constexpr const char * kExchangeIdKey = "exchange_id";
constexpr const char * kMessageCounterKey = "msg_counter";
constexpr const char * kSecurityFlagsKey = "security_flags";
constexpr const char * kMessageFlagsKey = "msg_flags";
constexpr const char * kSourceNodeIdKey = "source_node_id";
constexpr const char * kDestinationNodeIdKey = "dest_node_id";
constexpr const char * kDestinationGroupIdKey = "group_id";
constexpr const char * kExchangeFlagsKey = "exchange_flags";
constexpr const char * kIsInitiatorKey = "is_initiator";
constexpr const char * kNeedsAckKey = "is_ack_requested";
constexpr const char * kAckMsgKey = "acknowledged_msg_counter";
constexpr const char * kPayloadDataKey = "payload_hex";
constexpr const char * kPayloadSizeKey = "payload_size";
constexpr const char * kDirectionKey = "direction";
constexpr const char * kPeerAddress = "peer_address";
constexpr char kProtocolIdKey[] = "protocol_id";
constexpr char kProtocolCodeKey[] = "protocol_opcode";
constexpr char kSessionIdKey[] = "session_id";
constexpr char kExchangeIdKey[] = "exchange_id";
constexpr char kMessageCounterKey[] = "msg_counter";
constexpr char kSecurityFlagsKey[] = "security_flags";
constexpr char kMessageFlagsKey[] = "msg_flags";
constexpr char kSourceNodeIdKey[] = "source_node_id";
constexpr char kDestinationNodeIdKey[] = "dest_node_id";
constexpr char kDestinationGroupIdKey[] = "group_id";
constexpr char kExchangeFlagsKey[] = "exchange_flags";
constexpr char kIsInitiatorKey[] = "is_initiator";
constexpr char kNeedsAckKey[] = "is_ack_requested";
constexpr char kAckMsgKey[] = "acknowledged_msg_counter";
constexpr char kPayloadDataKey[] = "payload_hex";
constexpr char kPayloadSizeKey[] = "payload_size";
constexpr char kDirectionKey[] = "direction";
constexpr char kPeerAddress[] = "peer_address";

bool IsOutbound(const Json::Value & json)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/common/tracing/decoder/TraceDecoderProtocols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace {

constexpr const char * kUnknown = "Unknown";
constexpr char kUnknown[] = "Unknown";

void ENFORCE_FORMAT(1, 2) TLVPrettyPrinter(const char * aFormat, ...)
{
Expand Down
24 changes: 12 additions & 12 deletions examples/common/tracing/decoder/bdx/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
#include <protocols/bdx/BdxMessages.h>

namespace {
constexpr const char * kProtocolName = "Bulk Data Exchange";
constexpr char kProtocolName[] = "Bulk Data Exchange";

constexpr const char * kUnknown = "Unknown";
constexpr const char * kSendInit = "Send Init";
constexpr const char * kSendAccept = "Send Accept";
constexpr const char * kReceiveInit = "Receive Init";
constexpr const char * kReceiveAccept = "Receive Accept";
constexpr const char * kBlockQuery = "Block Query";
constexpr const char * kBlock = "Block";
constexpr const char * kBlockEOF = "Block End Of File";
constexpr const char * kBlockAck = "Block Ack";
constexpr const char * kBlockAckEOF = "Block Ack End Of File";
constexpr const char * kBlockQueryWithSkip = "Block Query With Skip";
constexpr char kUnknown[] = "Unknown";
constexpr char kSendInit[] = "Send Init";
constexpr char kSendAccept[] = "Send Accept";
constexpr char kReceiveInit[] = "Receive Init";
constexpr char kReceiveAccept[] = "Receive Accept";
constexpr char kBlockQuery[] = "Block Query";
constexpr char kBlock[] = "Block";
constexpr char kBlockEOF[] = "Block End Of File";
constexpr char kBlockAck[] = "Block Ack";
constexpr char kBlockAckEOF[] = "Block Ack End Of File";
constexpr char kBlockQueryWithSkip[] = "Block Query With Skip";
} // namespace

using MessageType = chip::bdx::MessageType;
Expand Down
8 changes: 4 additions & 4 deletions examples/common/tracing/decoder/echo/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <protocols/echo/Echo.h>

namespace {
constexpr const char * kProtocolName = "Echo";
constexpr char kProtocolName[] = "Echo";

constexpr const char * kUnknown = "Unknown";
constexpr const char * kEchoRequest = "Echo Request";
constexpr const char * kEchoResponse = "Echo Response";
constexpr char kUnknown[] = "Unknown";
constexpr char kEchoRequest[] = "Echo Request";
constexpr char kEchoResponse[] = "Echo Response";
} // namespace

using MessageType = chip::Protocols::Echo::MsgType;
Expand Down
26 changes: 13 additions & 13 deletions examples/common/tracing/decoder/interaction_model/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
#include <app/MessageDef/WriteResponseMessage.h>

namespace {
constexpr const char * kProtocolName = "Interaction Model";

constexpr const char * kUnknown = "Unknown";
constexpr const char * kStatusResponse = "Status Response";
constexpr const char * kReadRequest = "Read Request";
constexpr const char * kSubscribeRequest = "Subscribe Request";
constexpr const char * kSubscribeResponse = "Subscribe Response";
constexpr const char * kReportData = "Report Data";
constexpr const char * kWriteRequest = "Write Request";
constexpr const char * kWriteResponse = "Write Response";
constexpr const char * kInvokeCommandRequest = "InvokeCommandRequest";
constexpr const char * kInvokeCommandResponse = "InvokeCommandResponse";
constexpr const char * kTimedRequest = "Timed Request";
constexpr char kProtocolName[] = "Interaction Model";

constexpr char kUnknown[] = "Unknown";
constexpr char kStatusResponse[] = "Status Response";
constexpr char kReadRequest[] = "Read Request";
constexpr char kSubscribeRequest[] = "Subscribe Request";
constexpr char kSubscribeResponse[] = "Subscribe Response";
constexpr char kReportData[] = "Report Data";
constexpr char kWriteRequest[] = "Write Request";
constexpr char kWriteResponse[] = "Write Response";
constexpr char kInvokeCommandRequest[] = "InvokeCommandRequest";
constexpr char kInvokeCommandResponse[] = "InvokeCommandResponse";
constexpr char kTimedRequest[] = "Timed Request";

} // namespace

Expand Down
Loading

0 comments on commit c0a120c

Please sign in to comment.