Skip to content

Commit

Permalink
[SYCL][NFC] Fix bug with dereference null return value (#7294)
Browse files Browse the repository at this point in the history
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 adds assert to resolve the bug.

Signed-off-by: Soumi Manna <soumi.manna@intel.com>

Signed-off-by: Soumi Manna <soumi.manna@intel.com>
  • Loading branch information
smanna12 authored Nov 7, 2022
1 parent 998fd91 commit 823f2b2
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,7 @@ class SyclKernelPointerHandler : public SyclKernelFieldHandler {

const ConstantArrayType *CAT =
SemaRef.getASTContext().getAsConstantArrayType(ArrayTy);
assert(CAT && "Should only be called on constant-size array.");
QualType ModifiedArray = SemaRef.getASTContext().getConstantArrayType(
ModifiedArrayElement, CAT->getSize(),
const_cast<Expr *>(CAT->getSizeExpr()), CAT->getSizeModifier(),
Expand Down

0 comments on commit 823f2b2

Please sign in to comment.