Skip to content

Commit e8e9810

Browse files
committed
Fixed bug #39652 (Wrong negative results from memeory_get_usage())
1 parent 5ea32dc commit e8e9810

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ PHP NEWS
5555
after closeCursor()). (Ilia, Tony)
5656
- Fixed bug #39653 (ext/dba doesn't check for db-4.5 and db-4.4 when db4
5757
support is enabled). (Tony)
58+
- Fixed bug #39652 (Wrong negative results from memeory_get_usage()). (Dmitry)
5859
- Fixed bug #39648 (Implementation of PHP functions chown() and chgrp() are not
5960
thread safe). (Ilia, wharmby at uk dot ibm dot com)
6061
- Fixed bug #39623 (thread safety fixes on *nix for putenv() & mime_magic).

Zend/zend_alloc.c

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,8 @@ static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
11741174
size_t remaining_size = block_size - true_size;
11751175

11761176
if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
1177-
ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, block_size);
1177+
true_size = block_size;
1178+
ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
11781179
} else {
11791180
zend_mm_free_block *new_free_block;
11801181

@@ -1252,7 +1253,8 @@ static void *_zend_mm_alloc_int(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_D
12521253
ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(best_fit, block_size));
12531254

12541255
if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
1255-
ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, block_size);
1256+
true_size = block_size;
1257+
ZEND_MM_BLOCK(best_fit, ZEND_MM_USED_BLOCK, true_size);
12561258
} else {
12571259
zend_mm_free_block *new_free_block;
12581260

@@ -1391,23 +1393,18 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
13911393
zend_mm_block *mm_block = ZEND_MM_HEADER_OF(p);
13921394
zend_mm_block *next_block;
13931395
size_t true_size;
1396+
size_t orig_size;
13941397
void *ptr;
13951398

13961399
if (!p || !ZEND_MM_VALID_PTR(p)) {
13971400
return _zend_mm_alloc_int(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
13981401
}
13991402
mm_block = ZEND_MM_HEADER_OF(p);
14001403
true_size = ZEND_MM_TRUE_SIZE(size);
1401-
1402-
#if MEMORY_LIMIT
1403-
heap->size = heap->size + true_size - ZEND_MM_BLOCK_SIZE(mm_block);
1404-
if (heap->peak < heap->size) {
1405-
heap->peak = heap->size;
1406-
}
1407-
#endif
1404+
orig_size = ZEND_MM_BLOCK_SIZE(mm_block);
14081405

1409-
if (true_size <= ZEND_MM_BLOCK_SIZE(mm_block)) {
1410-
size_t remaining_size = ZEND_MM_BLOCK_SIZE(mm_block) - true_size;
1406+
if (true_size <= orig_size) {
1407+
size_t remaining_size = orig_size - true_size;
14111408

14121409
if (remaining_size >= ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
14131410
zend_mm_free_block *new_free_block;
@@ -1427,6 +1424,9 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
14271424

14281425
/* add the new free block to the free list */
14291426
zend_mm_add_to_free_list(heap, new_free_block);
1427+
#if MEMORY_LIMIT
1428+
heap->size += (true_size - orig_size);
1429+
#endif
14301430
HANDLE_UNBLOCK_INTERRUPTIONS();
14311431
}
14321432
#if ZEND_DEBUG
@@ -1443,14 +1443,15 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
14431443
next_block = ZEND_MM_NEXT_BLOCK(mm_block);
14441444

14451445
if (ZEND_MM_IS_FREE_BLOCK(next_block)) {
1446-
if (ZEND_MM_BLOCK_SIZE(mm_block) + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
1447-
size_t block_size = ZEND_MM_BLOCK_SIZE(mm_block) + ZEND_MM_FREE_BLOCK_SIZE(next_block);
1446+
if (orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block) >= true_size) {
1447+
size_t block_size = orig_size + ZEND_MM_FREE_BLOCK_SIZE(next_block);
14481448
size_t remaining_size = block_size - true_size;
14491449

14501450
HANDLE_BLOCK_INTERRUPTIONS();
14511451
zend_mm_remove_from_free_list(heap, (zend_mm_free_block *) next_block);
14521452

14531453
if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
1454+
true_size = block_size;
14541455
ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, block_size);
14551456
} else {
14561457
zend_mm_free_block *new_free_block;
@@ -1470,6 +1471,12 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
14701471
mm_block->debug.orig_filename = __zend_orig_filename;
14711472
mm_block->debug.orig_lineno = __zend_orig_lineno;
14721473
ZEND_MM_SET_END_MAGIC(mm_block);
1474+
#endif
1475+
#if MEMORY_LIMIT
1476+
heap->size = heap->size + true_size - orig_size;
1477+
if (heap->peak < heap->size) {
1478+
heap->peak = heap->size;
1479+
}
14731480
#endif
14741481
HANDLE_UNBLOCK_INTERRUPTIONS();
14751482
return p;
@@ -1551,7 +1558,8 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
15511558
ZEND_MM_LAST_BLOCK(ZEND_MM_BLOCK_AT(mm_block, block_size));
15521559

15531560
if (remaining_size < ZEND_MM_ALIGNED_MIN_HEADER_SIZE) {
1554-
ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, block_size);
1561+
true_size = block_size;
1562+
ZEND_MM_BLOCK(mm_block, ZEND_MM_USED_BLOCK, true_size);
15551563
} else {
15561564
zend_mm_free_block *new_free_block;
15571565

@@ -1575,6 +1583,12 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
15751583
mm_block->thread_id = tsrm_thread_id();
15761584
# endif
15771585
ZEND_MM_SET_END_MAGIC(mm_block);
1586+
#endif
1587+
#if MEMORY_LIMIT
1588+
heap->size = heap->size + true_size - orig_size;
1589+
if (heap->peak < heap->size) {
1590+
heap->peak = heap->size;
1591+
}
15781592
#endif
15791593
HANDLE_UNBLOCK_INTERRUPTIONS();
15801594
return ZEND_MM_DATA_OF(mm_block);
@@ -1584,7 +1598,7 @@ static void *_zend_mm_realloc_int(zend_mm_heap *heap, void *p, size_t size ZEND_
15841598
#if ZEND_DEBUG
15851599
memcpy(ptr, p, mm_block->debug.size);
15861600
#else
1587-
memcpy(ptr, p, ZEND_MM_BLOCK_SIZE(mm_block) - ZEND_MM_ALIGNED_HEADER_SIZE);
1601+
memcpy(ptr, p, orig_size - ZEND_MM_ALIGNED_HEADER_SIZE);
15881602
#endif
15891603
_zend_mm_free_int(heap, p ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
15901604
return ptr;

0 commit comments

Comments
 (0)