Thread overview
[Issue 2383] New: default arguments can implicitly access private global variables that are not visible at call site
Oct 01, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 02, 2008
d-bugmail
Oct 16, 2008
d-bugmail
Oct 21, 2008
d-bugmail
October 01, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383

           Summary: default arguments can implicitly access private global
                    variables that are not visible at call site
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: accepts-invalid, spec
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: tomas@famolsen.dk


Consider the following testcase:

------------------------
1st module:

module privateconst1;

extern(C) int rand();

private const int abc;

static this() {
  abc = rand();
}

void foo(int i = abc) {}


------------------------
2nd module:

module privateconst2;

import privateconst1;

void main()
{
  foo();
}


------
this is accepted by dmd and works. however this makes it impossible for another compiler to mark 'abc' as internal (or in C terms, static) disabling a lot of valuable optimizations.

in the 2nd module the call to 'foo' has to load the value of 'abc' which is private and should not be accessible.

I can't find anywhere in the spec where it says this is allowed, hence marking both 'spec' and 'accepts-invalid'


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #1 from bugzilla@digitalmars.com  2008-10-02 03:49 -------
It's legal because the default parameter value is evaluated in the context where it appears, and so has access to private values where it appears. I'll clarify the documentation.


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #2 from matti.niemenmaa+dbugzilla@iki.fi  2008-10-02 04:39 -------
"The default parameter value is evaluated in the context where it appears" - should I reopen Issue 191, then?


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #3 from tomas@famolsen.dk  2008-10-02 05:24 -------
how can I detect this and still make private globals internal to the module in the normal case ?


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #4 from tomas@famolsen.dk  2008-10-02 05:25 -------
I mean in terms of DMD...


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #5 from matti.niemenmaa+dbugzilla@iki.fi  2008-10-02 05:36 -------
Isn't the case just the same as the following? (I wish default args were always semantically equivalent to this, but oh well...)

private const int abc;
void foo() { foo(abc); }
void foo(int i) {}

That shouldn't require making abc non-private, or?


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #6 from tomas@famolsen.dk  2008-10-02 05:42 -------
reason I'm asking this is that DMD puts a the private global value in the AST at the call site. thus I can't make this specific global internal and I can't find any information to tell me that this specific global does not follow usual symbol visibility rules.


-- 

October 02, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #7 from bugzilla@digitalmars.com  2008-10-02 17:14 -------
(In reply to comment #2)
> "The default parameter value is evaluated in the context where it appears" - should I reopen Issue 191, then?

No, because a 'this' pointer would still be needed.


-- 

October 16, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383





------- Comment #8 from tomas@famolsen.dk  2008-10-16 07:25 -------
*bump* I could really use some hints on how to fix this in LDC .. Walter? How do you do it in DMD?

As I've already said, the callsite references the global variable, so I need some way to figure out that this in fact is not a real private.


-- 

October 21, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2383


bugzilla@digitalmars.com changed:

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




------- Comment #9 from bugzilla@digitalmars.com  2008-10-20 22:20 -------
Fixed dmd 1.036 and 2.020


--