Skip to content

Fix debug stomping size and safety in Array#22344

Merged
dkorpel merged 1 commit intodlang:masterfrom
gorsing:fixroot_array_memset
Jan 6, 2026
Merged

Fix debug stomping size and safety in Array#22344
dkorpel merged 1 commit intodlang:masterfrom
gorsing:fixroot_array_memset

Conversation

@gorsing
Copy link
Contributor

@gorsing gorsing commented Jan 2, 2026

Description: This MR fixes memory stomping logic for debug = stomp builds and improves general safety in memory handling.

  • Fix in ~this(): Corrected the memset size argument. It now uses the byte size (length * T.sizeof) instead of the element count (length), fixing incomplete stomping for types where T.sizeof > 1. Added a null check for data.ptr.
  • Refactor reserve(): Added checks to skip stomping/clearing unused memory if the array is using inline storage (smallarray) or is uninitialized. This applies to both debug and GC paths.

#22284 #22327

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @gorsing! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#22344"

Copy link
Contributor

@dkorpel dkorpel left a comment

Choose a reason for hiding this comment

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

Why do you not want to stomp inline storage?

@gorsing
Copy link
Contributor Author

gorsing commented Jan 2, 2026

@dkorpel Thank you for time.
Good point. I initially added this check to match the logic used for xfree (to be safe), but you are right — we should definitely stomp inline storage as well to catch invalid accesses there. I will remove the smallarray exclusion for the memset call.

I will update it to:

// In reserve method:

debug (stomp)
{
    if (data.ptr) 
    {
        if (length < data.length)
            memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof);
    }
}
~this() pure nothrow
{
    debug (stomp)
    {
        if (data.ptr)
            memset(data.ptr, 0xFF, data.length * T.sizeof);
    }
    
    if (data.ptr && data.ptr != &smallarray[0])
        mem.xfree(data.ptr);
}

@gorsing gorsing requested a review from dkorpel January 2, 2026 19:36
@gorsing gorsing force-pushed the fixroot_array_memset branch from b8cfd4f to 21e9cf7 Compare January 6, 2026 07:37
@dkorpel dkorpel merged commit 3fae52e into dlang:master Jan 6, 2026
42 checks passed
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.

3 participants