Search code examples
c#klocwork

klocwork error - call to function 'GetTokenResponseAsync' may be null and will be dereferenced


I am getting Klocwork error,

Reference 'this.GetTokenResponseAsync(cancellationToken)' returned from call to function 'GetTokenResponseAsync' at line 101 may be null and will be dereferenced at line 101

and here is code,

public async Task<SecurityToken> AcquireTokenAsync(CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();

        var tokenResponse = await GetTokenResponseAsync(cancellationToken).ConfigureAwait(false);

        return tokenResponse;
    }

Is this mean tokenResponse can be null? how to fix this?


Solution

  • You could probably avoid this error by initialising tokenResponse to a value such as 0 on the line above its assignment to await GetTokenResponseAsync(cancellationToken).ConfigureAwait(false).

    As it's assigned to a value that appears to be on an asynchronous thread it can't be guaranteed that it will ever hold a value.