Bug 11778 – format for null does not verify fmt flags.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-19T12:42:00Z
Last change time
2014-04-07T03:44:06Z
Keywords
pull
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2013-12-19T12:42:54Z
Printing pointer types is supposed to only work with %X, %x or %s.
However, if the passed pointer is null, there is no actual fmt validation:
//----
import std.stdio;
void main()
{
int* p;
writefln(s, p);
writefln(s, p + 1);
}
//----
null
std.format.FormatException@std/format.d(2957): Expected one of %s, %x or %X for pointer type.
//----
This can be an issue, as an invalid format flag may end up throwing depending on the *value* passed (and not the type). The bug may only reveal itself unexpectedly at a later date.
Comment #1 by andrej.mitrovich — 2013-12-20T06:39:59Z
Where is 's' defined?
Comment #2 by andrej.mitrovich — 2013-12-20T06:41:48Z
I guess this could be a test-case:
-----
import std.stdio;
void main()
{
string s = "%d";
int* p;
writefln(s, p); // does not throw
writefln(s, p + 1);
}
-----
Comment #3 by monarchdodra — 2013-12-20T07:34:20Z
(In reply to comment #1)
> Where is 's' defined?
Hum... I forgot to copy paste it. Much apologies.
I had defined it as:
enum s = "%d"
(In reply to comment #2)
> I guess this could be a test-case:
>
> -----
> import std.stdio;
> void main()
> {
> string s = "%d";
> int* p;
> writefln(s, p); // does not throw
> writefln(s, p + 1);
> }
> -----
Yes, exactly.