Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cf34fdb

Browse files
committed
Make AllocSetContextCreate throw an error for bad context-size parameters.
The previous behavior was to silently change them to something valid. That obscured the bugs fixed in commit ea268cd, and generally seems less useful than complaining. Unlike the previous commit, though, we'll do this in HEAD only --- it's a bit too late to be possibly breaking third-party code in 9.6. Discussion: <CA+TgmobNcELVd3QmLD3tx=w7+CokRQiC4_U0txjz=WHpfdkU=w@mail.gmail.com>
1 parent 4934062 commit cf34fdb

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/backend/utils/mmgr/aset.c

+23-14
Original file line numberDiff line numberDiff line change
@@ -445,25 +445,34 @@ AllocSetContextCreate(MemoryContext parent,
445445
{
446446
AllocSet set;
447447

448+
/*
449+
* First, validate allocation parameters. (If we're going to throw an
450+
* error, we should do so before the context is created, not after.) We
451+
* somewhat arbitrarily enforce a minimum 1K block size.
452+
*/
453+
if (initBlockSize != MAXALIGN(initBlockSize) ||
454+
initBlockSize < 1024)
455+
elog(ERROR, "invalid initBlockSize for memory context: %zu",
456+
initBlockSize);
457+
if (maxBlockSize != MAXALIGN(maxBlockSize) ||
458+
maxBlockSize < initBlockSize ||
459+
!AllocHugeSizeIsValid(maxBlockSize)) /* must be safe to double */
460+
elog(ERROR, "invalid maxBlockSize for memory context: %zu",
461+
maxBlockSize);
462+
if (minContextSize != 0 &&
463+
(minContextSize != MAXALIGN(minContextSize) ||
464+
minContextSize <= ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ))
465+
elog(ERROR, "invalid minContextSize for memory context: %zu",
466+
minContextSize);
467+
448468
/* Do the type-independent part of context creation */
449469
set = (AllocSet) MemoryContextCreate(T_AllocSetContext,
450470
sizeof(AllocSetContext),
451471
&AllocSetMethods,
452472
parent,
453473
name);
454474

455-
/*
456-
* Make sure alloc parameters are reasonable, and save them.
457-
*
458-
* We somewhat arbitrarily enforce a minimum 1K block size.
459-
*/
460-
initBlockSize = MAXALIGN(initBlockSize);
461-
if (initBlockSize < 1024)
462-
initBlockSize = 1024;
463-
maxBlockSize = MAXALIGN(maxBlockSize);
464-
if (maxBlockSize < initBlockSize)
465-
maxBlockSize = initBlockSize;
466-
Assert(AllocHugeSizeIsValid(maxBlockSize)); /* must be safe to double */
475+
/* Save allocation parameters */
467476
set->initBlockSize = initBlockSize;
468477
set->maxBlockSize = maxBlockSize;
469478
set->nextBlockSize = initBlockSize;
@@ -495,9 +504,9 @@ AllocSetContextCreate(MemoryContext parent,
495504
/*
496505
* Grab always-allocated space, if requested
497506
*/
498-
if (minContextSize > ALLOC_BLOCKHDRSZ + ALLOC_CHUNKHDRSZ)
507+
if (minContextSize > 0)
499508
{
500-
Size blksize = MAXALIGN(minContextSize);
509+
Size blksize = minContextSize;
501510
AllocBlock block;
502511

503512
block = (AllocBlock) malloc(blksize);

0 commit comments

Comments
 (0)