Skip to content

Improve malloc and realloc handling#28

Merged
marcoroth merged 1 commit intomainfrom
improve-malloc-remalloc-handling
Feb 14, 2025
Merged

Improve malloc and realloc handling#28
marcoroth merged 1 commit intomainfrom
improve-malloc-remalloc-handling

Conversation

@marcoroth
Copy link
Owner

This pull request includes several improvements and refactorings to enhance memory safety, buffer handling, and code organization. The most important changes involve refactoring the buffer and array modules to use safer memory allocation functions, adding error handling, and introducing new utility functions.

This is a follow up to #26 to address the valid bugprone-suspicious-realloc-usage it caught.

Memory Safety Enhancements:

  • Introduced safe_malloc, safe_realloc, nullable_safe_malloc, and nullable_safe_realloc functions to handle memory allocation safely and provide better error handling. (src/include/memory.h, src/memory.c).

Buffer Handling Improvements:

  • src/buffer.c: Refactored the buffer_append, buffer_prepend, and buffer_concat functions to use a safer reallocation function and added error handling for memory allocation failures. Added new functions buffer_increase_capacity, buffer_append_char, buffer_reserve, and buffer_clear.
  • src/array.c: Refactored the array_append and array_free functions to use a safer reallocation function and added error handling for memory allocation failures.
  • Updated lexer and other components to use the new buffer structure for handling strings and text content. (src/lexer.c, src/include/lexer.h).

Error Handling Improvements:

  • Improved error handling in various functions by checking for null pointers and adding appropriate error messages. (src/array.c, src/buffer.c, src/io.c.

Miscellaneous:

  • Enabled the bugprone-suspicious-realloc-usage check in .clang-tidy to catch potential issues with realloc usage (.clang-tidy)

Test suite enhancements:

  • test/main.c: Updated the test suite to include new test cases for the array, buffer, and io modules.

@marcoroth marcoroth requested a review from Copilot February 14, 2025 05:17
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 23 changed files in this pull request and generated no comments.

Files not reviewed (19)
  • src/array.c: Language not supported
  • src/buffer.c: Language not supported
  • src/erbx.c: Language not supported
  • src/include/array.h: Language not supported
  • src/include/buffer.h: Language not supported
  • src/include/erbx.h: Language not supported
  • src/include/macros.h: Language not supported
  • src/include/memory.h: Language not supported
  • src/include/token.h: Language not supported
  • src/include/util.h: Language not supported
  • src/io.c: Language not supported
  • src/lexer.c: Language not supported
  • src/location.c: Language not supported
  • src/main.c: Language not supported
  • src/memory.c: Language not supported
  • src/token.c: Language not supported
  • src/util.c: Language not supported
  • test/main.c: Language not supported
  • test/test_array.c: Language not supported

@marcoroth marcoroth merged commit 9b1c56a into main Feb 14, 2025
3 checks passed
@marcoroth marcoroth deleted the improve-malloc-remalloc-handling branch February 14, 2025 05:19
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