Skip to content

false negative on uninitialized reference (null pointer exception) in class method #41446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
beauxq opened this issue Nov 8, 2020 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@beauxq
Copy link

beauxq commented Nov 8, 2020

If a class constructor calls another method, references in that method are not checked for initialization.

TypeScript Version: 4.0.3

Search Terms: false negative null undefined used before being assigned

Code

class B {
    public foo() {}
}

class C {
    private b: B;
    constructor() {
        this.reset();
        this.b = new B();
    }

    private reset() {
        this.b.foo();  // false negative - `b` used before being assigned
    }
}

Expected behavior: error: b used before being assigned

Actual behavior: no error

Related Issues:
Note that this problem can be solved (mostly) with this: #30462
That issue talked about a false positive, but this false negative is also a problem that could be solved with the same solution.
It can't be solved completely because of possible complexities in structures.
But I ran into a null pointer exception that would have been caught by TypeScript if it just followed a simple single level of function calls.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 9, 2020
@RyanCavanaugh
Copy link
Member

This doesn't really tell us much, especially in the presence of virtual methods (which is going to be very common). Inlining one level instead of zero levels just creates "TypeScript isn't consistent about X" bugs.

@beauxq
Copy link
Author

beauxq commented Nov 9, 2020

It does not create "TypeScript isn't consistent about X" bugs.
All of those bugs are already here. It is already inconsistent about finding some "used before being assigned" bugs and not others.
If consistency is what you want, then you need to make it so you don't find any "used before being assigned" bugs.

It would not create any inconsistency. It would find more bugs.

@theonlypwner
Copy link

theonlypwner commented Apr 17, 2023

@beauxq I noticed a similar issue that has a false negative. How can we handle something like this?

Playground: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgJICFkG9kFsJgAWA9gCYAUAlAFzIBuxwpyAvgFBsIA2cAzr8gCCyYLgAOXCPnACM2NskXIARsgC8yEBADuydOSLBelBUvxEyVbMkO8AdMrvmSFSqzbtOPfnpHjJ0mCymFimigjEILxgUACuCGDEUOTKtBhuoUpZKk4ELlYA9AXICFB8hBACsSCSPhG4gRDMxLFgYe5ZzpYZJZG8xJJ2XMQA5uQADG7snlq6glS5Fq5sQA

interface IB { method(): void }

class A implements IB {
    b = new B(this)
    method() { this.b.method() } // delegation pattern
}

class B implements IB {
    constructor(b: IB) {
        b.method() // crashes unless commented out
    }
    method() { console.log(0) }
}

new A().method()

this.b is not yet assigned when A.method is called during the construction of A, but after it is constructed, subsequent calls to A.method are perfectly fine.

Here's a Java version of same thing (results in NullPointerException):

public class Main {
	interface IB { void method(); }

	static class A implements IB {
		final B b = new B(this);
		public void method() { this.b.method(); }
	}

	static class B implements IB {
		public B(IB b) { b.method(); }
		public void method() { System.out.println(0); }
	}

	public static void main(String []args){
		new A(); // crashes
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants