# sema: Rework Decl.value_arena to fix another memory corruption issue · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/2b592d7e3cf328deb1b8ffa7ea88389d785837ff)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 2b592d7e3cf328deb1b8ffa7ea88389d785837ff

Requested commit: 2b592d7e3cf328deb1b8ffa7ea88389d785837ff

Commit: 2b592d7e3cf328deb1b8ffa7ea88389d785837ff

Tree: 111892a8c4ef5301207a497382fe4fc6f51a3f4d

Author: kcbanner

Committer: kcbanner

## Message

```
sema: Rework Decl.value_arena to fix another memory corruption issue

This fixes a bug where resolveStructLayout to was promoting from stale
value_arena state which was then overwrriten when another ArenaAllocator
higher in the call stack saved its state back. This resulted in the memory
for struct_obj.optmized_order overlapping existing allocations.

My initial fix in c7067ef wasn't sufficient, as it only checked if the struct being
resolved had the same owner as the current sema instance. However, it's
possible for resolveStructLayout to be called when the sema instance
has a different owner, but the struct decl's value_arena is currently in
use higher up in the callstack.

This change introduces ValueArena, which holds the arena state as well as tracks
if an arena has already been promoted from it. This allows callers to use the
value_arena storage without needing to be aware of another user of this same storage
higher up in the call stack.

```

## Parents

- [396bd51c4818752f6309ff10f2d316f41598d5cd](https://git.cafe/gitcafe/zig/commit/396bd51c4818752f6309ff10f2d316f41598d5cd?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/2b592d7e3cf328deb1b8ffa7ea88389d785837ff?format=markdown)
