# Sema: prevent access of undefined fields · gitcafe/zig

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

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

Visibility: public

Requested revision: 56cfa8f22f69d813efedb1fa01fdcb7077ca0e5a

Requested commit: 56cfa8f22f69d813efedb1fa01fdcb7077ca0e5a

Commit: 56cfa8f22f69d813efedb1fa01fdcb7077ca0e5a

Tree: abefcd7eb5be87432d75e8f3abd971539a3938d3

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
Sema: prevent access of undefined fields

When instantiating a generic function, there is a period of time where
the function is inserted into monomorphed_funcs map, but is not yet
initialized. Despite semantic analysis being single-threaded, generic
function instantiation can happen recursively, meaning that the hash
and equality functions for monomorphed_funcs entries are potentially
invoked for an uninitialized function.

This problem was mitigated by pre-setting the hash field on the newly
allocated function, however it did not solve the problem for hash
collisions in which case the equality function would be invoked. That it
was solved for hash() but not eql() explains why the problem was
difficult to observe. I tested this patch by temporarily sabotaging the
hash and making it always return 0.

This fix is centered on adding a new field to Module.Fn which is the one
checked by eql() and is populated pre-initialization.

closes #12643

```

## Parents

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

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