www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3805] New: std.format writeUpToFormatSpec function has subtle loop index bug, will drop character after a %%

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

           Summary: std.format writeUpToFormatSpec function has subtle
                    loop index bug, will drop character after a %%
           Product: D
           Version: 2.040
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: y0uf00bar gmail.com



---
private void writeUpToFormatSpec(OutRange, S)(ref OutRange w, ref S fmt)
{
    for (size_t i = 0; i < fmt.length; ++i)
    {
        if (fmt[i] != '%') continue;
        if (fmt[++i] != '%')
        {
            // spec found, print and bailout
            w.put(fmt[0 .. i - 1]);
            fmt = fmt[i .. $];
            return;
        }
        // doubled! Now print whatever we had, then update the string
        // and move on
        w.put(fmt[0 .. i]);
        fmt = fmt[i + 1 .. $];
/// BUG !!  unable to ever reset a size_t for loop index value to zero, because
it is always incremented to 1 at start of next iteration.  This means next
character after a '%%' will be dropped.
        i = 0;  
    }

/// A better version might look like this.
/// Take the i++ out of the for loop top, move it to the first test case.

   for (size_t i = 0; i < fmt.length;)
    {
        if (fmt[i++] != '%') continue;
        if (fmt[i] != '%')
        {
            // spec found, print and bailout
            w.put(fmt[0 .. i - 1]);
            fmt = fmt[i .. $];
            return;
        }
        // doubled! Now print whatever we had, then update the string
        // and move on
        w.put(fmt[0 .. i]);
        fmt = fmt[i + 1 .. $];
        i = 0; // OK now, will be 0 at the top of the loop
    }

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


Andrei Alexandrescu <andrei metalanguage.com> changed:

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



21:54:21 PST ---
Awesome, thank you. It is very subtle indeed.

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |DUPLICATE



I think this is same as bug 4775, and it was already fixed.

*** This issue has been marked as a duplicate of issue 4775 ***

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