Skip to content

Commit 4594d4e

Browse files
Simplify the validation code by using VerifyOrReturnValue
setUpPINCode range is checked in IsValidSetupPIN() via CheckPayloadCommonConstraints() so no separate check is needed.
1 parent ed17bfe commit 4594d4e

File tree

2 files changed

+15
-44
lines changed

2 files changed

+15
-44
lines changed

src/setup_payload/SetupPayload.cpp

+14-44
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,23 @@ namespace chip {
3939
bool PayloadContents::isValidQRCodePayload(ValidationMode mode) const
4040
{
4141
// 3-bit value specifying the QR code payload version.
42-
if (version >= 1 << kVersionFieldLengthInBits)
43-
{
44-
return false;
45-
}
42+
VerifyOrReturnValue(version < (1 << kVersionFieldLengthInBits), false);
4643

47-
if (static_cast<uint8_t>(commissioningFlow) > static_cast<uint8_t>((1 << kCommissioningFlowFieldLengthInBits) - 1))
48-
{
49-
return false;
50-
}
44+
VerifyOrReturnValue(static_cast<uint8_t>(commissioningFlow) < (1 << kCommissioningFlowFieldLengthInBits), false);
5145

5246
// Device Commissioning Flow
5347
// Even in ValidatoinMode::kConsume we can only handle modes that we understand.
5448
// 0: Standard commissioning flow: such a device, when uncommissioned, always enters commissioning mode upon power-up, subject
5549
// to the rules in [ref_Announcement_Commencement]. 1: User-intent commissioning flow: user action required to enter
5650
// commissioning mode. 2: Custom commissioning flow: interaction with a vendor-specified means is needed before commissioning.
5751
// 3: Reserved
58-
if (commissioningFlow != CommissioningFlow::kStandard && commissioningFlow != CommissioningFlow::kUserActionRequired &&
59-
commissioningFlow != CommissioningFlow::kCustom)
60-
{
61-
return false;
62-
}
52+
VerifyOrReturnValue(commissioningFlow == CommissioningFlow::kStandard ||
53+
commissioningFlow == CommissioningFlow::kUserActionRequired ||
54+
commissioningFlow == CommissioningFlow::kCustom,
55+
false);
6356

6457
// General discriminator validity is enforced by the SetupDiscriminator class, but it can't be short for QR a code.
65-
if (discriminator.IsShortDiscriminator())
66-
{
67-
return false;
68-
}
69-
70-
if (setUpPINCode >= 1 << kSetupPINCodeFieldLengthInBits)
71-
{
72-
return false;
73-
}
58+
VerifyOrReturnValue(!discriminator.IsShortDiscriminator(), false);
7459

7560
// RendevouzInformation must be present for a QR code.
7661
VerifyOrReturnValue(rendezvousInformation.HasValue(), false);
@@ -86,12 +71,8 @@ bool PayloadContents::isValidQRCodePayload(ValidationMode mode) const
8671

8772
bool PayloadContents::isValidManualCode(ValidationMode mode) const
8873
{
89-
// Discriminator validity is enforced by the SetupDiscriminator class.
90-
if (setUpPINCode >= 1 << kSetupPINCodeFieldLengthInBits)
91-
{
92-
return false;
93-
}
94-
74+
// No additional constraints apply to Manual Pairing Codes.
75+
// (If the payload has a long discriminator it will be converted automatically.)
9576
return CheckPayloadCommonConstraints();
9677
}
9778

@@ -114,30 +95,19 @@ bool PayloadContents::CheckPayloadCommonConstraints() const
11495
// Validation rules in this method apply to all validation modes.
11596

11697
// Even in ValidationMode::kConsume we don't understand how to handle any payload version other than 0.
117-
if (version != 0)
118-
{
119-
return false;
120-
}
98+
VerifyOrReturnValue(version == 0, false);
12199

122-
if (!IsValidSetupPIN(setUpPINCode))
123-
{
124-
return false;
125-
}
100+
VerifyOrReturnValue(IsValidSetupPIN(setUpPINCode), false);
126101

127102
// VendorID must be unspecified (0) or in valid range expected.
128-
if (!IsVendorIdValidOperationally(vendorID) && (vendorID != VendorId::Unspecified))
129-
{
130-
return false;
131-
}
103+
VerifyOrReturnValue((vendorID == VendorId::Unspecified) || IsVendorIdValidOperationally(vendorID), false);
132104

133105
// A value of 0x0000 SHALL NOT be assigned to a product since Product ID = 0x0000 is used for these specific cases:
134106
// * To announce an anonymized Product ID as part of device discovery
135107
// * To indicate an OTA software update file applies to multiple Product IDs equally.
136108
// * To avoid confusion when presenting the Onboarding Payload for ECM with multiple nodes
137-
if (productID == 0 && vendorID != VendorId::Unspecified)
138-
{
139-
return false;
140-
}
109+
// In these special cases the vendorID must be 0 (Unspecified)
110+
VerifyOrReturnValue(productID != 0 || vendorID == VendorId::Unspecified, false);
141111

142112
return true;
143113
}

src/setup_payload/SetupPayload.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ inline constexpr uint8_t kCommissioningTimeoutTag = 0x04;
7676

7777
inline constexpr uint32_t kSetupPINCodeMaximumValue = 99999998;
7878
inline constexpr uint32_t kSetupPINCodeUndefinedValue = 0;
79+
static_assert(kSetupPINCodeMaximumValue < (1 << kSetupPINCodeFieldLengthInBits));
7980

8081
// clang-format off
8182
const int kTotalPayloadDataSizeInBits =

0 commit comments

Comments
 (0)