Skip to content

Commit

Permalink
Add ABSL_ATTRIBUTE_LIFETIME_BOUND attribute on generated repeated a…
Browse files Browse the repository at this point in the history
…nd map field accessors.

This allows the compiler to statically detect use-after-free bugs.

This change touches a subset of field types. More changes to follow.

PiperOrigin-RevId: 564777204
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 12, 2023
1 parent 3ae5a07 commit 9a0a3cf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
14 changes: 12 additions & 2 deletions src/google/protobuf/compiler/cpp/field_generators/enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -405,25 +405,35 @@ void RepeatedEnum::GenerateInlineAccessorDefinitions(io::Printer* p) const {
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return static_cast<$Enum$>(_internal_$name$().Get(index));
}
)cc");
p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Enum$ value) {
$assert_valid$;
_internal_mutable_$name$()->Set(index, value);
$annotate_set$
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
)cc");
p->Emit(R"cc(
inline void $Msg$::add_$name$($Enum$ value) {
$assert_valid$;
$TsanDetectConcurrentMutation$;
_internal_mutable_$name$()->Add(value);
$annotate_add$
// @@protoc_insertion_point(field_add:$pkg.Msg.field$)
}
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedField<int>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name$();
}
inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<int>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
Expand Down
10 changes: 8 additions & 2 deletions src/google/protobuf/compiler/cpp/field_generators/map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,23 @@ void Map::GenerateInlineAccessorDefinitions(io::Printer* p) const {
$TsanDetectConcurrentRead$;
return $field_$.GetMap();
}
inline const $Map$& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $Map$& $Msg$::$name$() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_get$;
// @@protoc_insertion_point(field_map:$pkg.Msg.field$)
return _internal_$name$();
}
)cc");
p->Emit(R"cc(
inline $Map$* $Msg$::_internal_mutable_$name$() {
$PrepareSplitMessageForWrite$;
$TsanDetectConcurrentMutation$;
return $field_$.MutableMap();
}
inline $Map$* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $Map$* $Msg$::mutable_$name$() ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable$;
// @@protoc_insertion_point(field_mutable_map:$pkg.Msg.field$)
return _internal_mutable_$name$();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,30 +474,40 @@ void RepeatedPrimitive::GenerateInlineAccessorDefinitions(
// @@protoc_insertion_point(field_get:$pkg.Msg.field$)
return _internal_$name$().Get(index);
}
)cc");
p->Emit(R"cc(
inline void $Msg$::set_$name$(int index, $Type$ value) {
$annotate_set$;
_internal_mutable_$name$()->Set(index, value);
// @@protoc_insertion_point(field_set:$pkg.Msg.field$)
}
)cc");
p->Emit(R"cc(
inline void $Msg$::add_$name$($Type$ value) {
$TsanDetectConcurrentMutation$;
_internal_mutable_$name$()->Add(value);
$annotate_add$;
// @@protoc_insertion_point(field_add:$pkg.Msg.field$)
}
inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const {
)cc");
p->Emit(R"cc(
inline const $pb$::RepeatedField<$Type$>& $Msg$::$name$() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_list$;
// @@protoc_insertion_point(field_list:$pkg.Msg.field$)
return _internal_$name$();
}
inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$() {
)cc");
p->Emit(R"cc(
inline $pb$::RepeatedField<$Type$>* $Msg$::mutable_$name$()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
$annotate_mutable_list$;
// @@protoc_insertion_point(field_mutable_list:$pkg.Msg.field$)
$TsanDetectConcurrentMutation$;
return _internal_mutable_$name$();
}
)cc");

if (should_split()) {
p->Emit(R"cc(
inline const $pb$::RepeatedField<$Type$>& $Msg$::_internal_$name$()
Expand Down
36 changes: 24 additions & 12 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a0a3cf

Please sign in to comment.