More evidence that memory safety is the future for programming languages

Jacob Carlborg doob at me.com
Thu Apr 2 10:04:24 UTC 2020


On Wednesday, 1 April 2020 at 21:44:27 UTC, Walter Bright wrote:

>     int* foo(int i) { return &i; }

This is detected by Clang without running the static analyzer:

$ clang main.c
main.c:1:27: warning: address of stack memory associated with 
parameter 'i' returned [-Wreturn-stack-address]
int* foo(int i) { return &i; }
                           ^
1 warning generated.

> gets detected by static analysis. This one does not:
>
>     int* bar(int* p) { return p; }
>
>     int* foo(int i) { return bar(&i); }

The Clang static analyzer detects this:

$ clang --analyze main.c
main.c:2:19: warning: Address of stack memory associated with 
local variable 'i' returned to caller
int* foo(int i) { return bar(&i); }
          ~~~~~    ^~~~~~~~~~~~~~
1 warning generated.

--
/Jacob Carlborg







More information about the Digitalmars-d mailing list