Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -9867,6 +9867,10 @@ def err_bounds_type_annotation_lost_checking : Error<
def err_checked_scope_no_assume_bounds_casting : Error<
"_Assume_bounds_cast not allowed in a checked scope or function">;

def err_checked_scope_no_cast_to_nt_array_ptr : Error<
"%0 cannot be cast to %1 in a checked scope because "
"%0 might not point to a null-terminated array">;

def err_checked_on_non_function : Error<
"%select{'_Unchecked'|'_Checked _Bounds_only|'_Checked'}0 "
"can only appear on functions">;
Expand Down
16 changes: 16 additions & 0 deletions lib/Sema/SemaCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,22 @@ void CastOperation::CheckCStyleCast(bool IsCheckedScope) {
SrcExpr = ExprError();
return;
}

// Disallow cast from other Checked Pointer types to nt_arary_ptr because
// the SrcType might not point to a NULL-terminated array.
if (DestType->isPointerType()) {
if (cast<PointerType>(DestType)->getKind() == CheckedPointerKind::NtArray) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you use the function isCheckedPointerNtArrayType for testing whether a type is or is not an nt_array_ptr type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the code as suggested.

if (SrcType->isPointerType() &&
cast<PointerType>(SrcType)->getKind() != CheckedPointerKind::NtArray) {
Self.Diag(SrcExpr.get()->getExprLoc(),
diag::err_checked_scope_no_cast_to_nt_array_ptr)
<< SrcType << DestType << SrcExpr.get()->getSourceRange();
SrcExpr = ExprError();
return;
}
}
}

}

DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
Expand Down