Skip to content

WI #2306 Protect against NullReferenceException when reading NameLiteral property #2801

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

fm-117
Copy link
Contributor

@fm-117 fm-117 commented Aug 18, 2025

Fixes #2306

The NullReferenceException regularly observed in the AddError method when checking REDEFINES may be caused by:

  • either the NameLiteral being null
  • or the Token of the NameLiteral being null.

This PR aims at fixing unsafe uses of SymbolInformation.NameLiteral.

Comment on lines +243 to 255
var dataName = specialRegister.DataDescriptionEntry?.DataName;
var dataNameToken = dataName?.NameLiteral?.Token;
if (dataNameToken != null)
{
CobolWordsBuilder.symbolInformationForTokens[dataNameToken] = dataName;
}
if (specialRegister.SymbolReference != null) {
CobolWordsBuilder.symbolInformationForTokens[specialRegister.SymbolReference.NameLiteral.Token] = specialRegister.SymbolReference;

var specialRegisterReference = specialRegister.SymbolReference;
var specialRegisterReferenceToken = specialRegisterReference?.NameLiteral?.Token;
if (specialRegisterReferenceToken != null)
{
CobolWordsBuilder.symbolInformationForTokens[specialRegisterReferenceToken] = specialRegisterReference;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated 3 times.
It could be factorized in a methode AddToSymbolInformation with this code:

        private void AddToSymbolInformation(SymbolDefinition specialRegisterName, SymbolReference specialRegisterReference)
        {
            var specialRegisterNameToken = specialRegisterName?.NameLiteral?.Token;
            if (specialRegisterNameToken != null)
            {
                CobolWordsBuilder.symbolInformationForTokens[specialRegisterNameToken] = specialRegisterName;
            }

            var specialRegisterReferenceToken = specialRegisterReference?.NameLiteral?.Token;
            if (specialRegisterReferenceToken != null)
            {
                CobolWordsBuilder.symbolInformationForTokens[specialRegisterReferenceToken] = specialRegisterReference;
            }
        }

Calll it like this;
AddToSymbolInformation(specialRegister.DataDescriptionEntry?.DataName, specialRegister.SymbolReference);

Comment on lines +810 to +816

var token = reference.NameLiteral?.Token;
if (token != null)
{
symbolInformationForTokens[token] = reference;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated 4 times. Similar factorization with method:

        private void AddToSymbolInformation(SymbolReference reference)
        {
            var token = reference.NameLiteral?.Token;
            if (token != null)
            {
                symbolInformationForTokens[token] = reference;
            }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NullPointerException in DiagnosticUtils.AddError(Node node, String message, SymbolReference symbol, MessageCode code)
2 participants