Skip to content

Use add_assoc_zval_ex#7

Merged
yordadev merged 15 commits into
mainfrom
patch-main-php-85-heap-corruption-error
Jan 14, 2026
Merged

Use add_assoc_zval_ex#7
yordadev merged 15 commits into
mainfrom
patch-main-php-85-heap-corruption-error

Conversation

@yordadev
Copy link
Copy Markdown
Contributor

Changed from:
zend_hash_add(Z_ARRVAL_P(return_value), key, &header_array);

To:
add_assoc_zval_ex(return_value, ZSTR_VAL(key), ZSTR_LEN(key), &header_array);

This creates a fresh key string from the raw char* and length, ensuring proper key ownership in the return value array.

Files Modified

  • src/request.c:1005-1018 - Request::getHeaders()
  • src/response.c:522-535 - Response::getHeaders()

Fixes two critical memory management bugs:

  1. Double-free in Request::create() with Uri objects (src/request.c:909-914)
     - Removed erroneous smart_str_free() after ZVAL_STR_COPY()
     - ZVAL_STR_COPY already takes ownership via refcounting
     - The smart_str_free() was prematurely freeing memory still referenced by intern->zv_uri
     - Caused 'zend_mm_heap corrupted' crash at test 63/210 in CI on PHP 8.5.3-dev

  2. Memory leak in Response::create() with string bodies (src/response.c:388)
     - Added missing zval_ptr_dtor(&body_zv) to clean up copied parameter
     - ZVAL_COPY increments refcount, must be balanced with zval_ptr_dtor
Security Fixes:
   - Fix use-after-free in Stream::getContents() and __toString() by adding
     zend_string_copy() to properly increment refcounts before returning strings
   - Add integer overflow/underflow checks in Stream::seek() for SEEK_CUR and
     SEEK_END operations to prevent wraparound vulnerabilities
   - Add NULL check in Request::getHeaderLine() to prevent crashes when
     smart_str_0() returns NULL for empty header arrays
   - Replace unsafe atol() with ZEND_STRTOL() in UploadedFile for safe
     string-to-long conversion with proper error handling and bounds checking

   Build System Improvements:
   - Add VERSION build argument to Dockerfile (83, 84, 85) for multi-version
     support, defaulting to PHP 8.5
   - Update Makefile to use VERSION variable consistently across all targets
     (docker-build, docker-test, docker-shell, ci-test-all)
   - Remove build-and-push.sh script and associated Makefile target
   - Properly format VERSION to PHP branch (85 -> PHP-8.5) using shell parsing

   Configuration:
   - Enable -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 unconditionally in config.m4
     for better performance on modern PHP 7.0+ (recommended best practice)
@yordadev yordadev self-assigned this Jan 14, 2026
@yordadev yordadev merged commit 785065f into main Jan 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant