Skip to content

Commit

Permalink
Fix calculation of aligned buffer size
Browse files Browse the repository at this point in the history
As is, for requested size which are already aligned, we over-allocate,
so we fix this.  We also fix the allocation for chunk size 1.

This issue has been reported by @kkmuffme.

Thanks to @iluuu1994 for improving the fix!

Closes GH-16161.
  • Loading branch information
cmb69 committed Oct 9, 2024
1 parent 4d32402 commit a1d4595
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ PHP NEWS
- DOM:
. Added Dom\Element::$outerHTML. (nielsdos)

- Output:
. Fixed calculation of aligned buffer size. (cmb)

- PDO_PGSQL:
. Added Iterable support for PDO::pgsqlCopyFromArray. (KentarouTakeda)
. Implement GH-15387 Pdo\Pgsql::setAttribute(PDO::ATTR_PREFETCH, 0) or
Expand Down
4 changes: 2 additions & 2 deletions main/php_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ typedef enum _php_output_handler_hook_t {
} php_output_handler_hook_t;

#define PHP_OUTPUT_HANDLER_INITBUF_SIZE(s) \
( ((s) > 1) ? \
(s) + PHP_OUTPUT_HANDLER_ALIGNTO_SIZE - ((s) % (PHP_OUTPUT_HANDLER_ALIGNTO_SIZE)) : \
( ((s) > 0) ? \
ZEND_MM_ALIGNED_SIZE_EX(s, PHP_OUTPUT_HANDLER_ALIGNTO_SIZE) : \
PHP_OUTPUT_HANDLER_DEFAULT_SIZE \
)
#define PHP_OUTPUT_HANDLER_ALIGNTO_SIZE 0x1000
Expand Down
27 changes: 27 additions & 0 deletions tests/output/gh16135.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-16135: output buffer over-allocated for aligned chunk sizes
--FILE--
<?php
ob_start(null, 0);
ob_start(null, 1);
ob_start(null, 2);
ob_start(null, 8191);
ob_start(null, 8192);
ob_start(null, 8193);
var_dump(array_map(fn ($s) => $s["buffer_size"], ob_get_status(true)));
?>
--EXPECT--
array(6) {
[0]=>
int(16384)
[1]=>
int(4096)
[2]=>
int(4096)
[3]=>
int(8192)
[4]=>
int(8192)
[5]=>
int(12288)
}

0 comments on commit a1d4595

Please sign in to comment.