[llvm-bugs] [Bug 49860] New: allocation function should not be called for erroneous expression

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Apr 6 04:57:04 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=49860

            Bug ID: 49860
           Summary: allocation function should not be called for erroneous
                    expression
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++17
          Assignee: unassignedclangbugs at nondot.org
          Reporter: zilla at kayari.org
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

namespace std {
  using size_t = decltype(sizeof(0));
}

extern "C" void abort();
extern "C" int puts(const char*);

struct X
{
  void* operator new[](std::size_t) noexcept {
    puts("should not be here");
    abort();
    return nullptr;
  }

  int data;
};

int main()
{
  int n = -1;
  auto p = new X[n];
  if (p)
    abort();
}

This terminates with:

should not be here
Aborted (core dumped)

The allocation function should not be called, because the expression -1 is
erroneous. In C++14 the new-expression should throw std::bad_array_new_length,
in C++17 it should yield (X*)0 because the allocation function is non-throwing.
But in both cases the allocation function should not be called.

It doesn't seem to matter whether the expression is a non-class type less than
zero, or a class type that converts to an integer less than zero, or whether
it's a value is such that the size of the allocated object would be larger than
the entire address space. In all three cases the erroneous value is not
detected and the allocation function is called (except when the erroneous value
is a core constant expression, which gets detected since the fix for Bug
22845).

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210406/c7d34976/attachment-0001.html>


More information about the llvm-bugs mailing list