Skip to content

Commit 6344ccf

Browse files
cjen1-msftachamayouCopilot
authored
Exhaustive enum switch (#7701)
Co-authored-by: Amaury Chamayou <amaury@xargs.fr> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Amaury Chamayou <amchamay@microsoft.com>
1 parent 50feda3 commit 6344ccf

26 files changed

Lines changed: 149 additions & 48 deletions

cmake/preproject.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function(add_warning_checks name)
7171
-Wno-unused-parameter
7272
-Wno-unused-function
7373
-Wshadow
74+
-Wswitch-enum
7475
)
7576
endfunction()
7677

include/ccf/crypto/curve.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ namespace ccf::crypto
4343
{
4444
switch (ec)
4545
{
46+
case CurveID::NONE:
47+
case CurveID::CURVE25519:
48+
case CurveID::X25519:
49+
{
50+
throw std::logic_error(fmt::format("Invalid ECDSA curve: {}", ec));
51+
}
4652
case CurveID::SECP384R1:
4753
return MDType::SHA384;
4854
case CurveID::SECP256R1:
4955
return MDType::SHA256;
5056
default:
5157
{
52-
throw std::logic_error(fmt::format("Unhandled CurveID: {}", ec));
58+
throw std::logic_error(fmt::format("Unhandled CurveId: {}", ec));
5359
}
5460
}
5561
}

include/ccf/crypto/jwk.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ namespace ccf::crypto
6969
{
7070
switch (curve_id)
7171
{
72+
case CurveID::NONE:
73+
case CurveID::CURVE25519:
74+
case CurveID::X25519:
75+
throw std::logic_error(
76+
fmt::format("Invalid JWK EC CurveId {}", curve_id));
7277
case CurveID::SECP384R1:
7378
return JsonWebKeyECCurve::P384;
7479
case CurveID::SECP256R1:
@@ -82,6 +87,9 @@ namespace ccf::crypto
8287
{
8388
switch (jwk_curve)
8489
{
90+
case JsonWebKeyECCurve::P521:
91+
throw std::logic_error(
92+
fmt::format("Unsupported JWK curve {}", jwk_curve));
8593
case JsonWebKeyECCurve::P384:
8694
return CurveID::SECP384R1;
8795
case JsonWebKeyECCurve::P256:
@@ -105,6 +113,10 @@ namespace ccf::crypto
105113
{
106114
switch (curve_id)
107115
{
116+
case CurveID::NONE:
117+
case CurveID::SECP384R1:
118+
case CurveID::SECP256R1:
119+
throw std::logic_error(fmt::format("Invalid EdDSA curve {}", curve_id));
108120
case CurveID::CURVE25519:
109121
return JsonWebKeyEdDSACurve::ED25519;
110122
case CurveID::X25519:

include/ccf/endpoint.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -508,30 +508,27 @@ struct formatter<ccf::endpoints::ForwardingRequired>
508508
auto format(
509509
const ccf::endpoints::ForwardingRequired& v, FormatContext& ctx) const
510510
{
511-
char const* s = nullptr;
512511
switch (v)
513512
{
514513
case ccf::endpoints::ForwardingRequired::Sometimes:
515514
{
516-
s = "sometimes";
517-
break;
515+
return format_to(ctx.out(), "sometimes");
518516
}
519517
case ccf::endpoints::ForwardingRequired::Always:
520518
{
521-
s = "always";
522-
break;
519+
return format_to(ctx.out(), "always");
523520
}
524521
case ccf::endpoints::ForwardingRequired::Never:
525522
{
526-
s = "never";
527-
break;
523+
return format_to(ctx.out(), "never");
528524
}
529525
default:
530526
{
531-
throw std::logic_error("Unhandled value for ForwardingRequired");
527+
throw std::logic_error(fmt::format(
528+
"Unhandled value for ForwardingRequired: {}",
529+
static_cast<uint8_t>(v)));
532530
}
533531
}
534-
return format_to(ctx.out(), "{}", s);
535532
}
536533
};
537534
FMT_END_NAMESPACE

include/ccf/pal/attestation_sev_snp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ pRb21iI1NlNCfOGUPIhVpWECAwEAAQ==
278278
.fmc = tcb.fmc};
279279
}
280280
default:
281-
throw std::logic_error(
282-
"Unsupported SEV-SNP product for TCB version policy");
281+
throw std::logic_error(fmt::format(
282+
"Unsupported SEV-SNP product for TCB version policy: {}", product));
283283
}
284284
}
285285

include/ccf/service/tables/proposals.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,18 @@ struct formatter<ccf::ProposalState>
7272
{
7373
return format_to(ctx.out(), "rejected");
7474
}
75+
case (ccf::ProposalState::FAILED):
76+
{
77+
return format_to(ctx.out(), "failed");
78+
}
7579
case (ccf::ProposalState::DROPPED):
7680
{
7781
return format_to(ctx.out(), "dropped");
7882
}
7983
default:
8084
{
81-
return format_to(ctx.out(), "UNKNOWN");
85+
throw std::logic_error(fmt::format(
86+
"Unknown proposal state {}", static_cast<uint8_t>(state)));
8287
}
8388
}
8489
}

samples/apps/programmability/programmability.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ namespace programmabilityapp
268268
"Failed to check if action is original");
269269
return true;
270270
}
271-
default:
271+
case ccf::ApiResult::Uninitialised:
272+
case ccf::ApiResult::NotFound:
272273
{
273274
return true;
274275
}

src/consensus/aft/raft.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,11 @@ namespace aft
799799
break;
800800
}
801801

802+
case raft_append_entries_signed_response:
802803
default:
803804
{
804-
RAFT_FAIL_FMT("Unhandled AFT message type: {}", type);
805+
RAFT_FAIL_FMT("Received unhandled AFT message type: {}", type);
806+
return;
805807
}
806808
}
807809
}
@@ -1444,7 +1446,11 @@ namespace aft
14441446
break;
14451447
}
14461448

1447-
default:
1449+
case ccf::kv::ApplyResult::PASS_BACKUP_SIGNATURE:
1450+
case ccf::kv::ApplyResult::PASS_BACKUP_SIGNATURE_SEND_ACK:
1451+
case ccf::kv::ApplyResult::PASS_NONCES:
1452+
case ccf::kv::ApplyResult::PASS_NEW_VIEW:
1453+
case ccf::kv::ApplyResult::PASS_APPLY:
14481454
{
14491455
throw std::logic_error("Unknown ApplyResult value");
14501456
}
@@ -2427,7 +2433,9 @@ namespace aft
24272433
case ccf::kv::LeadershipState::Candidate:
24282434
become_leader();
24292435
break;
2430-
default:
2436+
case ccf::kv::LeadershipState::None:
2437+
case ccf::kv::LeadershipState::Leader:
2438+
case ccf::kv::LeadershipState::Follower:
24312439
throw std::logic_error(
24322440
"add_vote_for_me() called while not a pre-vote candidate or "
24332441
"candidate");

src/crypto/openssl/hash.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ namespace ccf::crypto
3333
default:
3434
throw std::runtime_error("Unsupported hash algorithm");
3535
}
36-
return nullptr;
3736
}
3837

3938
std::vector<uint8_t> hkdf(

src/ds/actors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ccf
3737
{
3838
return "node";
3939
}
40-
default:
40+
case ActorsType::unknown:
4141
{
4242
return "";
4343
}

0 commit comments

Comments
 (0)