@@ -410,6 +410,7 @@ PHPAPI void php_stream_error_operation_end(php_stream_context *context)
410410 if (context == NULL ) {
411411 context = FG (default_context );
412412 }
413+
413414 /* Get error handling settings */
414415 int error_mode = php_stream_get_error_mode (context );
415416 int store_mode = php_stream_get_error_store_mode (context , error_mode );
@@ -423,12 +424,14 @@ PHPAPI void php_stream_error_operation_end(php_stream_context *context)
423424 if (store_mode == PHP_STREAM_ERROR_STORE_NONE ) {
424425 /* Free all errors */
425426 php_stream_error_entry_free (op -> first_error );
427+ op -> first_error = NULL ;
426428 } else {
427429 /* Filter and store */
428430 php_stream_error_entry * entry = op -> first_error ;
429431 php_stream_error_entry * prev = NULL ;
430432 php_stream_error_entry * to_store_first = NULL ;
431433 php_stream_error_entry * to_store_last = NULL ;
434+ php_stream_error_entry * remaining_first = NULL ;
432435
433436 while (entry ) {
434437 php_stream_error_entry * next = entry -> next ;
@@ -444,12 +447,6 @@ PHPAPI void php_stream_error_operation_end(php_stream_context *context)
444447
445448 if (should_store ) {
446449 /* Move to storage chain */
447- if (prev ) {
448- prev -> next = next ;
449- } else {
450- op -> first_error = next ;
451- }
452-
453450 entry -> next = NULL ;
454451 if (to_store_last ) {
455452 to_store_last -> next = entry ;
@@ -458,24 +455,16 @@ PHPAPI void php_stream_error_operation_end(php_stream_context *context)
458455 }
459456 to_store_last = entry ;
460457 } else {
461- /* Free this error */
458+ /* Keep in remaining chain (to be freed) */
459+ entry -> next = NULL ;
462460 if (prev ) {
463- prev -> next = next ;
461+ prev -> next = entry ;
464462 } else {
465- op -> first_error = next ;
463+ remaining_first = entry ;
466464 }
467-
468- zend_string_release (entry -> message );
469- efree (entry -> wrapper_name );
470- efree (entry -> param );
471- efree (entry -> docref );
472- efree (entry );
473-
474- entry = next ;
475- continue ;
465+ prev = entry ;
476466 }
477467
478- prev = entry ;
479468 entry = next ;
480469 }
481470
@@ -489,8 +478,12 @@ PHPAPI void php_stream_error_operation_end(php_stream_context *context)
489478 state -> stored_count ++ ;
490479 }
491480
492- /* Free any remaining errors not moved to storage */
493- php_stream_error_entry_free (op -> first_error );
481+ /* Free remaining errors that were not stored */
482+ if (remaining_first ) {
483+ php_stream_error_entry_free (remaining_first );
484+ }
485+
486+ op -> first_error = NULL ;
494487 }
495488 }
496489
0 commit comments