Thread overview
[Issue 9904] New: typeof(null) can be casted to aggregate type if .sizeof equals size of pointer
Apr 08, 2013
Andrej Mitrovic
Apr 08, 2013
Andrej Mitrovic
Apr 08, 2013
Andrej Mitrovic
Jun 08, 2013
Kenji Hara
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904

           Summary: typeof(null) can be casted to aggregate type if
                    .sizeof equals size of pointer
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-08 07:42:22 PDT ---
struct S1
{
    int x;
}

struct S2
{
    int x;
    byte b;
}

void main()
{
    static assert(S1.sizeof == typeof(null).sizeof);
    auto s1 = cast(S1)null;  // compiles, should not

    static assert(S2.sizeof != typeof(null).sizeof);
    auto s2 = cast(S2)null;  // NG, as it should
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-08 07:47:34 PDT ---
(In reply to comment #0)
> struct S1
> {
>     int x;
> }
> 
> struct S2
> {
>     int x;
>     byte b;
> }

Those should really use size_t to reproduce on all systems:

> struct S1
> {
>     size_t x;
> }
> 
> struct S2
> {
>     size_t x;
>     byte b;
> }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #2 from bearophile_hugs@eml.cc 2013-04-08 10:01:36 PDT ---
(In reply to comment #1)

> Those should really use size_t to reproduce on all systems:
> 
> > struct S1
> > {
> >     size_t x;
> > }
> > 
> > struct S2
> > {
> >     size_t x;
> >     byte b;
> > }

Why is that cast a problem/bug? Maybe there is no compiler bug here.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-04-08 10:31:56 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> 
> > Those should really use size_t to reproduce on all systems:
> > 
> > > struct S1
> > > {
> > >     size_t x;
> > > }
> > > 
> > > struct S2
> > > {
> > >     size_t x;
> > >     byte b;
> > > }
> 
> Why is that cast a problem/bug? Maybe there is no compiler bug here.

It may not do what the user expects. For example they might think this is a good way to initialize the struct to its default value, but it's wrong:

import std.math;

struct S
{
    float x;
}

void main()
{
    auto s1 = S.init;
    auto s2 = cast(S)null;
    assert(isnan(s1.x));
    assert(isnan(s2.x));  // fails
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904



--- Comment #4 from github-bugzilla@puremagic.com 2013-06-07 22:19:54 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/d859a617045bdf04b39edf14bb782faff642141a
fix issue 9904 - typeof(null) can be casted to aggregate type if .sizeof
equals size of pointer

https://github.com/D-Programming-Language/dmd/commit/3c5eb896fb87b788dd0cb7631fbdf8c5cf77d9f2 Merge pull request #2148 from hpohl/9904

fix issue 9904 - typeof(null) can be casted to aggregate type if .sizeof equals
size of pointer

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9904


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2013-06-07 22:20:34 PDT ---
Merged pull request: https://github.com/D-Programming-Language/dmd/pull/2148

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------