Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove empty if block and prevent use of pointer after erase #3098

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

dudantas
Copy link
Member

@dudantas dudantas commented Nov 10, 2024

Description

This PR removes an empty if block that was originally used back when pointer management was manual using raw pointers (*). At that time, the if block was intended to decrement the reference counter in a manual garbage collection mechanism. Since the project now uses modern smart pointers, this block has become obsolete and can be safely removed without affecting functionality.

The change also prevents the use of the container pointer after erasing the iterator it from the openContainers map, which was causing undefined behavior and potential crashes.

Behaviour

Actual

When attempting to close a container, the program would access a pointer (container) after its related iterator had been erased, resulting in a crash.

Expected

When closing a container, the program should remove the container entry safely without attempting to access invalidated pointers, preventing crashes.

Fixes this crash: crash.txt

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Manual testing to verify container closing works as expected without accessing invalid pointers.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works

This removes an empty if block that was kept for legacy purposes but no longer serves any functional purpose. Additionally, it addresses the improper use of the container pointer after the iterator it is erased from the openContainers structure, which could lead to undefined behavior and crashes.

Changes made:

• Removed the empty block if (container && container->getID() == ITEM_BROWSEFIELD) { }, which was no longer needed.

• Stored the value of container in a temporary variable before erasing the iterator it from the openContainers map.

These changes improve code stability, prevent potential crashes, and make the code cleaner by removing unnecessary logic.
Copy link

@dudantas dudantas merged commit c65c714 into main Nov 10, 2024
35 checks passed
@dudantas dudantas deleted the dudantas/fix-unused-if-causing-crash branch November 10, 2024 20:06
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