www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2479] New: sformat is broken

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479

           Summary: sformat is broken
           Product: D
           Version: 2.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: dick221z yahoo.com


char [32] tmp;
sformat(tmp,"hi");
writefln(tmp);

tmp is null after calling sformat. In some other cases an
EXCEPTION_ACCESS_VIOLATION is thrown.

Oddly enough format works fine

The problem seems to be in passing the variable argument list to sformat, the
argptr passed to sformat is null.

Workaround is to go back to 2.021


-- 
Nov 29 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Gide Nwawudu <gide nwawudu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |gide nwawudu.com
           Severity|major                       |regression





Set importance to regression.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 20 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |braddr puremagic.com





PDT ---
A reduced test case (larger, but complete and doesn't import large swaths of
code).  This is a massive reduction of all the irrelevant parts of doFormat
torn away, and sformat reduced a good bit as well.

module bug2479;

import std.stdarg;

void doFormat(void delegate(dchar) myputc, TypeInfo[] arguments, va_list
argptr)
{
    string fmt = va_arg!(string)(argptr);
    assert(!(fmt is null)); // fires when it shouldn't.
}

char[] sformat(char[] s, ...)
{
    void putc(dchar c) { s[0] = cast(char)c; }

    doFormat(&putc, _arguments, _argptr);
    return s[0 .. 1];
}

void main()
{
    char[32] tmp;
    sformat(tmp, "%s", "hi");
}

Changing putc to not touch 's' causes the va_arg call to succeed an fmt is no
longer null.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 29 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479






PDT ---
Adding a printf at the top of doFormat...

The broken case:
  argptr = 0xf7ceeffc
  core.exception.AssertError bug2479.d(11): Assertion failure

With putc emptied out:
  argptr = 0xffb004d4
  (no assert)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 29 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au





Even further reduced, removing all imports. My patch to bug 814 does not fix
this.
------
alias void* va_list;

T va_arg(T)(inout va_list _argptr) {
    T arg = *cast(T*)_argptr;
    _argptr = _argptr + ((T.sizeof + int.sizeof - 1) & ~(int.sizeof - 1));
    return arg;
}

void doFormat(void delegate(dchar) myputc, TypeInfo[] args, va_list
argptr){
    string fmt = va_arg!(string)(argptr);
    assert(!(fmt is null)); // fires when it shouldn't.
}

char[] sformat(char[] s, ...){
    void putc(dchar c) { s[0] = cast(char)c; }

    doFormat(&putc, _arguments, _argptr);
    return s[0 .. 1];
}

void main(){
    char[32] tmp;
    sformat(tmp, "%s", "hi");
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 31 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 11 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|Phobos                      |DMD
            Summary|sformat is broken           |Badcode regression: closure
                   |                            |corrupts _argptr value



Reduced test case. This does not seem to be a Phobos bug.
If you remove any reference to s from the delegate (so that it stops being a 
closure), the code works correctly.
----
void doFormat(void delegate() myputc, void * argptr){
    assert(!(*cast(string*)argptr is null));
}

void sformat(string s, ...){
    void putc() { assert(s[0]==s[0]); }
    doFormat(&putc, _argptr);
}

void main(){
    sformat("xxx", "hi");
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |NEW


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479




I have patched Phobos so that original symptom is fixed.
The compiler bug is that in FuncDeclaration::buildClosure() in toir.c, variadic
arguments aren't supported, but _argptr is set in FuncDeclaration::semantic3()
as if they were.

Partial patch, to turn this from a wrong-code into a rejects-valid bug, until
the bug is actually fixed:
func.c, line 1282.
---
        if (argptr)
        {    // Initialize _argptr to point past non-variadic arg
#if IN_GCC
        // Handled in FuncDeclaration::toObjFile
        v_argptr = argptr;
        v_argptr->init = new VoidInitializer(loc);
#else
+        // BUG: Fix this in FuncDeclaration::buildClosure() in toir.c.
+        if (needsClosure())
+            error("Closures are not yet supported with variadic arguments
(Bugzilla 2479)");
        Type *t = argptr->type;
        VarDeclaration *p;
        unsigned offset;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Leandro Lucarella <llucax gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |llucax gmail.com



PST ---
SVN commit (workarround in Phobos, not a real fix):
http://www.dsource.org/projects/phobos/changeset/1318

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



20:23:50 PST ---
I'll add a message for now, dmd svn 239.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 04 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479




PST ---
Direct link to the SVN commit:
http://www.dsource.org/projects/dmd/changeset/239

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 05 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |rejects-valid
         AssignedTo|andrei metalanguage.com     |nobody puremagic.com
            Summary|Badcode regression: closure |Regression: cannot use
                   |corrupts _argptr value      |variadic arguments inside a
                   |                            |closure



Was: Badcode regression: closure corrupts _argptr value
In DMD2.036, this is now just a rejects-valid rather than wrong-code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 10 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2479


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Regression: cannot use      |Cannot use variadic
                   |variadic arguments inside a |arguments inside a closure
                   |closure                     |
           Severity|regression                  |normal



Removing the 'regression' keyword, since variadic arguments in closures have
never worked.

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