Bug 3805 – std.format writeUpToFormatSpec function has subtle loop index bug, will drop character after a %%
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-02-15T20:32:00Z
Last change time
2015-06-09T01:27:40Z
Assigned to
andrei
Creator
y0uf00bar
Comments
Comment #0 by y0uf00bar — 2010-02-15T20:32:00Z
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
}
Comment #1 by andrei — 2010-02-15T21:54:21Z
Awesome, thank you. It is very subtle indeed.
Comment #2 by k.hara.pg — 2011-09-10T05:20:12Z
I think this is same as bug 4775, and it was already fixed.
*** This issue has been marked as a duplicate of issue 4775 ***