Tighten up size calculation for flexible array members

PiperOrigin-RevId: 697036283
pull/19285/head
Protobuf Team Bot 3 months ago committed by Copybara-Service
parent 28812a8fae
commit f1d81a0d38
  1. 17
      csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs
  2. 2
      upb/mini_descriptor/build_enum.c
  3. 9
      upb/port/def.inc
  4. 1
      upb/port/undef.inc

@ -1,17 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#endregion
namespace Google.Protobuf.Reflection;
internal sealed partial class FeatureSetDescriptor
{
// Canonical serialized form of the edition defaults, generated by embed_edition_defaults.
private const string DefaultsBase64 =
"ChMYhAciACoMCAEQAhgCIAMoATACChMY5wciACoMCAIQARgBIAIoATABChMY6AciDAgBEAEYASACKAEwASoAIOYHKOgH";
}

@ -31,7 +31,7 @@ typedef struct {
} upb_MdEnumDecoder;
static size_t upb_MiniTableEnum_Size(size_t count) {
return sizeof(upb_MiniTableEnum) + count * sizeof(uint32_t);
return UPB_SIZEOF_FLEX(upb_MiniTableEnum, UPB_PRIVATE(data), count);
}
static upb_MiniTableEnum* _upb_MiniTable_AddEnumDataMember(upb_MdEnumDecoder* d,

@ -64,6 +64,15 @@ Error, UINTPTR_MAX is undefined
*/
#define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
// A flexible array member may have lower alignment requirements than the struct
// overall - in that case, it can overlap with the trailing padding of the rest
// of the struct, and a naive sizeof(base) + sizeof(flex) * count calculation
// will not take into account that overlap, and allocate more than is required.
#define UPB_SIZEOF_FLEX(type, member, count) \
(UPB_MAX(sizeof(type), \
(offsetof(type, member) + \
count * (offsetof(type, member[1]) - offsetof(type, member[0])))))
#define UPB_MAPTYPE_STRING 0
// UPB_EXPORT: always generate a public symbol.

@ -9,6 +9,7 @@
#undef UPB_SIZE
#undef UPB_PTR_AT
#undef UPB_SIZEOF_FLEX
#undef UPB_MAPTYPE_STRING
#undef UPB_EXPORT
#undef UPB_INLINE

Loading…
Cancel
Save