aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler/src/v3_core.erl
diff options
context:
space:
mode:
authorBjörn Gustavsson <[email protected]>2012-08-22 14:54:45 +0200
committerBjörn Gustavsson <[email protected]>2012-08-22 16:16:26 +0200
commite4e5d08621137473674cf3cdce0b36a43a8d6d15 (patch)
treebe27bd18de935d599efa2b48363d9df6d0edc766 /lib/compiler/src/v3_core.erl
parent064b42237d891d5fdcb6c1a351980b8291437618 (diff)
downloadotp-e4e5d08621137473674cf3cdce0b36a43a8d6d15.tar.gz
otp-e4e5d08621137473674cf3cdce0b36a43a8d6d15.tar.bz2
otp-e4e5d08621137473674cf3cdce0b36a43a8d6d15.zip
compiler: Warn if the size of a binary segment is invalid
The compiler would silently accept and Dialyzer would crash on code like: <<X:(2.5)>> It is never acceptable for Dialyzer to crash. The compiler should at least generate a warning for such code. It is tempting to let the compiler generate an error, but that would mean that code like: Sz = 42.0, <<X:Sz>>. would be possible to compile with optimizations disabled, but not with optimizations enabled. Dialyzer crashes because it calls cerl:bitstr_bitsize/1, which crashes if the type of size for the segment is invalid. The easiest way to avoid that crash is to extend the sanity checks in v3_core to also include the size field of binary segments. That will cause the compiler to issue a warning and to replace the bad binary construction with a call to erlang:error/1. (It also means that Dialyzer will not issue a warning for bad size fields.)
Diffstat (limited to 'lib/compiler/src/v3_core.erl')
-rw-r--r--lib/compiler/src/v3_core.erl7
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/compiler/src/v3_core.erl b/lib/compiler/src/v3_core.erl
index 242196c593..01042cc56f 100644
--- a/lib/compiler/src/v3_core.erl
+++ b/lib/compiler/src/v3_core.erl
@@ -823,6 +823,13 @@ bitstr({bin_element,_,E0,Size0,[Type,{unit,Unit}|Flags]}, St0) ->
{_,_} ->
throw(bad_binary)
end,
+ case Size1 of
+ #c_var{} -> ok;
+ #c_literal{val=Sz} when is_integer(Sz), Sz >= 0 -> ok;
+ #c_literal{val=undefined} -> ok;
+ #c_literal{val=all} -> ok;
+ _ -> throw(bad_binary)
+ end,
{#c_bitstr{val=E1,size=Size1,
unit=#c_literal{val=Unit},
type=#c_literal{val=Type},