LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 35573 - Calling array new with a negative size should be checked in C++14 mode
Summary: Calling array new with a negative size should be checked in C++14 mode
Status: NEW
Alias: None
Product: clang
Classification: Unclassified
Component: C++14 (show other bugs)
Version: trunk
Hardware: All All
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-08 02:14 PST by Tim Northover
Modified: 2019-02-04 19:38 PST (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tim Northover 2017-12-08 02:14:42 PST
When emitting code for a new array expression we check whether the array size is negative in C++98 and C++11 mode, but not from C++14 onwards. E.g.

char *foo(int a) {
  return new char[a];
}

$ clang++ tmp.cpp -std=c++11 -S -o- -emit-llvm -Os
define noalias nonnull i8* @_Z3fooi(i32 %a) local_unnamed_addr #0 {
entry:
  %0 = sext i32 %a to i64
  %1 = icmp sgt i64 %0, -1
  %2 = select i1 %1, i64 %0, i64 -1
  %call = tail call i8* @_Znam(i64 %2) #2
  ret i8* %call
}

$ clang++ tmp.cpp -std=c++14 -S -o- -emit-llvm -Os
define noalias nonnull i8* @_Z3fooi(i32 %a) local_unnamed_addr #0 {
entry:
  %conv = sext i32 %a to i64
  %call = tail call i8* @_Znam(i64 %conv) #2
  ret i8* %call
}
Comment 1 Tim Northover 2017-12-08 02:17:02 PST
This is related to https://llvm.org/PR11644: we should actually be throwing std::bad_array_new_length from C++11 onwards, but we certainly shouldn't be dropping the bounds check entirely.
Comment 2 hstong 2019-02-04 19:38:07 PST
Omitting the bounds check on an non-allocating form of operator new[] means running the initialization code.

[[nodiscard]] void *operator new[](decltype(sizeof 0), void *) noexcept;
extern "C" void abort();
int *f(void *p, int sz) { return new (p) int[sz] {0, (abort(), 1)}; }