Skip to content

Change CommandParser::parse to return StatusOr#45752

Merged
yanavlasov merged 4 commits into
envoyproxy:mainfrom
yanavlasov:remove-exceptions-5
Jun 24, 2026
Merged

Change CommandParser::parse to return StatusOr#45752
yanavlasov merged 4 commits into
envoyproxy:mainfrom
yanavlasov:remove-exceptions-5

Conversation

@yanavlasov

Copy link
Copy Markdown
Contributor

Change CommandParser::parse return value to StatusOr<>. This is preparation for removing exceptions from this method. Presently no parse overrides return error status and C++ exceptions still in use.

Risk Level: low, noop
Testing: unit tests
Docs Changes: no
Release Notes: no
Platform Specific Features: no

Signed-off-by: Yan Avlasov <yavlasov@google.com>
kyessenov
kyessenov previously approved these changes Jun 22, 2026

@kyessenov kyessenov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a good change!

@botengyao botengyao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you fix the CI?

/wait

@botengyao botengyao self-assigned this Jun 23, 2026
Signed-off-by: yavlasov <yavlasov@google.com>
*/
virtual FormatterProviderPtr parse(absl::string_view command, absl::string_view command_arg,
absl::optional<size_t> max_length) const PURE;
virtual FormatterProviderResult parse(absl::string_view command, absl::string_view command_arg,

@ravenblackx ravenblackx Jun 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be more readable as a straight absl::StatusOr<FormatterProviderPtr> return type. At the call-sites using FormatterProviderResult sounds like a result from a FormatterProvider, not a result that may be a FormatterProvider.

Alternatively, MaybeFormatterProvider or FormatterProviderOrStatus would also work, but just doing it straight with the StatusOr and FormatterProviderPtr helper to avoid double-nesting seems simple and self-explanatory.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// Try built-in command parsers to create the formatter.
for (const auto& parser : BuiltInCommandParserFactoryHelper::commandParsers()) {
auto formatter = parser->parse(command, param, max_length);
auto formatter_result = parser->parse(command, param, max_length);

@ravenblackx ravenblackx Jun 23, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually get berated for auto and I think this one is a good case for not doing it since it's again not clear without reading more of the function for context what a formatter_result is.
(The auto below is fine since it's explained by this one being a StatusOr)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, including other places

Comment on lines 110 to 113
auto formatter = std::move(formatter_result).value();
if (formatter != nullptr) {
return formatter;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be just ASSERT that it's not nullptr, or is there a valid case where a non-error-status is returned, but also no pointer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it can return nullptr. Not sure if this is an error or not. I will be looking into these case then removing exceptions from parse overrides.

Comment on lines 257 to 258
auto provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length).value();
ASSERT_TRUE(provider != nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a test-crash in the case of parse returning an error status.

Recommend instead

Suggested change
auto provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length).value();
ASSERT_TRUE(provider != nullptr);
auto status_or_provider = commands[0]->parse("COMMAND_EXTENSION", "", max_length);
ASSERT_THAT(status_or_provider, IsOkAndHolds(NotNull());

(With #include "test/test_common/status_utility.h" and appropriate usings)

Same comment twice more in this file, once in test/common/formatter/substitution_formatter_test.cc, and many times in test/extensions/filters/udp/dns_filter/dns_filter_access_log_test.cc (arguably not in the helper function), and many in cel_test.

For these later 'many' cases I would suggest bumping the provider-collection up into the test class's initialization or a helper function, to cut down on repetition and make it clearer that the tests aren't really testing the return value of the parse function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, including other cases. Thanks for suggestion.

Comment on lines +156 to 157
auto provider = parser->parse("NOT_SECRET", "my-token", absl::nullopt).value();
EXPECT_EQ(nullptr, provider);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, so it is expected to sometimes return a non-error null?

@ravenblackx ravenblackx self-assigned this Jun 23, 2026
Signed-off-by: yavlasov <yavlasov@google.com>
Copilot AI added a commit that referenced this pull request Jun 23, 2026
- Change CommandParser::parse to return absl::StatusOr<FormatterProviderPtr>
- Update all formatter implementations and extensions
- Update all callers and tests to use new StatusOr return type
- Preserve CVE-2026-47220 fix (null checks in getHostFromHeaders)
- Keep CVE tests for empty headers in substitution_formatter_test.cc

Co-authored-by: yanavlasov <6360027+yanavlasov@users.noreply.github.com>
@yanavlasov yanavlasov merged commit 4430600 into envoyproxy:main Jun 24, 2026
28 checks passed
@yanavlasov yanavlasov deleted the remove-exceptions-5 branch June 26, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants