Skip to content

fix: Prevent double-free and memory leaks in validate function - #9

Draft
matapatos wants to merge 1 commit into
mainfrom
vibe/fix-memory-double-free
Draft

fix: Prevent double-free and memory leaks in validate function#9
matapatos wants to merge 1 commit into
mainfrom
vibe/fix-memory-double-free

Conversation

@matapatos

Copy link
Copy Markdown
Contributor

Summary

  • Fix critical memory management bugs in validate function

Details

This PR fixes several memory management issues in src/validate_function.c:

Bugs Fixed:

  1. Double-free bug: field_name was being released twice in some code paths
  2. Memory leak: field_name was not released when field_value is NULL and stop_first_error is false
  3. Memory leak: field_name was not released when validate_field_value throws an exception
  4. Memory leak: field_name was not released when valid_value is NULL and stop_first_error is false

Changes:

  • Reorganized the cleanup logic to ensure field_name is released in all code paths
  • Added missing zend_string_release calls before continue and RETURN_THROWS statements
  • Maintained the same functionality while preventing memory leaks and double-free errors

Impact

These fixes prevent:

  • Use-after-free errors that could cause crashes
  • Memory leaks that could exhaust memory over time
  • Undefined behavior from double-free operations

Testing

The fix maintains the same external behavior while correcting internal memory management.

- Fix double-free bug where field_name was released twice
- Fix memory leak where field_name was not released when field_value is NULL and stop_first_error is false
- Fix memory leak where field_name was not released on exception from validate_field_value
- Fix memory leak where field_name was not released when valid_value is NULL and stop_first_error is false
- Ensure field_name is released in all code paths before returning or continuing

This prevents use-after-free errors and memory leaks in the validation logic.

Co-authored-by: matapatos <matapatos@users.noreply.github.com>
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.

2 participants