File 0423-beam_z-Don-t-crash-when-compiling-illegal-bs_put_bin.patch of Package erlang
From a2744149a3471607a95e84894e11a70d0069e2cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Fri, 21 Feb 2020 16:43:39 +0100
Subject: [PATCH 03/30] beam_z: Don't crash when compiling illegal
 bs_put_binary instructions
When compiled with the no_type_opt option, the following code
would crash the compiler:
    {'EXIT',{badarg,_}} = (catch << (catch "\001") || true >>)
---
 lib/compiler/src/beam_z.erl            | 3 ++-
 lib/compiler/test/Makefile             | 1 +
 lib/compiler/test/bs_bincomp_SUITE.erl | 4 ++++
 3 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/lib/compiler/src/beam_z.erl b/lib/compiler/src/beam_z.erl
index b5b2dde22c..eb394050d2 100644
--- a/lib/compiler/src/beam_z.erl
+++ b/lib/compiler/src/beam_z.erl
@@ -90,7 +90,8 @@ undo_renames([{bs_put,_,{bs_put_binary,1,_},
                [{atom,all},{literal,<<>>}]}|Is]) ->
     undo_renames(Is);
 undo_renames([{bs_put,Fail,{bs_put_binary,1,_Flags},
-               [{atom,all},{literal,BinString}]}|Is0]) ->
+               [{atom,all},{literal,BinString}]}|Is0])
+  when is_bitstring(BinString)->
     Bits = bit_size(BinString),
     Bytes = Bits div 8,
     case Bits rem 8 of
diff --git a/lib/compiler/test/Makefile b/lib/compiler/test/Makefile
index 1a6628fc9f..88ce57625f 100644
--- a/lib/compiler/test/Makefile
+++ b/lib/compiler/test/Makefile
@@ -58,6 +58,7 @@ NO_OPT= \
 	beam_utils \
 	bif \
 	bs_construct \
+	bs_bincomp \
         bs_match \
 	bs_utf \
 	core_fold \
diff --git a/lib/compiler/test/bs_bincomp_SUITE.erl b/lib/compiler/test/bs_bincomp_SUITE.erl
index a4125f00f1..9ac261c805 100644
--- a/lib/compiler/test/bs_bincomp_SUITE.erl
+++ b/lib/compiler/test/bs_bincomp_SUITE.erl
@@ -356,6 +356,10 @@ matched_out_size_1(Binary) ->
 no_generator(Config) ->
     [<<"abc">>] = [<<(id(<<"abc">>)) || true >>],
     {<<>>} = {<<(id(<<"abc">>)) || false >>},
+
+    %% Would crash the compiler when compiled with +no_type_opt.
+    {'EXIT',{badarg,_}} = (catch << (catch "\001") || true >>),
+
     ok.
 
 cs_init() ->
-- 
2.16.4