public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99934] New: bad_array_new_length thrown when non-throwing allocation function would have been used
@ 2021-04-06 11:33 redi at gcc dot gnu.org
  2021-04-06 12:41 ` [Bug c++/99934] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-04-06 11:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99934

            Bug ID: 99934
           Summary: bad_array_new_length thrown when non-throwing
                    allocation function would have been used
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

Reduced from Bug 99845 comment 11:

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:

terminate called after throwing an instance of 'std::bad_array_new_length'
  what():  std::bad_array_new_length
Aborted (core dumped)

The new-expression is erroneous (it has non-class type and its value before
converting to std::size_t is less than zero), the allocation function that
would be called is non-throwing, therefore the value of the new-expression
should be (X*)nullptr instead of throwing bad_array_new_length. This was
changed by https://wg21.link/cwg1992

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-22 16:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06 11:33 [Bug c++/99934] New: bad_array_new_length thrown when non-throwing allocation function would have been used redi at gcc dot gnu.org
2021-04-06 12:41 ` [Bug c++/99934] " jakub at gcc dot gnu.org
2022-03-28  9:17 ` redi at gcc dot gnu.org
2023-03-20  9:52 ` redi at gcc dot gnu.org
2023-03-20 10:08 ` redi at gcc dot gnu.org
2023-03-20 10:23 ` redi at gcc dot gnu.org
2024-04-22 16:14 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).