Skip to content

Return [] instead of raising on corrupt beam in Normalized.Typespec - #346

Merged
lukaszsamson merged 1 commit into
masterfrom
fix-typespec-beam-lib-raise
Oct 18, 2026
Merged

lukaszsamson merged 1 commit into
masterfrom
fix-typespec-beam-lib-raise

Conversation

@lukaszsamson

Copy link
Copy Markdown
Collaborator

Problem

Completion and signature help crash when a module's .beam is malformed or truncated (e.g. while being rewritten by a concurrent compile):

an exception was raised:
  ...]}}
    (stdlib 6.2.2) beam_lib.erl:712: :beam_lib.read_info/1
    (elixir 1.18.2) code/typespec.ex:173: Code.Typespec.get_module_and_beam/1
    (elixir 1.18.2) code/typespec.ex:152: Code.Typespec.typespecs_abstract_code/1
    (elixir 1.18.2) code/typespec.ex:121: Code.Typespec.fetch_specs/1
    elixir_sense/core/normalized/typespec.ex:9: ElixirSense.Core.Normalized.Typespec.get_specs/1
    elixir_sense/core/type_info.ex:379: ElixirSense.Core.TypeInfo.get_module_specs/1

:beam_lib.read_info/1 only catches throws, so error/exit exceptions from parsing corrupt chunks escape Code.Typespec.fetch_*.

Fix

ElixirSense.Core.Normalized.Typespec.get_specs/1, get_types/1 and get_callbacks/1 now wrap the Code.Typespec.fetch_* call so any raise/exit/throw is treated as :error, which yields [].

Only callers of Code.Typespec in lib/ go through this module. ExckReader (which uses :beam_lib directly) has no callers in lib/, so it is unchanged.

Test

Added a test that writes a .beam with one byte flipped at offset 25 (inside the atom chunk) to a temp dir on the code path. Without the fix it raises MatchError from :beam_lib; with the fix all three functions return [].

Code.Typespec.fetch_* delegates to :beam_lib, which only catches throws.
A malformed or truncated .beam (e.g. one being rewritten by a concurrent
compile) raises MatchError/ArgumentError out of completion and signature
help. Treat any raise/exit/throw from the fetch as :error so get_specs,
get_types and get_callbacks return [].
@lukaszsamson
lukaszsamson merged commit 0e7d182 into master Oct 18, 2026
12 of 18 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.

1 participant