Fix segmentation fault (invalid free)#10
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a segmentation fault caused by invalid free operations by changing how zones are determined from chunks. Instead of deriving the zone from the chunk/page size (which could be inaccurate after memory operations), it now stores and uses an explicit zone_type field in each page.
- Added
zone_typefield topage_tstruct to persistently track which zone a page belongs to - Introduced
get_zone_from_type()function to retrieve zones based on the stored zone type - Modified
get_zone_from_chunk()to use the storedzone_typeinstead of calculating from size
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/zone.c | Adds get_zone_from_type() function and modifies get_zone_from_chunk() to use stored zone type instead of size-based calculation |
| src/page.c | Initializes the new zone_type field during page allocation |
| include/zone.h | Adds function declaration for get_zone_from_type() |
| include/page.h | Adds zone_type field to page_t struct |
| Makefile | Adds symbolic link creation and cleanup for libft_malloc.so |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| return &memory->large; |
There was a problem hiding this comment.
The switch statement in get_zone_from_type does not have a default case, which could lead to undefined behavior if an invalid zone_type_t value is passed. While the fallthrough after the switch returns &memory->large, it would be clearer and more maintainable to handle this explicitly within a default case. Consider adding:
default:
return &memory->large;This makes the intent explicit and ensures all code paths are clearly documented.
| } | |
| return &memory->large; | |
| default: | |
| return &memory->large; | |
| } |
c4bb5ad to
718707d
Compare
No description provided.