Skip to content

[SYCL][NFC] Fix bug with dereference null return value #7294

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

Merged
merged 1 commit into from
Nov 7, 2022

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Nov 6, 2022

Reported by static analyzer tool:

Dereference null return value
If the function actually returns a null value, a null pointer dereference will occur.

In ::SyclKernelPointerHandler::leaveArray(clang::FieldDecl *, clang::QualType, clang::QualType): Return value of function which returns null is dereferenced without checking

bool leaveArray(FieldDecl *FD, QualType ArrayTy, QualType ET) final {
QualType ModifiedArrayElement = ModifiedArrayElementsOrArray.pop_back_val();

	// returned_null: getAsConstantArrayType returns nullptr (checked 73 out of 88 times).
	// var_assigned: Assigning: CAT = nullptr return value from getAsConstantArrayType.
    const ConstantArrayType *CAT =
    SemaRef.getASTContext().getAsConstantArrayType(ArrayTy);

   // Dereference null return value (NULL_RETURNS)
   // dereference: Dereferencing a pointer that might be nullptr CAT when calling getSizeExpr.
   QualType ModifiedArray = SemaRef.getASTContext().getConstantArrayType(
      ModifiedArrayElement, CAT->getSize(),
      const_cast<Expr *>(CAT->getSizeExpr()), CAT->getSizeModifier(),
      CAT->getIndexTypeCVRQualifiers());

This patch adds assert to resolve the bug.

Signed-off-by: Soumi Manna [email protected]

Reported by static analyzer tool:

Dereference null return value
If the function actually returns a null value, a null pointer dereference will occur.

In <unnamed>::SyclKernelPointerHandler::leaveArray(clang::FieldDecl *, clang::QualType, clang::QualType): Return value of function which returns null is dereferenced without checking

bool leaveArray(FieldDecl *FD, QualType ArrayTy, QualType ET) final {
    QualType ModifiedArrayElement = ModifiedArrayElementsOrArray.pop_back_val();

    	// returned_null: getAsConstantArrayType returns nullptr (checked 73 out of 88 times).
    	// var_assigned: Assigning: CAT = nullptr return value from getAsConstantArrayType.
        const ConstantArrayType *CAT =
        SemaRef.getASTContext().getAsConstantArrayType(ArrayTy);

       // Dereference null return value (NULL_RETURNS)
       // dereference: Dereferencing a pointer that might be nullptr CAT when calling getSizeExpr.
       QualType ModifiedArray = SemaRef.getASTContext().getConstantArrayType(
          ModifiedArrayElement, CAT->getSize(),
          const_cast<Expr *>(CAT->getSizeExpr()), CAT->getSizeModifier(),
          CAT->getIndexTypeCVRQualifiers());

This patch updates the codes to resolve the bug.

Signed-off-by: Soumi Manna <[email protected]>
@smanna12 smanna12 requested a review from a team as a code owner November 6, 2022 15:16
@smanna12
Copy link
Contributor Author

smanna12 commented Nov 7, 2022

Thank you everyone for reviews!

@smanna12
Copy link
Contributor Author

smanna12 commented Nov 7, 2022

@intel/llvm-gatekeepers, This PR is ready to be merged. Thank you

@pvchupin pvchupin merged commit 823f2b2 into intel:sycl Nov 7, 2022
@smanna12 smanna12 deleted the FixBugNullDerefernce branch November 7, 2022 19:33
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.

4 participants