Skip to content
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

Disallow explicit cast to nt_array_ptr in checked scopes (#391) #626

Merged
merged 2 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions lib/Sema/SemaCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,19 @@ 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() && DestType->isCheckedPointerNtArrayType()) {
if (SrcType->isPointerType() && !SrcType->isCheckedPointerNtArrayType()) {
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