Move allocation size overflow handling to MemoryContextAllocExtended()? - Mailing list pgsql-hackers

From Andres Freund
Subject Move allocation size overflow handling to MemoryContextAllocExtended()?
Date
Msg-id 20161004235631.omnab3nvdpbirn4i@alap3.anarazel.de
Whole thread Raw
Responses Re: Move allocation size overflow handling to MemoryContextAllocExtended()?  (Michael Paquier <michael.paquier@gmail.com>)
List pgsql-hackers
Hi,

I was working on making sure that allocations for [1] don't overflow
size_t for large hash tables, when created on 32bit systems.  I started
to write code like

if (sizeof(SH_CONTAINS) * (uint64) tb->size) !=  sizeof(SH_CONTAINS) * (size_t) tb->size))
{   elog(ERROR, "hash table too large for a 32 bit system");
}

that could code potentially, although somewhat unlikely, be trigger on a
32bit system.


That made me wonder if it's not actually a mistake for
MemoryContextAllocExtended() size parameter to be declared
Size/size_t. That prevents it from detecting such overflows, forcing
code like the above on callsites.

Comments?

- Andres

[1] http://archives.postgresql.org/message-id/20160727004333.r3e2k2y6fvk2ntup%40alap3.anarazel.de



pgsql-hackers by date:

Previous
From: Thomas Munro
Date:
Subject: Re: Hash tables in dynamic shared memory
Next
From: Michael Paquier
Date:
Subject: Re: Move allocation size overflow handling to MemoryContextAllocExtended()?