Bug 68710 - flexible array member of a base class accepted in a non-empty derived class
Summary: flexible array member of a base class accepted in a non-empty derived class
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: accepts-invalid
Depends on:
Blocks: flexmembers
  Show dependency tree / graph
 
Reported: 2015-12-05 00:17 UTC by Martin Sebor
Modified: 2024-11-23 03:10 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 5.1.0, 6.0
Last reconfirmed: 2015-12-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2015-12-05 00:17:46 UTC
G++ accepts definitions of non-empty derived classes whose base class contains a flexible array member.  While such base classes are valid on their own (for compatibility with C), they cannot be derived from by non-empty classes that contain data members of their own since accessing elements of the base class would make it possible to access the derived class.  The following example shows why such constructs need to be rejected.  The output shows that modifying elements of the flexible array member modifies other members of the object.  The abort implies that GCC itself assumes that flexible array members do not alias other members of the same object.

$ cat z.cpp && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -O2 -Wall -Wextra -Wpedantic -L ~/bin/gcc-5.1.0/lib64 -xc++ z.cpp && ./a.out
struct A {
    const char *a;
    const char *b[];
};

struct B: A {
    const char *c;
};

extern "C" {
    void free (void*);
    void* malloc (__SIZE_TYPE__);
    int printf (const char*, ...);
}

void foo (B *b)
{
    const char* const s = "";

    b->c = s;

    b->b [0] = 0;

    if (b->c != s)
        __builtin_abort (); 
}

int main ()
{
    B *b = (B*)malloc (sizeof *b + 2 * sizeof *b->b);
    b->a = "a";
    b->c = "c";
    b->b [0] = "b[0]";
    b->b [1] = "b[1]";
    b->b [2] = "b[2]";
    b->b [3] = "b[3]";

    printf ("{ { a = %s, b = { %s, %s, %s, %s } }, c = %s }\n",
            b->a, b->b[0], b->b[1], b->b[2], b->b[3], b->c);

    foo (b);

    free (b);
}
z.cpp:3:19: warning: ISO C++ forbids zero-size array ‘b’ [-Wpedantic]
     const char *b[];
                   ^

{ { a = a, b = { b[0], b[1], b[2], b[3] } }, c = b[0] }
Aborted (core dumped)
Comment 1 Martin Sebor 2015-12-05 00:18:29 UTC
Working on a patch.
Comment 2 Martin Sebor 2015-12-17 01:42:52 UTC
Fixed in r231665.