How to use sprintf
Andrej Mitrovic
andrej.mitrovich at gmail.com
Tue Apr 26 10:34:25 PDT 2011
On 4/26/11, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> On Tue, 26 Apr 2011 13:11:20 -0400, Andrej Mitrovic
> <andrej.mitrovich at gmail.com> wrote:
>
>> On 4/26/11, Timon Gehr <timon.gehr at gmx.ch> wrote:
>>>> Isn't it dangerous to pass a D string and let the C code overwrite the
>>>> string, regardless of char* vs .ptr field?
>>>
>>> There is a possibility for buffer overflow.
>>>
>>
>> No, I mean the C function will overwrite an immutable string.
>
> D would not allow you to pass a .ptr field of an immutable string, because
> the type would be immutable(char)* and wouldn't implicitly cast to the
> required char* argument. Casting, however, would mask that problem and
> probably overwrite immutable data (and probably segfault on Linux).
> Another example of why casting is bad.
>
> for example, this should throw an error:
>
> import std.stdio;
>
> void main()
> {
> sprintf("buf".ptr, "%d", 12345);
> }
>
> -Steve
>
Yes I was refering to casting. It would segfault on Linux but probably
not on Windows, which brings us to the infamous string literal
assignment bug:
void main()
{
"hello" = "red";
string x = "hello";
assert(x == "red");
}
http://d.puremagic.com/issues/show_bug.cgi?id=4539
More information about the Digitalmars-d
mailing list