Skip to content

Commit ea8ef2c

Browse files
author
Mandeep Singh Grang
authored
An nt_checked array with an empty initializer list should be an error (#1121)
Consider the following declaration of an nt_checked array: char p nt_checked[] = {} According to the Checked C spec section 2.4: 1. nt_checked declares an array whose last element is a null terminator. The size of the array includes the null terminator element. 2. An nt_checked array with size d converts to an nt_array_ptr with a count of d - 1 elements. So it should be illegal to declare an nt_checked array with an empty initializer list. This fixes issue #1120.
1 parent c6edde1 commit ea8ef2c

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ bool InitListExpr::isNullTerminated(ASTContext &C, unsigned DeclArraySize) const
22602260
"sub-objects are made explicit");
22612261

22622262
if (getNumInits() == 0) {
2263-
return true;
2263+
return false;
22642264
}
22652265

22662266
if (getNumInits() == 1 && getInit(0))

clang/lib/Sema/SemaDecl.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13023,10 +13023,17 @@ bool Sema::ValidateNTCheckedType(ASTContext &Ctx, QualType VDeclType,
1302313023
}
1302413024
} else if (InitListExpr *E = dyn_cast<InitListExpr>(Init)) {
1302513025
if (!E->isNullTerminated(Ctx, *DeclaredArraySize)) {
13026-
const Expr *LastItem = E->getInit(E->getNumInits() - 1);
13027-
Diag(LastItem->getBeginLoc(),
13028-
diag::err_initializer_not_null_terminated_for_nt_checked)
13029-
<< LastItem->getSourceRange();
13026+
if (E->getNumInits() == 0) {
13027+
Diag(Init->getBeginLoc(),
13028+
diag::err_initializer_not_null_terminated_for_nt_checked)
13029+
<< Init->getSourceRange();
13030+
13031+
} else {
13032+
const Expr *LastItem = E->getInit(E->getNumInits() - 1);
13033+
Diag(LastItem->getBeginLoc(),
13034+
diag::err_initializer_not_null_terminated_for_nt_checked)
13035+
<< LastItem->getSourceRange();
13036+
}
1303013037
return false;
1303113038
}
1303213039
}

0 commit comments

Comments
 (0)