Skip to content

Commit b471640

Browse files
GregPlowmanStefanKarpinski
authored andcommitted
improve deserialize error message (#31374)
1 parent 4105848 commit b471640

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

stdlib/Serialization/src/Serialization.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,19 @@ function resolve_ref_immediately(s::AbstractSerializer, @nospecialize(x))
737737
nothing
738738
end
739739

740+
function gettable(s::AbstractSerializer, id::Int)
741+
get(s.table, id) do
742+
errmsg = """Inconsistent Serializer state when deserializing.
743+
Attempt to access internal table with key $id failed.
744+
745+
This might occur if the Serializer contexts when serializing and deserializing are inconsistent.
746+
In particular, if multiple serialize calls use the same Serializer object then
747+
the corresponding deserialize calls should also use the same Serializer object.
748+
"""
749+
error(errmsg)
750+
end
751+
end
752+
740753
# deserialize_ is an internal function to dispatch on the tag
741754
# describing the serialized representation. the number of
742755
# representations is fixed, so deserialize_ does not get extended.
@@ -750,10 +763,10 @@ function handle_deserialize(s::AbstractSerializer, b::Int32)
750763
return deserialize_tuple(s, Int(read(s.io, UInt8)::UInt8))
751764
elseif b == SHORTBACKREF_TAG
752765
id = read(s.io, UInt16)::UInt16
753-
return s.table[Int(id)]
766+
return gettable(s, Int(id))
754767
elseif b == BACKREF_TAG
755768
id = read(s.io, Int32)::Int32
756-
return s.table[Int(id)]
769+
return gettable(s, Int(id))
757770
elseif b == ARRAY_TAG
758771
return deserialize_array(s)
759772
elseif b == DATATYPE_TAG
@@ -800,7 +813,7 @@ function handle_deserialize(s::AbstractSerializer, b::Int32)
800813
return deserialize_expr(s, Int(read(s.io, Int32)::Int32))
801814
elseif b == LONGBACKREF_TAG
802815
id = read(s.io, Int64)::Int64
803-
return s.table[Int(id)]
816+
return gettable(s, Int(id))
804817
elseif b == LONGSYMBOL_TAG
805818
return deserialize_symbol(s, Int(read(s.io, Int32)::Int32))
806819
elseif b == HEADER_TAG

0 commit comments

Comments
 (0)