THRIFT-127. erlang: Skip fields that have the wrong type
In the other languages (AFAIK), if a field has the wrong type,
it is silently skipped. Erlang currently assumes that the type
is correct and de-synchronizes. This change makes it behave like
the other languages (except that it is not silent).
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@760161 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/erl/src/thrift_protocol.erl b/lib/erl/src/thrift_protocol.erl
index a514d8f..e6045e2 100644
--- a/lib/erl/src/thrift_protocol.erl
+++ b/lib/erl/src/thrift_protocol.erl
@@ -140,19 +140,30 @@
_Else ->
case dict:find(Fid, SDict) of
{ok, {Type, Index}} ->
- {ok, Val} = read(IProto, Type),
- thrift_protocol:read(IProto, field_end),
- NewRTuple = setelement(Index, RTuple, Val),
- read_struct_loop(IProto, SDict, NewRTuple);
+ case term_to_typeid(Type) of
+ FType ->
+ {ok, Val} = read(IProto, Type),
+ thrift_protocol:read(IProto, field_end),
+ NewRTuple = setelement(Index, RTuple, Val),
+ read_struct_loop(IProto, SDict, NewRTuple);
+ Expected ->
+ error_logger:info_msg(
+ "Skipping field ~p with wrong type (~p != ~p)~n",
+ [Fid, FType, Expected]),
+ skip_field(FType, IProto, SDict, RTuple)
+ end;
_Else2 ->
- error_logger:info_msg("Skipping fid ~p~n", [Fid]),
- FTypeAtom = thrift_protocol:typeid_to_atom(FType),
- thrift_protocol:skip(IProto, FTypeAtom),
- read(IProto, field_end),
- read_struct_loop(IProto, SDict, RTuple)
+ error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
+ skip_field(FType, IProto, SDict, RTuple)
end
end.
+skip_field(FType, IProto, SDict, RTuple) ->
+ FTypeAtom = thrift_protocol:typeid_to_atom(FType),
+ thrift_protocol:skip(IProto, FTypeAtom),
+ read(IProto, field_end),
+ read_struct_loop(IProto, SDict, RTuple).
+
skip(Proto, struct) ->
ok = read(Proto, struct_begin),