www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Detect uninitialized class var access

reply rassoc <rassoc posteo.de> writes:
Recently I refactored some old code of mine, now utilizing classes instead
structs, and I got hit 
by an uninitialized variable access pattern similar to the simplified example
below.

How can we easily spot this? What other switches and safeguards are there?

 dmd -dip1000 -debug -g -w
just crashes with `Error: program killed by signal 11` on linux and nothing else. ```d import std; safe: class Foo { int x; } void main() { Foo f; // = new Foo; writeln(f.x); } ```
Sep 24 2022
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
 just crashes with `Error: program killed by signal 11` on linux 
 and nothing else.
gdb --args ./your_program and then it will tell you all the details you want to know about when this happens. strangly sometimes adding the -O option will cause dmd to catch simple cases at compile time but gdb is the general solution to track these down.
Sep 24 2022
parent reply rassoc <rassoc posteo.de> writes:
On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:
 gdb --args ./your_program
 
 and then it will tell you all the details you want to know about when this
happens.
Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( D's competition got stuff like this right: ```
 crystal build foo.cr
In foo.cr:6:6 6 | puts f.x ^ Error: read before assignment to local variable 'f' ``` [1] https://issues.dlang.org/show_bug.cgi?id=4595
Sep 24 2022
parent reply Tejas <notrealemail gmail.com> writes:
On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:
 On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:
 gdb --args ./your_program
 
 and then it will tell you all the details you want to know 
 about when this happens.
Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( D's competition got stuff like this right: ```
 crystal build foo.cr
In foo.cr:6:6 6 | puts f.x ^ Error: read before assignment to local variable 'f' ``` [1] https://issues.dlang.org/show_bug.cgi?id=4595
AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmd
Sep 24 2022
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 25 September 2022 at 03:04:45 UTC, Tejas wrote:
 On Saturday, 24 September 2022 at 23:04:00 UTC, rassoc wrote:
 On 9/24/22 15:28, Adam D Ruppe via Digitalmars-d-learn wrote:
 gdb --args ./your_program
 
 and then it will tell you all the details you want to know 
 about when this happens.
Thank you for your input, Adam. Real shame that there's no built-in compiler solution and probably never will be according to [1]. :( [1] https://issues.dlang.org/show_bug.cgi?id=4595
AFAIK diagnostics like that are not possible because Walter doesn't want to implement full blown dataflow analysis in the compiler since it will slow down the compilation speed of dmd
Implementing such a diagnostic is equivalent, in the general case, to solving the halting problem. So even with dataflow analysis, there would always be cases where the compiler either fails to detect a potential null dereference (false negative), or detects one that can't actually happen at runtime (false positive).
Sep 25 2022
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
 Recently I refactored some old code of mine, now utilizing 
 classes instead structs, and I got hit by an uninitialized 
 variable access pattern similar to the simplified example below.
I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀 SDB 79
Sep 24 2022
parent reply wjoe <invalid example.com> writes:
On Sunday, 25 September 2022 at 02:10:00 UTC, Salih Dincer wrote:
 On Saturday, 24 September 2022 at 13:17:19 UTC, rassoc wrote:
 Recently I refactored some old code of mine, now utilizing 
 classes instead structs, and I got hit by an uninitialized 
 variable access pattern similar to the simplified example 
 below.
I think changing the structure will make things harder (I mean in general). I know most people will object; but classes are useless. Structs are enough for you for the rest of your life 😀 SDB 79
How would you do this without classes: ``` D interface ForwardRange { // ... } class Foo: ForwardRange { // ... } class Bar: ForwardRange { // ... } struct Baz { ForwardRange r; this(ForwardRange r_) { r = r_; } } ```
Sep 26 2022
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
Currently in D you would be forced to create a vtable struct manually.

But if we had something like signatures you could do this:

```d
struct Foo {
	//...
}

struct Bar {
	InputRange input;
}

void doIt() {
	Bar bar;
	Foo* foo = new Foo;
	bar.input = foo;
}
```

Classes are not the only way to do OOP :)
Sep 26 2022
parent wjoe <invalid example.com> writes:
On Monday, 26 September 2022 at 16:51:11 UTC, rikki cattermole 
wrote:
 Currently in D you would be forced to create a vtable struct 
 manually.
Or I could just use classes. The cure shouldn't be worse than the disease.
 But if we had something like signatures you could do this:

 ```d
 struct Foo {
 	//...
 }

 struct Bar {
 	InputRange input;
 }

 void doIt() {
 	Bar bar;
 	Foo* foo = new Foo;
 	bar.input = foo;
 }
 ```
This would be nice but since we don't it doesn't really matter. unfortunately.
Sep 26 2022