Skip to content

Commit eebaa51

Browse files
Alexey Sidnevmeta-codesync[bot]
authored andcommitted
Fix UB in to_string(Error) for unrecognized error codes (#18554)
Summary: Pull Request resolved: #18554 executorch::runtime::to_string(Error) returns const char* via a switch statement with no default case. For unrecognized error codes, the function falls off the end without returning (undefined behavior), which in practice returns nullptr. The fix: 1. Adds a fallback return "Error::Unknown" after the switch in to_string() (without a default: case, preserving -Wswitch coverage for missing enum values). 2. Adds tests for to_string() covering all 21 enum values and the unknown error code path. Differential Revision: D97662639
1 parent 520566c commit eebaa51

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

runtime/core/error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ constexpr const char* to_string(const Error error) {
152152
case Error::RegistrationAlreadyRegistered:
153153
return "Error::RegistrationAlreadyRegistered";
154154
}
155+
return "Error::Unknown";
155156
}
156157

157158
} // namespace runtime

runtime/core/test/error_handling_test.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,75 @@ class Movable {
105105
mutable void* buffer_;
106106
};
107107

108+
TEST(ErrorHandlingTest, ToStringKnownError) {
109+
EXPECT_STREQ(
110+
executorch::runtime::to_string(Error::Ok), "Error::Ok");
111+
EXPECT_STREQ(
112+
executorch::runtime::to_string(Error::Internal), "Error::Internal");
113+
EXPECT_STREQ(
114+
executorch::runtime::to_string(Error::InvalidState),
115+
"Error::InvalidState");
116+
EXPECT_STREQ(
117+
executorch::runtime::to_string(Error::EndOfMethod),
118+
"Error::EndOfMethod");
119+
EXPECT_STREQ(
120+
executorch::runtime::to_string(Error::AlreadyLoaded),
121+
"Error::AlreadyLoaded");
122+
EXPECT_STREQ(
123+
executorch::runtime::to_string(Error::NotSupported),
124+
"Error::NotSupported");
125+
EXPECT_STREQ(
126+
executorch::runtime::to_string(Error::NotImplemented),
127+
"Error::NotImplemented");
128+
EXPECT_STREQ(
129+
executorch::runtime::to_string(Error::InvalidArgument),
130+
"Error::InvalidArgument");
131+
EXPECT_STREQ(
132+
executorch::runtime::to_string(Error::InvalidType),
133+
"Error::InvalidType");
134+
EXPECT_STREQ(
135+
executorch::runtime::to_string(Error::OperatorMissing),
136+
"Error::OperatorMissing");
137+
EXPECT_STREQ(
138+
executorch::runtime::to_string(Error::RegistrationExceedingMaxKernels),
139+
"Error::RegistrationExceedingMaxKernels");
140+
EXPECT_STREQ(
141+
executorch::runtime::to_string(Error::RegistrationAlreadyRegistered),
142+
"Error::RegistrationAlreadyRegistered");
143+
EXPECT_STREQ(
144+
executorch::runtime::to_string(Error::NotFound), "Error::NotFound");
145+
EXPECT_STREQ(
146+
executorch::runtime::to_string(Error::MemoryAllocationFailed),
147+
"Error::MemoryAllocationFailed");
148+
EXPECT_STREQ(
149+
executorch::runtime::to_string(Error::AccessFailed),
150+
"Error::AccessFailed");
151+
EXPECT_STREQ(
152+
executorch::runtime::to_string(Error::InvalidProgram),
153+
"Error::InvalidProgram");
154+
EXPECT_STREQ(
155+
executorch::runtime::to_string(Error::InvalidExternalData),
156+
"Error::InvalidExternalData");
157+
EXPECT_STREQ(
158+
executorch::runtime::to_string(Error::OutOfResources),
159+
"Error::OutOfResources");
160+
EXPECT_STREQ(
161+
executorch::runtime::to_string(Error::DelegateInvalidCompatibility),
162+
"Error::DelegateInvalidCompatibility");
163+
EXPECT_STREQ(
164+
executorch::runtime::to_string(Error::DelegateMemoryAllocationFailed),
165+
"Error::DelegateMemoryAllocationFailed");
166+
EXPECT_STREQ(
167+
executorch::runtime::to_string(Error::DelegateInvalidHandle),
168+
"Error::DelegateInvalidHandle");
169+
}
170+
171+
TEST(ErrorHandlingTest, ToStringUnknownError) {
172+
auto unknown = static_cast<Error>(0xFF);
173+
const char* result = executorch::runtime::to_string(unknown);
174+
EXPECT_STREQ(result, "Error::Unknown");
175+
}
176+
108177
TEST(ErrorHandlingTest, ResultBasic) {
109178
Result<uint32_t> r(1);
110179
ASSERT_TRUE(r.ok());

0 commit comments

Comments
 (0)