Skip to content

Fix segmentation fault (invalid free)#10

Merged
tdameros merged 4 commits into
mainfrom
fix-segmentation-fault
Nov 16, 2025
Merged

Fix segmentation fault (invalid free)#10
tdameros merged 4 commits into
mainfrom
fix-segmentation-fault

Conversation

@tdameros

Copy link
Copy Markdown
Owner

No description provided.

@tdameros tdameros requested a review from Copilot November 16, 2025 15:14
@tdameros tdameros self-assigned this Nov 16, 2025
@tdameros tdameros added the bug Something isn't working label Nov 16, 2025
@tdameros tdameros marked this pull request as ready for review November 16, 2025 15:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_type field to page_t struct 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 stored zone_type instead 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.

Comment thread src/zone.c Outdated
Comment on lines +25 to +26
}
return &memory->large;

Copilot AI Nov 16, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
}
return &memory->large;
default:
return &memory->large;
}

Copilot uses AI. Check for mistakes.
@tdameros tdameros force-pushed the fix-segmentation-fault branch from c4bb5ad to 718707d Compare November 16, 2025 15:24
@tdameros tdameros merged commit 7ecb1a2 into main Nov 16, 2025
1 check passed
@tdameros tdameros deleted the fix-segmentation-fault branch November 16, 2025 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants