Move allocation size overflow handling to MemoryContextAllocExtended()?

From: Andres Freund <andres(at)anarazel(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Move allocation size overflow handling to MemoryContextAllocExtended()?
Date: 2016-10-04 23:56:31
Message-ID: 20161004235631.omnab3nvdpbirn4i@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2016-10-05 00:38:15 Re: Move allocation size overflow handling to MemoryContextAllocExtended()?
Previous Message Thomas Munro 2016-10-04 23:23:12 Re: Hash tables in dynamic shared memory