Re: Allocations that overflow



On Thu, 19 Jun 2008 00:24:57 +0300, "Angel Tsankov"
<fn42551@xxxxxxxxxxxxxxxx> wrote:

Hello,

According to the standard, what must the following function do if it is
passed std::numeric_limits<std::size_t>::max()?

struct S
{
char a[64]; // Any size greater than 1 would do.
};

S* allocate(std::size_t size)
{
return new S[size];
}


I can't find any prescribed behavior for this in the standard. However, VC9
does detect overflow for the multiplication of the count and object size.
Consider the following fragment:

int* f(size_t n)
{
return new int[n];
}

Compiled with cl -O2 -EHs -FAs -c a.cpp, I get:

?f@@YAPAHI@Z PROC ; f, COMDAT

; 3 : return new int[n];

mov eax, DWORD PTR _n$[esp-4]
xor ecx, ecx
mov edx, 4
mul edx
seto cl
neg ecx
or ecx, eax
mov DWORD PTR _n$[esp-4], ecx
jmp ??2@YAPAXI@Z ; operator new
?f@@YAPAHI@Z ENDP ; f

If overflow occurs, the argument to the operator new function will be
size_t(-1), i.e. all bits set, and operator new will detect this error.
(The seto/neg/or sequence does this.) Here's a full program to test it:

#include <stdio.h>
#include <stdexcept>

int* f(size_t n)
{
return new int[n];
}

int main()
{
try
{
f(size_t(-1));
}
catch (std::bad_alloc)
{
puts("1");
}
}

X>cl -O2 -EHs -W4 a.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for
80x86
Copyright (C) Microsoft Corporation. All rights reserved.

a.cpp
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

/out:a.exe
a.obj

X>a
1

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: Plauger, size_t and ptrdiff_t
    ... Plauger's "The Standard C Library," where he states "... ... This language would not rule out one being int ... we explicitly decided to allow some pointer differences to be ... BTW, even when you do get a ptrdiff_t overflow, on a ...
    (comp.lang.c)
  • Re: Exceeding limits during arithmetic
    ... Is there any standard (or even non-standard) way to detect limit ... overflow in arithmetic in C? ... int a=2147483647; ...
    (comp.lang.c)
  • Re: Portability: Harmony between PC and microcontroller
    ... int is the natural integer type for the system. ... You are, perhaps unintentionally, paraphrasing the standard in a way ... One of the things that you might not realize is that the C programming ... In the real world, most embedded systems have more complex jobs to do, ...
    (comp.lang.c)
  • Re: Bit-fields and integral promotion
    ... > un/signed type knowledge. ... unsigned char, the int-sized object must be treated as signed. ... Admittedly it is unfortunate that the standard does not specifically ... int but an 8-bit unsigned bit field promoted to an unsigned int. ...
    (comp.lang.c)
  • Re: call of variadic function
    ... arguments that should be passed to this function are of type int. ... You call foo with more arguments than are ... which is the standard way to access arguments of a variadic function ... Here the else clause of my sentence specifies one of the numerous non- ...
    (comp.lang.c)