From 9dfc13a1d8b4057748aa836692f979793410dffd Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 24 Jul 2026 17:05:49 -0700 Subject: [PATCH 1/5] Fix crash in API usage Fix get_flags/1 undefined crash by adding default value 0, this will allow `is_beam/1` and `is_entrypoint/1` to return `false` rather than crash on malformed or unrecognized files. Signed-off-by: Winford --- src/packbeam_api.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packbeam_api.erl b/src/packbeam_api.erl index d0f1e74..a7abd73 100644 --- a/src/packbeam_api.erl +++ b/src/packbeam_api.erl @@ -345,7 +345,7 @@ is_beam(AVMElement) -> %% @private get_flags(AVMElement) -> - proplists:get_value(flags, AVMElement). + proplists:get_value(flags, AVMElement, 0). %% @private parse_files(InputPaths, Lib, StartModule, IncludeLines) -> From 90c4ff8418a027efc055190e1590a591d4c8e500 Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 24 Jul 2026 18:16:50 -0700 Subject: [PATCH 2/5] Prevent crash on missing chunk_refs Add default [] to get_imports/1 and get_atoms/1. Signed-off-by: Winford --- src/packbeam_api.erl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/packbeam_api.erl b/src/packbeam_api.erl index a7abd73..792d08c 100644 --- a/src/packbeam_api.erl +++ b/src/packbeam_api.erl @@ -524,14 +524,20 @@ remove(Module, ParsedFiles) -> %% @private get_imports(ParsedFile) -> - Imports = proplists:get_value(imports, proplists:get_value(chunk_refs, ParsedFile)), + Chunks = proplists:get_value(chunk_refs, ParsedFile, []), + Imports = + case proplists:get_value(imports, Chunks, []) of + missing_chunk -> []; + Imports0 -> Imports0 + end, lists:usort([M || {M, _F, _A} <- Imports]). %% @private get_atoms(ParsedFile) -> + Chunks = proplists:get_value(chunk_refs, ParsedFile, []), AtomsT = [ Atom - || {_Index, Atom} <- proplists:get_value(atoms, proplists:get_value(chunk_refs, ParsedFile)) + || {_Index, Atom} <- proplists:get_value(atoms, Chunks, []) ], AtomsFromLiterals = get_atom_literals(proplists:get_value(uncompressed_literals, ParsedFile)), lists:usort(AtomsT ++ AtomsFromLiterals). From eca1a96d0eb3e397fb84d4543d33cbfaeb75b2aa Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 24 Jul 2026 17:20:15 -0700 Subject: [PATCH 3/5] Use consistent error format Use structured tuple `{no_start_module_found, ParsedFiles}` in `prune/2` for error reporting. Signed-off-by: Winford --- src/packbeam_api.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packbeam_api.erl b/src/packbeam_api.erl index 792d08c..cc92885 100644 --- a/src/packbeam_api.erl +++ b/src/packbeam_api.erl @@ -398,7 +398,7 @@ load_file(Path) -> prune(ParsedFiles, RootApplicationModule) -> case find_entrypoint(ParsedFiles) of false -> - throw("No input beam files contain start/0 entrypoint"); + throw({no_start_module_found, ParsedFiles}); {value, Entrypoint} -> BeamFiles = lists:filter(fun is_beam/1, ParsedFiles), Modules = closure(Entrypoint, BeamFiles, [get_element_module(Entrypoint)]), From 5e19f6e518b036f5f60f696e095d25be9ded73c1 Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 24 Jul 2026 17:29:05 -0700 Subject: [PATCH 4/5] Report error on write failures Propagate write errors in write_packbeam/2, return {error, {write_failed, ...}} instead of silently swallowing the error. Signed-off-by: Winford --- src/packbeam_api.erl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/packbeam_api.erl b/src/packbeam_api.erl index cc92885..dd492a7 100644 --- a/src/packbeam_api.erl +++ b/src/packbeam_api.erl @@ -815,7 +815,10 @@ write_packbeam(OutputFilePath, ParsedFiles) -> PackedData = [<> | [pack_data(ParsedFile) || ParsedFile <- ParsedFiles]] ++ [create_header(0, 0, <<"end">>)], - file:write_file(OutputFilePath, PackedData). + case file:write_file(OutputFilePath, PackedData) of + ok -> ok; + {error, Reason} -> {error, {write_failed, OutputFilePath, Reason}} + end. %% @private pack_data(ParsedFile) -> From 66c7f0a2b6143dc4dad8aaca86d5fef0a619b966 Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 24 Jul 2026 18:22:33 -0700 Subject: [PATCH 5/5] Prevent early termination on module_not_found error Replace exit/1 with throw/1 in get_parsed_file/2 to prevent early VM termination, and allow error to be reported. Signed-off-by: Winford --- src/packbeam_api.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packbeam_api.erl b/src/packbeam_api.erl index dd492a7..a130016 100644 --- a/src/packbeam_api.erl +++ b/src/packbeam_api.erl @@ -588,7 +588,7 @@ get_parsed_file(Module, ParsedFiles) -> {value, V} -> V; false -> - exit({module_not_found, Module, ParsedFiles}) + throw({module_not_found, Module, ParsedFiles}) end. %% @private