Skip to content

Objects of empty classes have size zero in generated C (memset overwrites neighbours) #610

Backlog

Description

@nv-cmeerw

Discussed in #560

Originally posted by lexx42xxel November 18, 2026

Issue Summary

With USE_EMPTY_STRUCT_IN_GENERATED_C (g++ ABI compatibility) complete objects of an empty class have size zero in the generated C although the class has size 1 in C++. Writing sizeof bytes to such an object overwrites neighbouring objects; distinct objects may share an address; address constants such as &ga[3] evaluate to &ga[0].

Reproducing Source Code

extern "C" int printf(const char *, ...);
extern "C" int strcmp(const char *, const char *);
extern "C" void *memset(void *, int, unsigned long);
struct Z {};
int main() {
  char guard[4] = "abc";
  Z z[4];
  memset(z, 0, sizeof z);          /* sizeof z == 4: must stay inside z */
  printf("guard \"%s\" (expected \"abc\")\n", guard);
  return strcmp(guard, "abc") != 0;
}

Command-line Options

eccp --c++17 --g++ --exceptions t.cpp -o t && ./t

Type of Issue

[Back End] There is a problem with the generated C code.

Additional Details

Version: EDG C/C++ front end 7.0, main at commit
6a6c7f2d6bd8027f1b0c90877b4ba222fbff6642 (2026-11-15).

Actual output:

guard "" (expected "abc")

Expected: guard "abc"

Proposed fix: declare such variables (and arrays of them) as char NAME[size] and refer to them
through a cast; by-value parameters keep size zero (as in g++) and are referenced through a local
char array; address-constant offsets into such arrays are added on a char *.

The program exits nonzero when the bug is present. A fix with a regression
test is ready (branch fix/empty-class-object-storage); a PR will reference this issue.

AddressSanitizer

The reproducer above is also detected by AddressSanitizer.

eccp --c++17 --g++ --exceptions -g --c_to_obj_option -fsanitize=address t.cpp -o t && ./t

Report on main (6a6c7f2), addresses elided:

ERROR: AddressSanitizer: stack-buffer-overflow on address 0x... at pc 0x... bp 0x... sp 0x...
    #0 0x... in memset (/lib64/libasan.so.8+0xe3a09)
    #1 0x... in main empty-class-object-storage.cpp:8
SUMMARY: AddressSanitizer: stack-buffer-overflow empty-class-object-storage.cpp:8 in main

No report with the fix, nor with g++ (-fsanitize=address).

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Reproducing (Non-standard) Configuration

No response

I acknowledge that:

  • I have checked open issues and existing discussions before filing this report.
  • The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
  • I have reduced the reproducing source code to the best of my ability.
  • I have selected at least one standard configuration or uploaded a non-standard configuration dump.
  • I understand code shared via this form is NOT confidential.
  • I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
  • I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a LICENSE.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions