This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Integer overflow in operator new. Solved?


#include <stddef.h>

void *__allocate_array_OptionA(size_t num, size_t size) { // 1st best
  unsigned long long tmp = (unsigned long long)size * num;
  if (tmp >= 0x0000000080000000ULL) tmp=~size_t(0);
  return operator new[](tmp);
}

void *__allocate_array_OptionB(size_t num, size_t size) { // 2nd best
  unsigned long long tmp = (unsigned long long)size * num;
  if (tmp >= 0x0000000080000000ULL) return(operator new[](~size_t(0)));
  return operator new[](tmp);
}

-----------------------------------------------------------------------------

_Z24__allocate_array_OptionAjj:
[ gcc 4.1.3 20070326 (prerelease) : 9 instructions ]
	movl	8(%esp), %eax
	mull	4(%esp)
	cmpl	$0, %edx
	ja	.L11
	cmpl	$2147483647, %eax
	jbe	.L9
.L11:
	orl	$-1, %eax
.L9:
	movl	%eax, 4(%esp)
	jmp	_Znaj

_Z24__allocate_array_OptionBjj:
[ gcc 4.1.3 20070326 (prerelease) : 10 instructions ]
	movl	8(%esp), %eax
	mull	4(%esp)
	cmpl	$0, %edx
	ja	.L4
	cmpl	$2147483647, %eax
	jbe	.L2
.L4:
	movl	$-1, 4(%esp)
	jmp	.L7            # <- why not jmp _Znaj directly?
.L2:
	movl	%eax, 4(%esp)
.L7:
	jmp	_Znaj

-----------------------------------------------------------------------------

It seems to be solved the integer overflow in operator new.

J.C. Pizarro.

Attachment: allocate_array_longmult_april2007.tar.gz
Description: GNU Zip compressed data


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]