Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc: validate reserved names are identifiers #10586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions src/google/protobuf/compiler/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "absl/strings/ascii.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/io/tokenizer.h"
Expand Down Expand Up @@ -389,13 +390,16 @@ void Parser::AddError(const std::string& error) {
AddError(input_->current().line, input_->current().column, error);
}

void Parser::AddWarning(const std::string& warning) {
void Parser::AddWarning(int line, int column, const std::string& warning) {
if (error_collector_ != nullptr) {
error_collector_->AddWarning(input_->current().line,
input_->current().column, warning);
error_collector_->AddWarning(line, column, warning);
}
}

void Parser::AddWarning(const std::string& warning) {
AddWarning(input_->current().line, input_->current().column, warning);
}

// -------------------------------------------------------------------

Parser::LocationRecorder::LocationRecorder(Parser* parser)
Expand Down Expand Up @@ -1728,11 +1732,23 @@ bool Parser::ParseReserved(DescriptorProto* message,
}
}

bool Parser::ParseReservedName(std::string* name, const char* error_message) {
// Capture the position of the token, in case we have to report an
// error after it is consumed.
int line = input_->current().line;
int col = input_->current().column;
DO(ConsumeString(name, error_message));
if (!io::Tokenizer::IsIdentifier(*name)) {
AddWarning(line, col, absl::StrFormat("Reserved name \"%s\" is not a valid identifier.", *name));
}
return true;
}

bool Parser::ParseReservedNames(DescriptorProto* message,
const LocationRecorder& parent_location) {
do {
LocationRecorder location(parent_location, message->reserved_name_size());
DO(ConsumeString(message->add_reserved_name(), "Expected field name."));
DO(ParseReservedName(message->add_reserved_name(), "Expected field name."));
} while (TryConsume(","));
DO(ConsumeEndOfDeclaration(";", &parent_location));
return true;
Expand Down Expand Up @@ -1787,42 +1803,42 @@ bool Parser::ParseReservedNumbers(DescriptorProto* message,
return true;
}

bool Parser::ParseReserved(EnumDescriptorProto* message,
const LocationRecorder& message_location) {
bool Parser::ParseReserved(EnumDescriptorProto* proto,
const LocationRecorder& enum_location) {
io::Tokenizer::Token start_token = input_->current();
// Parse the declaration.
DO(Consume("reserved"));
if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
LocationRecorder location(message_location,
LocationRecorder location(enum_location,
EnumDescriptorProto::kReservedNameFieldNumber);
location.StartAt(start_token);
return ParseReservedNames(message, location);
return ParseReservedNames(proto, location);
} else {
LocationRecorder location(message_location,
LocationRecorder location(enum_location,
EnumDescriptorProto::kReservedRangeFieldNumber);
location.StartAt(start_token);
return ParseReservedNumbers(message, location);
return ParseReservedNumbers(proto, location);
}
}

bool Parser::ParseReservedNames(EnumDescriptorProto* message,
bool Parser::ParseReservedNames(EnumDescriptorProto* proto,
const LocationRecorder& parent_location) {
do {
LocationRecorder location(parent_location, message->reserved_name_size());
DO(ConsumeString(message->add_reserved_name(), "Expected enum value."));
LocationRecorder location(parent_location, proto->reserved_name_size());
DO(ParseReservedName(proto->add_reserved_name(), "Expected enum value."));
} while (TryConsume(","));
DO(ConsumeEndOfDeclaration(";", &parent_location));
return true;
}

bool Parser::ParseReservedNumbers(EnumDescriptorProto* message,
bool Parser::ParseReservedNumbers(EnumDescriptorProto* proto,
const LocationRecorder& parent_location) {
bool first = true;
do {
LocationRecorder location(parent_location, message->reserved_range_size());
LocationRecorder location(parent_location, proto->reserved_range_size());

EnumDescriptorProto::EnumReservedRange* range =
message->add_reserved_range();
proto->add_reserved_range();
int start, end;
io::Tokenizer::Token start_token;
{
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ class PROTOBUF_EXPORT Parser {
// of the current token.
void AddError(const std::string& error);

// Invokes error_collector_->AddWarning(), if error_collector_ is not NULL.
void AddWarning(int line, int column, const std::string& warning);

// Invokes error_collector_->AddWarning() with the line and column number
// of the current token.
void AddWarning(const std::string& warning);
Expand Down Expand Up @@ -394,6 +397,7 @@ class PROTOBUF_EXPORT Parser {
const LocationRecorder& message_location);
bool ParseReservedNames(DescriptorProto* message,
const LocationRecorder& parent_location);
bool ParseReservedName(std::string* name, const char* error_message);
bool ParseReservedNumbers(DescriptorProto* message,
const LocationRecorder& parent_location);
bool ParseReserved(EnumDescriptorProto* message,
Expand Down
31 changes: 31 additions & 0 deletions src/google/protobuf/compiler/parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ class ParserTest : public testing::Test {
EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type);
}

// Parse the text and expect that the given warnings are reported.
void ExpectHasWarnings(const char* text, const char* expected_warnings) {
SetupParser(text);
FileDescriptorProto file;
ASSERT_TRUE(parser_->Parse(input_.get(), &file));
EXPECT_EQ(io::Tokenizer::TYPE_END, input_->current().type);
ASSERT_EQ("", error_collector_.text_);
EXPECT_EQ(expected_warnings, error_collector_.warning_);
}

// Same as above but does not expect that the parser parses the complete
// input.
void ExpectHasEarlyExitErrors(const char* text, const char* expected_errors) {
Expand Down Expand Up @@ -1675,6 +1685,17 @@ TEST_F(ParseErrorTest, EnumReservedMissingQuotes) {
"2:11: Expected enum value or number range.\n");
}

TEST_F(ParseErrorTest, EnumReservedInvalidIdentifier) {
ExpectHasWarnings(
R"pb(
enum TestEnum {
FOO = 1;
reserved "foo bar";
}
)pb",
"3:17: Reserved name \"foo bar\" is not a valid identifier.\n");
}

// -------------------------------------------------------------------
// Reserved field number errors

Expand Down Expand Up @@ -1702,6 +1723,16 @@ TEST_F(ParseErrorTest, ReservedMissingQuotes) {
"1:11: Expected field name or number range.\n");
}

TEST_F(ParseErrorTest, ReservedInvalidIdentifier) {
ExpectHasWarnings(
R"pb(
message Foo {
reserved "foo bar";
}
)pb",
"2:17: Reserved name \"foo bar\" is not a valid identifier.\n");
}

TEST_F(ParseErrorTest, ReservedNegativeNumber) {
ExpectHasErrors(
"message Foo {\n"
Expand Down