https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93437

            Bug ID: 93437
           Summary: On protobuf generated code, -Warray-bounds is brittle
                    and not very helpful
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rafael at espindo dot la
  Target Milestone: ---

Created attachment 47710
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47710&action=edit
testcase

In the attached testcase, "g++ -S metrics2.pb.ii -O2 -Wall" produces no
warning, but "g++ -S metrics2.pb.ii -O3 -Wall" produces

In constructor ‘Bucket::Bucket()’,
    inlined from ‘void InitDefaultsBucket()’ at metrics2.pb.ii:30:5:
metrics2.pb.ii:24:13: warning: ‘void* memset(void*, int, size_t)’ offset [9,
12] from the object at ‘_Bucket_default_instance_’ is out of the bounds of
referenced subobject ‘Bucket::cumulative_count_’ with type ‘unsigned int’ at
offset 4 [-Warray-bounds]

Small modifications to the source avoid the warning. For instance, replacing
the placement new

    new (ptr)::Bucket();

With a regular new avoids the warning. Similar for removing memset:

  memset(&_has_bits_, 0, sizeof(_has_bits_));

So GCC should at least be a bit more consistent about when the warning is
produced, but why is it needed? Is it invalid to initialized multiple members
with a memset? Should the protobuf compiler be disabling this warning with a
pragma around the memset?

Reply via email to