Fear of Compiler Magic

Walter Bright newshound2 at digitalmars.com
Sat Aug 3 17:02:55 UTC 2024


On 8/2/2024 2:29 AM, Dennis wrote:
> You would think it's safe to transform this:
> ```D
> printf("x = %s\n", x);
> printf("x = %s\n", x);
> ```
> 
> Into this:
> ```D
> const(char)* fmt = "x = %s\n";
> printf(fmt, x);
> printf(fmt, x);
> ```
> 
> But with magic printf format string rewrites, that transformation turns correct 
> code into memory corrupting code when x is an int.

The transformation won't compile if the call is marked @safe, and won't compile 
with the various proposals to increase the default safety-ness.

It is in the same box as:

```
int[] array;
x = array[5];
```

and rewriting as:

```
int[] array;
x = *(array.ptr + 5);
```


More information about the Digitalmars-d mailing list