Skip to content

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

Closed

Description

@beauxq

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.

Activity

  1. RyanCavanaugh commented on Nov 9, 2020

    @RyanCavanaugh
    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.

  2. beauxq commented on Nov 9, 2020

    @beauxq
    Author

    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.

  3. theonlypwner commented on Apr 17, 2023

    @theonlypwner

    Doug Hoskisson (@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions