Skip to content

Commit

Permalink
Implement "reserved" for extension declaration.
Browse files Browse the repository at this point in the history
This can be useful when an extension field/declaration is deleted in schema to avoid data corruption.

It also adds a check for duplicate numbers in the declarations.

PiperOrigin-RevId: 523269837
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Apr 11, 2023
1 parent 11d9cd9 commit 41287bd
Show file tree
Hide file tree
Showing 8 changed files with 599 additions and 458 deletions.
566 changes: 284 additions & 282 deletions php/ext/google/protobuf/php-upb.c

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions php/ext/google/protobuf/php-upb.h
Original file line number Diff line number Diff line change
Expand Up @@ -4873,6 +4873,21 @@ UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_is_repeate
const upb_MiniTableField field = {4, 8, 4, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_reserved(google_protobuf_ExtensionRangeOptions_Declaration* msg) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) {
bool default_val = false;
bool ret;
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
return _upb_Message_HasNonExtensionField(msg, &field);
}

UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_number(google_protobuf_ExtensionRangeOptions_Declaration *msg, int32_t value) {
const upb_MiniTableField field = {1, 4, 1, kUpb_NoSub, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)};
Expand All @@ -4890,6 +4905,10 @@ UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_is_repeate
const upb_MiniTableField field = {4, 8, 4, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_reserved(google_protobuf_ExtensionRangeOptions_Declaration *msg, bool value) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_SetNonExtensionField(msg, &field, &value);
}

/* google.protobuf.FieldDescriptorProto */

Expand Down
7 changes: 4 additions & 3 deletions ruby/ext/google/protobuf_c/ruby-upb.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,24 +926,25 @@ const upb_MiniTable google_protobuf_ExtensionRangeOptions_msg_init = {
})
};

static const upb_MiniTableField google_protobuf_ExtensionRangeOptions_Declaration__fields[4] = {
static const upb_MiniTableField google_protobuf_ExtensionRangeOptions_Declaration__fields[5] = {
{1, 4, 1, kUpb_NoSub, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)},
{2, UPB_SIZE(12, 16), 2, kUpb_NoSub, 12, kUpb_FieldMode_Scalar | kUpb_LabelFlags_IsAlternate | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{3, UPB_SIZE(20, 32), 3, kUpb_NoSub, 12, kUpb_FieldMode_Scalar | kUpb_LabelFlags_IsAlternate | (kUpb_FieldRep_StringView << kUpb_FieldRep_Shift)},
{4, 8, 4, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
{5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)},
};

const upb_MiniTable google_protobuf_ExtensionRangeOptions_Declaration_msg_init = {
NULL,
&google_protobuf_ExtensionRangeOptions_Declaration__fields[0],
UPB_SIZE(32, 48), 4, kUpb_ExtMode_NonExtendable, 4, UPB_FASTTABLE_MASK(56), 0,
UPB_SIZE(32, 48), 5, kUpb_ExtMode_NonExtendable, 5, UPB_FASTTABLE_MASK(56), 0,
UPB_FASTTABLE_INIT({
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0004000001000008, &upb_psv4_1bt},
{0x0010000002000012, &upb_psb_1bt},
{0x002000000300001a, &upb_psb_1bt},
{0x0008000004000020, &upb_psb1_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0009000005000028, &upb_psb1_1bt},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
{0x0000000000000000, &_upb_FastDecoder_DecodeGeneric},
})
Expand Down
19 changes: 19 additions & 0 deletions ruby/ext/google/protobuf_c/ruby-upb.h
Original file line number Diff line number Diff line change
Expand Up @@ -4875,6 +4875,21 @@ UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_is_repeate
const upb_MiniTableField field = {4, 8, 4, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
return _upb_Message_HasNonExtensionField(msg, &field);
}
UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_clear_reserved(google_protobuf_ExtensionRangeOptions_Declaration* msg) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_ClearNonExtensionField(msg, &field);
}
UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) {
bool default_val = false;
bool ret;
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret;
}
UPB_INLINE bool google_protobuf_ExtensionRangeOptions_Declaration_has_reserved(const google_protobuf_ExtensionRangeOptions_Declaration* msg) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
return _upb_Message_HasNonExtensionField(msg, &field);
}

UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_number(google_protobuf_ExtensionRangeOptions_Declaration *msg, int32_t value) {
const upb_MiniTableField field = {1, 4, 1, kUpb_NoSub, 5, kUpb_FieldMode_Scalar | (kUpb_FieldRep_4Byte << kUpb_FieldRep_Shift)};
Expand All @@ -4892,6 +4907,10 @@ UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_is_repeate
const upb_MiniTableField field = {4, 8, 4, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_SetNonExtensionField(msg, &field, &value);
}
UPB_INLINE void google_protobuf_ExtensionRangeOptions_Declaration_set_reserved(google_protobuf_ExtensionRangeOptions_Declaration *msg, bool value) {
const upb_MiniTableField field = {5, 9, 5, kUpb_NoSub, 8, kUpb_FieldMode_Scalar | (kUpb_FieldRep_1Byte << kUpb_FieldRep_Shift)};
_upb_Message_SetNonExtensionField(msg, &field, &value);
}

/* google.protobuf.FieldDescriptorProto */

Expand Down
22 changes: 21 additions & 1 deletion src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7333,6 +7333,17 @@ void DescriptorBuilder::ValidateFieldOptions(

for (const auto& declaration : extension_range->options_->declaration()) {
if (declaration.number() != field->number()) continue;
if (declaration.reserved()) {
AddError(field->full_name(), proto,
DescriptorPool::ErrorCollector::EXTENDEE, [&] {
return absl::Substitute(
"Cannot use number $0 for extension field $1, as it is "
"reserved in the extension declarations for message $2.",
field->number(), field->full_name(),
field->containing_type()->full_name());
});
return;
}
CheckExtensionDeclaration(*field, proto, declaration.full_name(),
declaration.type(), declaration.is_repeated());
return;
Expand Down Expand Up @@ -7415,6 +7426,7 @@ void DescriptorBuilder::ValidateExtensionDeclaration(
const RepeatedPtrField<ExtensionRangeOptions_Declaration>& declarations,
const DescriptorProto_ExtensionRange& proto,
absl::flat_hash_set<absl::string_view>& full_name_set) {
absl::flat_hash_set<int> extension_number_set;
for (const auto& declaration : declarations) {
if (declaration.number() < proto.start() ||
declaration.number() >= proto.end()) {
Expand All @@ -7426,7 +7438,15 @@ void DescriptorBuilder::ValidateExtensionDeclaration(
});
}

if (!declaration.has_full_name() || !declaration.has_type()) {
if (!extension_number_set.insert(declaration.number()).second) {
AddError(full_name, proto, DescriptorPool::ErrorCollector::NUMBER, [&] {
return absl::Substitute(
"Extension declaration number $0 is declared multiple times.",
declaration.number());
});
}

if (declaration.has_full_name() != declaration.has_type()) {
AddError(full_name, proto, DescriptorPool::ErrorCollector::EXTENDEE, [&] {
return absl::StrCat(
"Extension declaration #", declaration.number(),
Expand Down
Loading

0 comments on commit 41287bd

Please sign in to comment.