srl_decoder.c

filter: Statementstmt 29.7% 517 / 1736 Branchbran 17.0% 283 / 1662 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 23.5% 800 / 3398 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_checksum_policy.h ↑ Summary srl_common.h »
1 /* Must be defined before including Perl header files or we slow down by 2x! */
2 #define PERL_NO_GET_CONTEXT
3  
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10 #define NEED_newSV_type
11 #define NEED_newSVpvn_flags
12 #include "ppport.h"
13 #ifdef __cplusplus
14 }
15 #endif
16  
17 #ifndef PERL_VERSION
18 # include <patchlevel.h>
19 # if !(defined(PERL_VERSION) || (PERL_SUBVERSION > 0 && defined(PATCHLEVEL)))
20 # include <could_not_find_Perl_patchlevel.h>
21 # endif
22 # define PERL_REVISION 5
23 # define PERL_VERSION PATCHLEVEL
24 # define PERL_SUBVERSION PERL_SUBVERSION
25 #endif
26 #if PERL_VERSION < 8
27 # define PERL_MAGIC_qr 'r' /* precompiled qr// regex */
28 # define BFD_Svs_SMG_OR_RMG SVs_RMG
29 #elif ((PERL_VERSION==8) && (PERL_SUBVERSION >= 1) || (PERL_VERSION>8))
30 # define BFD_Svs_SMG_OR_RMG SVs_SMG
31 # define MY_PLACEHOLDER PL_sv_placeholder
32 #else
33 # define BFD_Svs_SMG_OR_RMG SVs_RMG
34 # define MY_PLACEHOLDER PL_sv_undef
35 #endif
36 #if (((PERL_VERSION == 9) && (PERL_SUBVERSION >= 4)) || (PERL_VERSION > 9))
37 # define NEW_REGEX_ENGINE 1
38 #endif
39 #if (((PERL_VERSION == 8) && (PERL_SUBVERSION >= 1)) || (PERL_VERSION > 8))
40 #define MY_CAN_FIND_PLACEHOLDERS
41 #define HAS_SV2OBJ
42 #endif
43 #if (PERL_VERSION < 10)
44 # define FIXUP_RITER 1
45 #endif
46 #if (PERL_VERSION >= 10)
47 # define FAST_IV 1
48 #endif
49  
50 #if defined(__CYGWIN__)
51 /* Cygwin's main thread stack is set by the Perl executable. */
52 #define DEFAULT_MAX_RECUR_DEPTH 4096
53 #else
54 #define DEFAULT_MAX_RECUR_DEPTH 8192
55 #endif
56 #define PARANOID_MAX_RECUR_DEPTH 2048
57 #define PARANOID_MAX_UNCOMPRESSED_SIZE (256 * 1024 * 1024)
58 #define PARANOID_MAX_STRING_LENGTH (64 * 1024 * 1024)
59 #define PARANOID_MAX_ARRAY_ENTRIES 1000000
60 #define PARANOID_MAX_HASH_ENTRIES 250000
61  
62 #if !defined(HAVE_CSNAPPY)
63 # include "snappy/csnappy_decompress.c"
64 #endif
65  
66 #include "srl_decoder.h"
67  
68 #include "srl_common.h"
69 #include "srl_float.h"
70 #include "srl_reader_skip.h"
71 #include "ptable.h"
72 #include "srl_reader.h"
73 #include "srl_reader_error.h"
74 #include "srl_reader_varint.h"
75 #include "srl_reader_misc.h"
76 #include "srl_reader_decompress.h"
77 #include "srl_protocol.h"
78 #include "srl_taginfo.h"
79 #include "srl_siphash.h"
80 #include "srl_checksum_policy.h"
81 #include "srl_reader_checksum.h"
82  
83 /* 5.8.8 and earlier have a nasty bug in their handling of overloading:
84 * The overload-flag is set on the referer of the blessed object instead of
85 * the referent. That means that our late-bless logic breaks for
86 * multiply-occurring objects.
87 * So for 5.8.8 and earlier, the easiest workaround is to bless as we go
88 * instead of blessing at the end of a decode run. Additionally, on repeatedly
89 * encountered objects (REFP), we have to check the stash of the referent for
90 * overloadedness and set the OVERLOAD flag (AMAGIC_on) on the NEW referer.
91 *
92 * Details on the perl bug in perl589delta.pod,
93 * see "Reblessing overloaded objects now works".
94 *
95 * This is potentially a security problem (destructors!), but we really need
96 * this to work on 5.8.5 for now, so let's make it work.
97 * Another way of making it work might be to keep track of all occurrences
98 * of objects and fix them up afterwards. That seems even more intrusive.
99 * Please prove us wrong, though, since it's semantically a better fix.
100 *
101 * --Eric and Steffen
102 */
103 #if ((PERL_VERSION == 8) && (PERL_SUBVERSION >= 9) || (PERL_VERSION > 8))
104 # define USE_588_WORKAROUND 0
105 #else
106 # define USE_588_WORKAROUND 1
107 #endif
108  
109 /* V1 offsets are relative to the packet, so nonzero offsets can still
110 * address header bytes. V2+ offsets start at one within each body. */
111 SRL_STATIC_INLINE UV
112 0 srl_read_backref_offset(pTHX_ srl_decoder_t *dec, const char *errstr)
113 {
114 0 const UV ofs = srl_read_varint_uv_offset(aTHX_ dec->pbuf, errstr);
115 0 ▶ if (expect_false(ofs < dec->min_backref_offset))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(ofs < dec->min_backref_offset)) 0 0 0
116 0 SRL_RDR_ERRORf1(dec->pbuf, "Back-reference offset points before the document body%s", errstr);
117 0 return ofs;
118 }
119  
120 /* predeclare all our subs so we have one definitive authority for their signatures */
121 SRL_STATIC_INLINE SV *srl_fetch_item(pTHX_ srl_decoder_t *dec, UV item, const char * const tag_name);
122  
123 /* these three are "Public" */
124 srl_decoder_t *srl_build_decoder_struct(pTHX_ HV *opt, sv_with_hash *options,
125 const U8 *known_option_heads,
126 const U8 *known_option_next); /* constructor */
127 void srl_destroy_decoder(pTHX_ srl_decoder_t *dec); /* destructor - called from ->DESTROY() */
128 void srl_decoder_destructor_hook(pTHX_ void *p); /* destructor hook - called automagically */
129  
130 /* the top level components of the decode process - called by srl_decode_into() */
131 /* srl_begin_decoding: set up the decoder to handle a given var */
132 SRL_STATIC_INLINE srl_decoder_t *srl_begin_decoding(pTHX_ srl_decoder_t *dec, SV *src, UV start_offset, int copy_input);
133 SRL_STATIC_INLINE void srl_read_header(pTHX_ srl_decoder_t *dec, SV *header_user_data); /* read/validate header */
134 SRL_STATIC_INLINE void srl_read_single_value(pTHX_ srl_decoder_t *dec, SV* into, SV** container); /* main recursive dump routine */
135 SRL_STATIC_INLINE void srl_read_single_value_ex(pTHX_ srl_decoder_t *dec, SV* into, SV** container, int is_refn_referent);
136 SRL_STATIC_INLINE void srl_finalize_structure(pTHX_ srl_decoder_t *dec); /* optional finalize structure logic */
137 SRL_STATIC_INLINE void srl_clear_decoder(pTHX_ srl_decoder_t *dec); /* clean up decoder after a dump */
138 SRL_STATIC_INLINE void srl_clear_thaw_state(pTHX_ srl_decoder_t *dec); /* discard pending frozen-object state */
139 SRL_STATIC_INLINE void srl_assert_input_unchanged(pTHX_ srl_decoder_t *dec); /* croak if a callback modified the input */
140  
141 /* the internal routines to handle each kind of object we have to deserialize */
142 static SRL_NOINLINE void srl_read_copy(pTHX_ srl_decoder_t *dec, SV* into);
143  
144 SRL_STATIC_INLINE void srl_read_hash(pTHX_ srl_decoder_t *dec, SV* into, U8 tag);
145 static SRL_NOINLINE void srl_read_array(pTHX_ srl_decoder_t *dec, SV* into, U8 tag);
146 static SRL_NOINLINE void srl_read_regexp(pTHX_ srl_decoder_t *dec, SV* into);
147  
148 SRL_STATIC_INLINE void srl_read_refp(pTHX_ srl_decoder_t *dec, SV* into);
149 SRL_STATIC_INLINE void srl_read_refn(pTHX_ srl_decoder_t *dec, SV* into);
150 SRL_STATIC_INLINE void srl_read_weaken(pTHX_ srl_decoder_t *dec, SV* into);
151 static SRL_NOINLINE void srl_read_long_double(pTHX_ srl_decoder_t *dec, SV* into);
152 SRL_STATIC_INLINE void srl_read_double(pTHX_ srl_decoder_t *dec, SV* into);
153 SRL_STATIC_INLINE void srl_read_float(pTHX_ srl_decoder_t *dec, SV* into);
154 static SRL_NOINLINE void srl_read_float_128(pTHX_ srl_decoder_t *dec, SV* into);
155 SRL_STATIC_INLINE void srl_read_string(pTHX_ srl_decoder_t *dec, int is_utf8, SV* into);
156 SRL_STATIC_INLINE void srl_read_varint_into(pTHX_ srl_decoder_t *dec, SV* into, SV** container, const U8 *track_it);
157 SRL_STATIC_INLINE void srl_read_zigzag_into(pTHX_ srl_decoder_t *dec, SV* into, SV** container, const U8 *track_it);
158 static SRL_NOINLINE SRL_UNUSED void srl_read_reserved(pTHX_ srl_decoder_t *dec, U8 tag, SV* into);
159 SRL_STATIC_INLINE void srl_read_object(pTHX_ srl_decoder_t *dec, SV* into, U8 obj_tag, int read_class_name_only);
160 SRL_STATIC_INLINE void srl_read_objectv(pTHX_ srl_decoder_t *dec, SV* into, U8 obj_tag);
161  
162 SRL_STATIC_INLINE void srl_track_sv(pTHX_ srl_decoder_t *dec, const U8 *track_pos, SV *sv);
163 SRL_STATIC_INLINE void srl_read_frozen_object(pTHX_ srl_decoder_t *dec, HV *class_stash, SV *into);
164 SRL_STATIC_INLINE SV * srl_follow_refp_alias_reference(pTHX_ srl_decoder_t *dec, UV offset, int is_refn_referent);
165 SRL_STATIC_INLINE AV * srl_follow_objectv_reference(pTHX_ srl_decoder_t *dec, UV offset);
166 SRL_STATIC_INLINE SV *srl_thaw_object(pTHX_ srl_decoder_t *dec, HV *class_stash, SV *sv);
167 SRL_STATIC_INLINE int srl_class_allowed(pTHX_ HV *allow_hash, SV *allow_cb, HV *class_stash);
168 SRL_STATIC_INLINE void srl_track_class(pTHX_ srl_decoder_t *dec, SV *class_entry);
169  
170 /* FIXME unimplemented!!! */
171 static SRL_NOINLINE int srl_read_extend(pTHX_ srl_decoder_t *dec, SV* into);
172  
173 #define DEPTH_INCREMENT(dec) STMT_START { \
174 if (expect_false(++dec->recursion_depth > dec->max_recursion_depth)) { \
175 SRL_RDR_ERRORf1(dec->pbuf, "Reached recursion limit (%"UVuf") during deserialization", \
176 (UV)dec->max_recursion_depth); \
177 } \
178 } STMT_END
179  
180 #define DEPTH_DECREMENT(dec) dec->recursion_depth--
181  
182 #define SRL_CHECK_MAX_STRING_LENGTH(dec, len, kind) STMT_START { \
183 if (expect_false((dec)->max_string_length != 0 \
184 && (UV)(len) > (dec)->max_string_length)) { \
185 SRL_RDR_ERRORf3((dec)->pbuf, \
186 "Got input %s with length %"UVuf", configured maximum is %"UVuf, \
187 (kind), (UV)(len), (UV)(dec)->max_string_length); \
188 } \
189 } STMT_END
190  
191 #define IS_SRL_HDR_ARRAYREF(tag) (((tag) & SRL_HDR_ARRAYREF) == SRL_HDR_ARRAYREF)
192 #define IS_SRL_HDR_HASHREF(tag) (((tag) & SRL_HDR_HASHREF) == SRL_HDR_HASHREF)
193 #define IS_SRL_HDR_SHORT_BINARY(tag) (((tag) & SRL_HDR_SHORT_BINARY_LOW) == SRL_HDR_SHORT_BINARY_LOW)
194 #define SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag) ((tag) & SRL_MASK_SHORT_BINARY_LEN)
195  
196  
197 #define SRL_ASSERT_REF_PTR_TABLES(dec) STMT_START { \
198 if (expect_false( !(dec)->ref_stashes )) { \
199 (dec)->ref_stashes = PTABLE_new(); \
200 (dec)->ref_bless_av = PTABLE_new(); \
201 } \
202 } STMT_END
203  
204 #define SRL_sv_set_rv_to(into,referent) \
205 STMT_START { \
206 SRL_prepare_SV_for_RV(into); \
207 SvTEMP_off(referent); \
208 SvRV_set(into, referent); \
209 SvROK_on(into); \
210 } STMT_END
211  
212 /* COPY offsets and lengths are bounded by the addressable input buffer.
213 * UV matches that buffer's size on each Perl build; U32 would truncate
214 * valid offsets in raw packets larger than 4 GiB on 64-bit Perl. */
215 #define COPY_OFFSET_ERROR_FMT_PREFIX "Corrupted COPY tag, offset %"UVuf" with length %"UVuf" must precede the tag's own offset"
216  
217 STATIC void
218 0 srl_ptable_debug_callback(PTABLE_ENTRY_t *e)
219 {
220 dTHX;
221 0 printf("KEY=%"UVuf"\nVALUE:\n", (UV)e->key);
222 0 sv_dump((SV *)e->value);
223 0 printf("\n");
224 0 }
225  
226 SRL_STATIC_INLINE void
227 srl_ptable_debug_dump(pTHX_ PTABLE_t *tbl)
228 {
229 PTABLE_debug_dump(tbl, srl_ptable_debug_callback);
230 }
231  
232 static U32
233 18 srl_decoder_option_id(pTHX_ sv_with_hash *options, U32 option_count,
234 const U8 *known_option_heads, const U8 *known_option_next,
235 U32 key_hash, const char *key_bytes, STRLEN key_len)
236 {
237 18 U8 indexed = known_option_heads[key_hash & (SRL_OPTION_INDEX_BUCKETS - 1)];
238 28 ▶ while (indexed) {
Branch: 1 branch
BranchTrueFalseTotal
while (indexed) { 28 0 28
239 28 U32 option_id = (U32)indexed - 1;
240 STRLEN known_len;
241 const char *known_bytes;
242 28 ▶ if (options[option_id].hash == key_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (options[option_id].hash == key_hash) { 18 10 28
243 18 known_bytes = SvPV(options[option_id].sv, known_len);
244 18 ▶ if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len))
Branch: 2 branches
BranchTrueFalseTotal
if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len)) 18 0 18
if (key_len == known_len && memEQ(key_bytes, known_bytes, key_len)) 18 0 18
245 18 return option_id;
246 }
247 10 indexed = known_option_next[option_id];
248 }
249 0 return option_count;
250 }
251  
252 /* Multiple places in this file that want to use srl_read_varint_uv_length
253 * but don't have a buffer struct handy. */
254 SRL_STATIC_INLINE UV
255 0 S_read_varint_uv_length_char_ptr(pTHX_ const unsigned char **from, const unsigned char *end, const char * const errstr)
256 {
257 UV rv;
258 srl_reader_buffer_t buf;
259 0 buf.pos = *from;
260 0 buf.end = end;
261 0 buf.start = buf.pos; /* Keep error-offset pointer arithmetic defined. */
262 0 rv = srl_read_varint_uv_length(aTHX_ &buf, errstr);
263 0 *from = buf.pos;
264 0 return rv;
265 }
266  
267  
268 /* PUBLIC ROUTINES */
269  
270 /* Reads a limit option. SvUV() alone turns -1 into UV_MAX (no limit) and
271 * truncates fractions, so require a non-negative integer. A false value
272 * leaves the option at its default, as it did before validation was added. */
273 SRL_STATIC_INLINE UV
274 2 srl_limit_option_uv(pTHX_ SV *val, const char *name)
275 {
276 NV nv;
277 /* The validation may inspect the value more than once. Take one magic
278 * snapshot so a tied option cannot return different values mid-check. */
279 2 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 2 2
280 0 val = sv_2mortal(newSVsv(val));
281 2 ▶ if (!SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(val)) 2 0 2
282 2 return 0;
283 0 ▶ if (SvIOK(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOK(val)) 0 0 0
284 0 ▶ if (SvIsUV(val) || SvIVX(val) >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (SvIsUV(val) || SvIVX(val) >= 0) 0 0 0
if (SvIsUV(val) || SvIVX(val) >= 0) 0 0 0
285 0 return SvUV_nomg(val);
286 0 ▶ if (looks_like_number(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (looks_like_number(val)) { 0 0 0
287 0 nv= SvNV_nomg(val);
288 0 ▶ if (nv >= 0 && nv < UV_MAX_P1 && nv == Perl_floor(nv))
Branch: 3 branches
BranchTrueFalseTotal
if (nv >= 0 && nv < UV_MAX_P1 && nv == Perl_floor(nv)) 0 0 0
if (nv >= 0 && nv < UV_MAX_P1 && nv == Perl_floor(nv)) 0 0 0
if (nv >= 0 && nv < UV_MAX_P1 && nv == Perl_floor(nv)) 0 0 0
289 0 return (UV)nv;
290 }
291 0 croak("The '%s' option must be a non-negative integer", name);
292 return 0; /* not reached */
293 }
294  
295 /* Builds the C-level configuration and state struct.
296 * Construction errors release partially allocated state. */
297 srl_decoder_t *
298 44 srl_build_decoder_struct(pTHX_ HV *opt, sv_with_hash *options,
299 const U8 *known_option_heads, const U8 *known_option_next)
300 {
301 srl_decoder_t *dec;
302 SV *val;
303 44 U8 checksum_policy = SRL_CHECKSUM_POLICY_VERIFY_IF_AVAILABLE;
304 44 U8 undef_deadly_policy = SRL_UNDEF_DEADLY_UNDEF;
305 44 SV *thaw_allow_classes_value = NULL;
306 44 SV *allowed_classes_value = NULL;
307 44 SV *allow_classes_value = NULL;
308 44 SV *track_classes_value = NULL;
309 44 SV *checksum_key = NULL;
310 44 SV *checksum_policy_value = NULL;
311 44 SV *unknown_option_policy_value = NULL;
312 44 AV *unknown_option_keys = NULL;
313 44 SV *alias_smallint_value = NULL;
314 44 SV *alias_varint_under_value = NULL;
315  
316 44 ENTER;
317 44 Newxz(dec, 1, srl_decoder_t);
318 44 SAVEDESTRUCTOR_X(&srl_decoder_destructor_hook, (void *)dec);
319  
320 44 dec->ref_seenhash = PTABLE_new();
321 44 dec->max_recursion_depth = DEFAULT_MAX_RECUR_DEPTH;
322 /* 0 == any number for array, hash & string */
323 44 dec->max_num_hash_entries = 0;
324 44 dec->max_num_array_entries = 0;
325 44 dec->max_string_length = 0;
326 44 dec->max_uncompressed_size = 0;
327  
328 44 SRL_RDR_CLEAR(&dec->buf);
329 44 dec->pbuf = &dec->buf;
330 44 dec->zstd_dctx = NULL;
331  
332 /* Dispatch supplied options by their precomputed IDs. Dependent options
333 * are collected here and resolved after the loop. */
334 {
335 U32 option_id;
336 HE *entry;
337 44 int no_thaw_set = 0;
338 44 int no_thaw = 0;
339 44 int no_bless = 0;
340 44 int paranoid = 0;
341 44 int refuse_objects_set = 0;
342 44 int max_uncompressed_size_set = 0;
343 44 int max_string_length_set = 0;
344 44 int max_array_entries_set = 0;
345 44 int max_hash_entries_set = 0;
346 44 int max_recursion_depth_set = 0;
347 44 int validate_utf8_set = 0;
348 44 ▶ if (opt != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt != NULL) { 18 26 44
349 18 hv_iterinit(opt);
350 36 ▶ while ((entry = hv_iternext(opt))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((entry = hv_iternext(opt))) { 18 18 36
351 18 SV *key = hv_iterkeysv(entry);
352 STRLEN key_len;
353 18 const char *key_bytes = SvPV(key, key_len);
354 18 option_id = srl_decoder_option_id(aTHX_ options, SRL_DEC_OPT_COUNT,
355 known_option_heads, known_option_next,
356 18 HeHASH(entry), key_bytes, key_len);
357 18 ▶ if (option_id == SRL_DEC_OPT_COUNT) {
Branch: 1 branch
BranchTrueFalseTotal
if (option_id == SRL_DEC_OPT_COUNT) { 0 18 18
358 0 ▶ if (!unknown_option_keys)
Branch: 1 branch
BranchTrueFalseTotal
if (!unknown_option_keys) 0 0 0
359 0 unknown_option_keys = (AV *)sv_2mortal((SV *)newAV());
360 0 av_push(unknown_option_keys, newSVsv(key));
361 0 continue;
362 }
363 18 val = HeVAL(entry);
364 18 switch (option_id) {
365 0 case SRL_DEC_OPT_IDX_ALIAS_SMALLINT:
366 0 alias_smallint_value = val;
367 0 break;
368 0 case SRL_DEC_OPT_IDX_ALIAS_VARINT_UNDER:
369 0 alias_varint_under_value = val;
370 0 break;
371 0 case SRL_DEC_OPT_IDX_CHECKSUM_KEY_V1:
372 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
373 0 break;
374 0 case SRL_DEC_OPT_IDX_CHECKSUM_KEY_V2:
375 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
376 0 break;
377 0 case SRL_DEC_OPT_IDX_CHECKSUM_POLICY:
378 0 checksum_policy_value = val;
379 0 break;
380 0 case SRL_DEC_OPT_IDX_CHECKSUM_KEY:
381 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
382 0 break;
383 0 case SRL_DEC_OPT_IDX_CHECKSUM_MODE:
384 /* Accepted for shared Encoder/Decoder configuration. The
385 * document, not this option, selects the reader algorithm. */
386 0 (void)srl_checksum_mode_from_sv(aTHX_ val, "checksum_mode");
387 0 break;
388 0 case SRL_DEC_OPT_IDX_CHECKSUM_KEY_V3:
389 0 srl_set_checksum_key(aTHX_ &checksum_key, val);
390 0 break;
391 0 case SRL_DEC_OPT_IDX_UNDEF_DEADLY_POLICY:
392 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
393 STRLEN len;
394 0 const char *policy = SvPVbyte(val, len);
395 0 ▶ if (memEQs(policy, len, "undef"))
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(policy, len, "undef")) 0 0 0
if (memEQs(policy, len, "undef")) 0 0 0
396 0 undef_deadly_policy = SRL_UNDEF_DEADLY_UNDEF;
397 0 ▶ else if (memEQs(policy, len, "warn"))
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(policy, len, "warn")) 0 0 0
else if (memEQs(policy, len, "warn")) 0 0 0
398 0 undef_deadly_policy = SRL_UNDEF_DEADLY_WARN;
399 0 ▶ else if (memEQs(policy, len, "die"))
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(policy, len, "die")) 0 0 0
else if (memEQs(policy, len, "die")) 0 0 0
400 0 undef_deadly_policy = SRL_UNDEF_DEADLY_DIE;
401 else
402 0 croak("Invalid undef_deadly_policy; expected 'undef', 'warn', or 'die'");
403 }
404 0 break;
405 0 case SRL_DEC_OPT_IDX_UNKNOWN_OPTION_POLICY:
406 0 unknown_option_policy_value = val;
407 0 break;
408 0 case SRL_DEC_OPT_IDX_ALLOW_UNKNOWN_LONG_DOUBLE:
409 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
410 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_ALLOW_UNKNOWN_LONG_DOUBLE);
411 0 break;
412 0 case SRL_DEC_OPT_IDX_LEGACY_FLOAT_BYTEORDER:
413 {
414 STRLEN len;
415 0 const char *order = SvPV(val, len);
416 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_FLOAT_BYTEORDER_SET);
417 0 ▶ if (memEQs(order, len, "native"))
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(order, len, "native")) 0 0 0
if (memEQs(order, len, "native")) 0 0 0
418 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_USE_NATIVE_FLOATS);
419 0 ▶ else if (memEQs(order, len, "big"))
Branch: 2 branches
BranchTrueFalseTotal
else if (memEQs(order, len, "big")) 0 0 0
else if (memEQs(order, len, "big")) 0 0 0
420 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_BIG_ENDIAN_FLOATS);
421 0 ▶ else if (!memEQs(order, len, "little"))
Branch: 2 branches
BranchTrueFalseTotal
else if (!memEQs(order, len, "little")) 0 0 0
else if (!memEQs(order, len, "little")) 0 0 0
422 0 croak("Invalid legacy_float_byteorder; expected native, little, or big");
423 }
424 0 break;
425 0 case SRL_DEC_OPT_IDX_DESTRUCTIVE_INCREMENTAL:
426 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
427 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL);
428 0 break;
429 0 case SRL_DEC_OPT_IDX_MAX_NUM_HASH_ENTRIES:
430 0 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 0 0
431 0 val = sv_2mortal(newSVsv(val));
432 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
433 0 max_hash_entries_set = 1;
434 0 dec->max_num_hash_entries = srl_limit_option_uv(aTHX_ val, "max_num_hash_entries");
435 }
436 0 break;
437 0 case SRL_DEC_OPT_IDX_MAX_RECURSION_DEPTH: {
438 0 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 0 0
439 0 val = sv_2mortal(newSVsv(val));
440 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
441 0 UV limit = srl_limit_option_uv(aTHX_ val, "max_recursion_depth");
442 0 max_recursion_depth_set = 1;
443 0 ▶ if (limit)
Branch: 1 branch
BranchTrueFalseTotal
if (limit) 0 0 0
444 0 dec->max_recursion_depth = limit;
445 }
446 0 break;
447 }
448 0 case SRL_DEC_OPT_IDX_REFUSE_OBJECTS:
449 0 refuse_objects_set = 1;
450 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
451 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS);
452 0 break;
453 0 case SRL_DEC_OPT_IDX_REFUSE_SNAPPY:
454 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
455 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REFUSE_SNAPPY);
456 0 break;
457 0 case SRL_DEC_OPT_IDX_REFUSE_ZLIB:
458 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
459 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REFUSE_ZLIB);
460 0 break;
461 8 case SRL_DEC_OPT_IDX_SET_READONLY:
462 8 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 8 0 8
463 8 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_SET_READONLY);
464 8 break;
465 8 case SRL_DEC_OPT_IDX_SET_READONLY_SCALARS:
466 8 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 8 0 8
467 8 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_SET_READONLY_SCALARS);
468 8 break;
469 0 case SRL_DEC_OPT_IDX_USE_UNDEF:
470 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
471 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_USE_UNDEF);
472 0 break;
473 0 case SRL_DEC_OPT_IDX_VALIDATE_UTF8:
474 0 validate_utf8_set = 1;
475 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
476 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8);
477 0 break;
478 0 case SRL_DEC_OPT_IDX_REFUSE_ZSTD:
479 0 ▶ if (SvTRUE(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) 0 0 0
480 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REFUSE_ZSTD);
481 0 break;
482 0 case SRL_DEC_OPT_IDX_MAX_NUM_ARRAY_ENTRIES:
483 0 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 0 0
484 0 val = sv_2mortal(newSVsv(val));
485 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
486 0 max_array_entries_set = 1;
487 0 dec->max_num_array_entries = srl_limit_option_uv(aTHX_ val, "max_num_array_entries");
488 }
489 0 break;
490 0 case SRL_DEC_OPT_IDX_MAX_STRING_LENGTH:
491 0 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 0 0
492 0 val = sv_2mortal(newSVsv(val));
493 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
494 0 max_string_length_set = 1;
495 0 dec->max_string_length = srl_limit_option_uv(aTHX_ val, "max_string_length");
496 }
497 0 break;
498 2 case SRL_DEC_OPT_IDX_MAX_UNCOMPRESSED_SIZE:
499 2 ▶ if (SvGMAGICAL(val))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(val)) 0 2 2
500 0 val = sv_2mortal(newSVsv(val));
501 2 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 2 0 2
502 2 max_uncompressed_size_set = 1;
503 2 dec->max_uncompressed_size = srl_limit_option_uv(aTHX_ val, "max_uncompressed_size");
504 }
505 2 break;
506 0 case SRL_DEC_OPT_IDX_NO_THAW_OBJECTS:
507 0 no_thaw_set = 1;
508 0 no_thaw = SvTRUE(val) ? 1 : 0;
509 0 break;
510 0 case SRL_DEC_OPT_IDX_THAW_ALLOW_CLASSES:
511 0 thaw_allow_classes_value = val;
512 0 break;
513 0 case SRL_DEC_OPT_IDX_ALLOWED_CLASSES:
514 0 allowed_classes_value = val;
515 0 break;
516 0 case SRL_DEC_OPT_IDX_ALLOW_CLASSES:
517 0 allow_classes_value = val;
518 0 break;
519 0 case SRL_DEC_OPT_IDX_TRACK_CLASSES:
520 0 track_classes_value = val;
521 0 break;
522 0 case SRL_DEC_OPT_IDX_PARANOID:
523 0 paranoid = SvTRUE(val) ? 1 : 0;
524 0 break;
525 0 case SRL_DEC_OPT_IDX_THAW_DENY_ACTION:
526 0 ▶ if (SvOK(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(val)) { 0 0 0
527 STRLEN alen;
528 0 char *astr = SvPV(val, alen);
529 0 ▶ if (memEQs(astr, alen, "raw"))
Branch: 2 branches
BranchTrueFalseTotal
if (memEQs(astr, alen, "raw")) 0 0 0
if (memEQs(astr, alen, "raw")) 0 0 0
530 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_THAW_DENY_RAW);
531 0 ▶ else if (!memEQs(astr, alen, "croak"))
Branch: 2 branches
BranchTrueFalseTotal
else if (!memEQs(astr, alen, "croak")) 0 0 0
else if (!memEQs(astr, alen, "croak")) 0 0 0
532 0 croak("The 'thaw_deny_action' option must be 'croak' or 'raw'");
533 }
534 0 break;
535 0 case SRL_DEC_OPT_IDX_NO_BLESS_OBJECTS:
536 0 no_bless = SvTRUE(val) ? 1 : 0;
537 0 break;
538 0 default:
539 0 croak("Internal error: unhandled Decoder option ID %u", (unsigned int)option_id);
540 }
541 }
542 }
543  
544 /* Traverse nested allow-lists after the options loop: the allow-list
545 * may be the options hash itself, whose iterator must not be reset. */
546 44 val = thaw_allow_classes_value;
547 44 ▶ if (val && SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(val)) { 0 44 44
if (val && SvOK(val)) { 0 0 0
548 0 ▶ SV *rv = SvROK(val) ? SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = SvROK(val) ? SvRV(val) : NULL; 0 0 0
549 0 ▶ if (rv && SvTYPE(rv) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
550 0 AV *av = (AV*)rv;
551 0 SSize_t i, len = av_len(av) + 1;
552 0 dec->thaw_allow_hash = newHV();
553 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
554 0 SV **elem = av_fetch(av, i, 0);
555 0 ▶ if (elem && SvOK(*elem)) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && SvOK(*elem)) { 0 0 0
if (elem && SvOK(*elem)) { 0 0 0
556 STRLEN klen;
557 0 char *kstr = SvPV(*elem, klen);
558 0 ▶ (void)hv_store(dec->thaw_allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(dec->thaw_allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
559 }
560 }
561 }
562 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
563 0 HV *src = (HV*)rv;
564 HE *ent;
565 0 dec->thaw_allow_hash = newHV();
566 0 hv_iterinit(src);
567 0 ▶ while ((ent = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(src))) { 0 0 0
568 0 ▶ if (SvTRUE(HeVAL(ent))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(HeVAL(ent))) { 0 0 0
569 STRLEN klen;
570 0 ▶ char *kstr = HePV(ent, klen);
Branch: 1 branch
BranchTrueFalseTotal
char *kstr = HePV(ent, klen); 0 0 0
571 0 ▶ (void)hv_store(dec->thaw_allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(dec->thaw_allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
(void)hv_store(dec->thaw_allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
572 }
573 }
574 }
575 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
576 0 dec->thaw_allow_cb = newRV_inc(rv);
577 }
578 else {
579 0 croak("The 'thaw_allow_classes' option must be an array ref, hash ref or code ref");
580 }
581 }
582  
583 44 val = track_classes_value;
584 44 ▶ if (val && SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(val)) { 0 44 44
if (val && SvOK(val)) { 0 0 0
585 0 ▶ SV *rv = SvROK(val) ? SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = SvROK(val) ? SvRV(val) : NULL; 0 0 0
586 0 ▶ if (!rv || SvTYPE(rv) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!rv || SvTYPE(rv) != SVt_PVHV) 0 0 0
if (!rv || SvTYPE(rv) != SVt_PVHV) 0 0 0
587 0 croak("The 'track_classes' option must be a hash ref");
588 0 dec->track_classes = (HV *)SvREFCNT_inc(rv);
589 }
590  
591 44 ▶ if (allow_classes_value && allowed_classes_value)
Branch: 2 branches
BranchTrueFalseTotal
if (allow_classes_value && allowed_classes_value) 0 44 44
if (allow_classes_value && allowed_classes_value) 0 0 0
592 0 croak("Specify only one of 'allow_classes' and 'allowed_classes'");
593 44 ▶ val = allow_classes_value ? allow_classes_value : allowed_classes_value;
Branch: 1 branch
BranchTrueFalseTotal
val = allow_classes_value ? allow_classes_value : allowed_classes_value; 0 44 44
594 44 ▶ if (val && SvOK(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvOK(val)) { 0 44 44
if (val && SvOK(val)) { 0 0 0
595 0 ▶ SV *rv = SvROK(val) ? SvRV(val) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *rv = SvROK(val) ? SvRV(val) : NULL; 0 0 0
596 0 ▶ if (rv && SvTYPE(rv) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
if (rv && SvTYPE(rv) == SVt_PVAV) { 0 0 0
597 0 AV *av = (AV*)rv;
598 0 SSize_t i, len = av_len(av) + 1;
599 0 dec->allow_hash = newHV();
600 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
601 0 SV **elem = av_fetch(av, i, 0);
602 0 ▶ if (elem && SvOK(*elem)) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && SvOK(*elem)) { 0 0 0
if (elem && SvOK(*elem)) { 0 0 0
603 STRLEN klen;
604 0 char *kstr = SvPV(*elem, klen);
605 0 ▶ (void)hv_store(dec->allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(dec->allow_hash, kstr, SvUTF8(*elem) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
606 }
607 }
608 }
609 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVHV) { 0 0 0
610 0 HV *src = (HV*)rv;
611 HE *ent;
612 0 dec->allow_hash = newHV();
613 0 hv_iterinit(src);
614 0 ▶ while ((ent = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((ent = hv_iternext(src))) { 0 0 0
615 0 ▶ if (SvTRUE(HeVAL(ent))) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(HeVAL(ent))) { 0 0 0
616 STRLEN klen;
617 0 ▶ char *kstr = HePV(ent, klen);
Branch: 1 branch
BranchTrueFalseTotal
char *kstr = HePV(ent, klen); 0 0 0
618 0 ▶ (void)hv_store(dec->allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0);
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_store(dec->allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
(void)hv_store(dec->allow_hash, kstr, HeUTF8(ent) ? -(I32)klen : (I32)klen, &PL_sv_yes, 0); 0 0 0
619 }
620 }
621 }
622 0 ▶ else if (rv && SvTYPE(rv) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
else if (rv && SvTYPE(rv) == SVt_PVCV) { 0 0 0
623 0 dec->allow_cb = newRV_inc(rv);
624 }
625 else {
626 0 croak("The 'allow_classes' option must be an array ref, hash ref or code ref");
627 }
628 }
629  
630 44 ▶ if (paranoid) {
Branch: 1 branch
BranchTrueFalseTotal
if (paranoid) { 0 44 44
631 0 ▶ if (!max_recursion_depth_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_recursion_depth_set) 0 0 0
632 0 dec->max_recursion_depth = PARANOID_MAX_RECUR_DEPTH;
633 0 ▶ if (!max_uncompressed_size_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_uncompressed_size_set) 0 0 0
634 0 dec->max_uncompressed_size = PARANOID_MAX_UNCOMPRESSED_SIZE;
635 0 ▶ if (!max_string_length_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_string_length_set) 0 0 0
636 0 dec->max_string_length = PARANOID_MAX_STRING_LENGTH;
637 0 ▶ if (!max_array_entries_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_array_entries_set) 0 0 0
638 0 dec->max_num_array_entries = PARANOID_MAX_ARRAY_ENTRIES;
639 0 ▶ if (!max_hash_entries_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!max_hash_entries_set) 0 0 0
640 0 dec->max_num_hash_entries = PARANOID_MAX_HASH_ENTRIES;
641 0 ▶ if (!validate_utf8_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_utf8_set) 0 0 0
642 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8);
643 0 ▶ if (!unknown_option_policy_value)
Branch: 1 branch
BranchTrueFalseTotal
if (!unknown_option_policy_value) 0 0 0
644 0 unknown_option_policy_value = sv_2mortal(newSVpvs("die"));
645 0 ▶ if (!refuse_objects_set && !dec->allow_hash && !dec->allow_cb)
Branch: 3 branches
BranchTrueFalseTotal
if (!refuse_objects_set && !dec->allow_hash && !dec->allow_cb) 0 0 0
if (!refuse_objects_set && !dec->allow_hash && !dec->allow_cb) 0 0 0
if (!refuse_objects_set && !dec->allow_hash && !dec->allow_cb) 0 0 0
646 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS);
647 0 ▶ if (!no_thaw_set && !dec->thaw_allow_hash && !dec->thaw_allow_cb)
Branch: 3 branches
BranchTrueFalseTotal
if (!no_thaw_set && !dec->thaw_allow_hash && !dec->thaw_allow_cb) 0 0 0
if (!no_thaw_set && !dec->thaw_allow_hash && !dec->thaw_allow_cb) 0 0 0
if (!no_thaw_set && !dec->thaw_allow_hash && !dec->thaw_allow_cb) 0 0 0
648 0 no_thaw = 1;
649 }
650  
651 44 srl_handle_unknown_option_keys(aTHX_ unknown_option_keys, unknown_option_policy_value,
652 "Sereal::Decoder", "Sereal::Decoder::UnknownOptionPolicy");
653  
654 44 ▶ if (no_thaw)
Branch: 1 branch
BranchTrueFalseTotal
if (no_thaw) 0 44 44
655 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_NO_THAW_OBJECTS);
656 44 ▶ if (no_bless) {
Branch: 1 branch
BranchTrueFalseTotal
if (no_bless) { 0 44 44
657 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_NO_BLESS_OBJECTS);
658 0 ▶ if (!no_thaw_set)
Branch: 1 branch
BranchTrueFalseTotal
if (!no_thaw_set) 0 0 0
659 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_NO_THAW_OBJECTS);
660 }
661  
662 /* Paranoid requires authentication unless the caller explicitly
663 * selects a different policy. Package defaults cannot silently
664 * weaken this preset. */
665 44 ▶ if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value)))
Branch: 3 branches
BranchTrueFalseTotal
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 44 44
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 0 0
if (paranoid && (!checksum_policy_value || !SvOK(checksum_policy_value))) 0 0 0
666 0 checksum_policy_value = sv_2mortal(newSVpvs("require_key"));
667 44 checksum_policy = srl_get_checksum_policy(
668 aTHX_ checksum_policy_value, "Sereal::Decoder::ChecksumPolicy");
669 44 ▶ if (!checksum_key)
Branch: 1 branch
BranchTrueFalseTotal
if (!checksum_key) 44 0 44
670 44 checksum_key = srl_get_default_checksum_key(aTHX_ "Sereal::Decoder::DefaultKey");
671 44 srl_validate_checksum_key(aTHX_ checksum_key, "checksum_key", checksum_policy);
672 44 srl_validate_checksum_required_key(aTHX_ checksum_key, checksum_policy);
673 44 dec->checksum.policy = checksum_policy;
674 44 dec->undef_deadly_policy = undef_deadly_policy;
675 44 srl_checksum_store_key(aTHX_ dec->checksum.key, &dec->checksum.has_key,
676 checksum_key, "checksum_key");
677  
678 /* alias_varint_under implies smallint aliasing; explicit alias_smallint
679 * is used only when the varint threshold did not enable it already. */
680 44 val = alias_varint_under_value;
681 44 ▶ if (val && SvTRUE(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvTRUE(val)) { 0 44 44
if (val && SvTRUE(val)) { 0 0 0
682 0 UV max_alias_varint_under = (UV)IV_MAX;
683 0 UV alias_varint_under = SvUV(val);
684 0 UV max_u32 = (UV)(U32)~(U32)0;
685 0 ▶ if (max_alias_varint_under > max_u32)
Branch: 1 branch
BranchTrueFalseTotal
if (max_alias_varint_under > max_u32) 0 0 0
686 0 max_alias_varint_under = max_u32;
687 0 max_alias_varint_under -= 16;
688 0 ▶ if (expect_false(alias_varint_under > max_alias_varint_under))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(alias_varint_under > max_alias_varint_under)) 0 0 0
689 0 croak("The 'alias_varint_under' option is too large for the decoder's alias cache");
690 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_ALIAS_SMALLINT);
691 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_ALIAS_VARINT);
692 0 ▶ dec->alias_varint_under = alias_varint_under < 16 ? 16 : (IV)alias_varint_under;
Branch: 1 branch
BranchTrueFalseTotal
dec->alias_varint_under = alias_varint_under < 16 ? 16 : (IV)alias_varint_under; 0 0 0
693 0 dec->alias_cache = newAV();
694 }
695 else {
696 44 val = alias_smallint_value;
697 44 ▶ if (val && SvTRUE(val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && SvTRUE(val)) { 0 44 44
if (val && SvTRUE(val)) { 0 0 0
698 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_ALIAS_SMALLINT);
699 0 dec->alias_cache = newAV();
700 0 dec->alias_varint_under = 16;
701 }
702 }
703  
704 44 ▶ if (dec->alias_varint_under) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->alias_varint_under) { 0 44 44
705 0 av_extend(dec->alias_cache, 16 + dec->alias_varint_under);
706 0 AvFILLp(dec->alias_cache)= 16 + dec->alias_varint_under - 1;
707 }
708 }
709 44 ▶ dec->flags_readonly= SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_SET_READONLY ) ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
dec->flags_readonly= SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_SET_READONLY ) ? 1 : 36 8 44
710 36 SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_SET_READONLY_SCALARS) ? 2 :
711 0;
712 /* Keep the completed decoder when leaving the constructor's cleanup
713 * scope. Its empty buffer makes the reuse hook a no-op. The caller
714 * decides whether subsequent decodes should reuse this structure. */
715 44 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_REUSE);
716 44 LEAVE;
717 44 SRL_DEC_UNSET_OPTION(dec, SRL_F_DECODER_REUSE);
718 44 return dec;
719 }
720  
721 /* Clone a decoder whilst resetting ephemeral state on the clone. */
722 SRL_STATIC_INLINE srl_decoder_t *
723 0 srl_build_decoder_struct_alike(pTHX_ srl_decoder_t *proto)
724 {
725 srl_decoder_t *dec;
726  
727 0 Newxz(dec, 1, srl_decoder_t);
728  
729 0 dec->ref_seenhash = PTABLE_new();
730 0 dec->max_recursion_depth = proto->max_recursion_depth;
731 0 dec->max_num_hash_entries = proto->max_num_hash_entries;
732 0 dec->max_num_array_entries = proto->max_num_array_entries;
733 0 dec->max_string_length = proto->max_string_length;
734 0 dec->max_uncompressed_size = proto->max_uncompressed_size;
735 0 dec->checksum = proto->checksum;
736 0 dec->undef_deadly_policy = proto->undef_deadly_policy;
737  
738 0 ▶ if (proto->alias_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->alias_cache) { 0 0 0
739 0 dec->alias_cache = proto->alias_cache;
740 0 SvREFCNT_inc(dec->alias_cache);
741 }
742  
743 /* thaw allow-list is immutable config; share it with the clone. */
744 0 ▶ if (proto->thaw_allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->thaw_allow_hash) { 0 0 0
745 0 dec->thaw_allow_hash = proto->thaw_allow_hash;
746 0 SvREFCNT_inc((SV*)dec->thaw_allow_hash);
747 }
748 0 ▶ if (proto->thaw_allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->thaw_allow_cb) { 0 0 0
749 0 dec->thaw_allow_cb = proto->thaw_allow_cb;
750 0 SvREFCNT_inc(dec->thaw_allow_cb);
751 }
752 0 ▶ if (proto->allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->allow_hash) { 0 0 0
753 0 dec->allow_hash = proto->allow_hash;
754 0 SvREFCNT_inc((SV*)dec->allow_hash);
755 }
756 0 ▶ if (proto->allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->allow_cb) { 0 0 0
757 0 dec->allow_cb = proto->allow_cb;
758 0 SvREFCNT_inc(dec->allow_cb);
759 }
760 0 ▶ if (proto->track_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto->track_classes) { 0 0 0
761 0 dec->track_classes = proto->track_classes;
762 0 SvREFCNT_inc((SV*)dec->track_classes);
763 }
764  
765 0 SRL_RDR_CLEAR(&dec->buf);
766 0 dec->pbuf = &dec->buf;
767 0 dec->zstd_dctx = NULL;
768 0 dec->flags = proto->flags;
769 0 dec->flags_readonly = proto->flags_readonly;
770 0 dec->alias_varint_under = proto->alias_varint_under;
771 0 SRL_DEC_RESET_VOLATILE_FLAGS(dec);
772  
773 0 return dec;
774 }
775  
776 /* Explicit destructor */
777 void
778 44 srl_destroy_decoder(pTHX_ srl_decoder_t *dec)
779 {
780 44 ▶ if (dec->zstd_dctx != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->zstd_dctx != NULL) { 0 44 44
781 0 ZSTD_freeDCtx((ZSTD_DCtx *)dec->zstd_dctx);
782 0 dec->zstd_dctx = NULL;
783 }
784 44 PTABLE_free(dec->ref_seenhash);
785 44 ▶ if (dec->ref_stashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_stashes) { 2 42 44
786 2 PTABLE_free(dec->ref_stashes);
787 2 PTABLE_free(dec->ref_bless_av);
788 }
789 44 ▶ if (dec->weakref_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->weakref_av) { 0 44 44
790 0 SvREFCNT_dec(dec->weakref_av);
791 0 dec->weakref_av = NULL;
792 }
793 44 ▶ if (dec->ref_thawhash)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_thawhash) 0 44 44
794 0 PTABLE_free(dec->ref_thawhash);
795 44 ▶ if (dec->thaw_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->thaw_av) { 0 44 44
796 0 SvREFCNT_dec(dec->thaw_av);
797 0 dec->thaw_av = NULL;
798 }
799 44 ▶ if (dec->alias_cache)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->alias_cache) 0 44 44
800 0 SvREFCNT_dec(dec->alias_cache);
801 44 ▶ if (dec->thaw_allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->thaw_allow_hash) { 0 44 44
802 0 SvREFCNT_dec((SV*)dec->thaw_allow_hash);
803 0 dec->thaw_allow_hash = NULL;
804 }
805 44 ▶ if (dec->thaw_allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->thaw_allow_cb) { 0 44 44
806 0 SvREFCNT_dec(dec->thaw_allow_cb);
807 0 dec->thaw_allow_cb = NULL;
808 }
809 44 ▶ if (dec->allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->allow_hash) { 0 44 44
810 0 SvREFCNT_dec((SV*)dec->allow_hash);
811 0 dec->allow_hash = NULL;
812 }
813 44 ▶ if (dec->allow_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->allow_cb) { 0 44 44
814 0 SvREFCNT_dec(dec->allow_cb);
815 0 dec->allow_cb = NULL;
816 }
817 44 ▶ if (dec->track_classes) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->track_classes) { 0 44 44
818 0 SvREFCNT_dec((SV*)dec->track_classes);
819 0 dec->track_classes = NULL;
820 }
821 44 Safefree(dec);
822 44 }
823  
824 /* This is fired when we exit the Perl pseudo-block.
825 * It frees our decoder and all. Put decoder-level cleanup
826 * logic here so that we can simply use croak/longjmp for
827 * exception handling. Makes life vastly easier!
828 */
829 void
830 293 srl_decoder_destructor_hook(pTHX_ void *p)
831 {
832 293 srl_decoder_t *dec = (srl_decoder_t *)p;
833  
834 /* Only free decoder if not for reuse */
835 293 ▶ if (!SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REUSE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REUSE)) { 0 293 293
836 0 srl_destroy_decoder(aTHX_ dec);
837 }
838 else {
839 /* Clear instead - decoder reused */
840 293 srl_clear_decoder(aTHX_ dec);
841 }
842 293 }
843  
844 /* Caller-supplied output scalars may already hold a value: a string
845 * (possibly copy-on-write), a reference, an object. The value readers
846 * expect a fresh scalar and write into it directly, so reset it the way
847 * an assignment would. That releases a previous referent (running its
848 * DESTROY), drops old string buffers and flags, and refuses read-only
849 * destinations such as a literal undef argument. Fresh mortals are
850 * untouched. Must run after an input that aliases one of the outputs has
851 * been copied, and before srl_begin_decoding() sets up the input buffer:
852 * a DESTROY run by the reset may modify or free the input scalar's
853 * string, so the buffer must only be captured afterwards. */
854 SRL_STATIC_INLINE void
855 494 srl_reset_output_sv(pTHX_ SV *into)
856 {
857 494 ▶ if (!into)
Branch: 1 branch
BranchTrueFalseTotal
if (!into) 239 255 494
858 239 return;
859 255 ▶ if (SvREADONLY(into))
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(into)) 0 255 255
860 0 croak("%s", PL_no_modify);
861 /* Assigning undef to a glob warns before the later type check runs. */
862 255 ▶ if (SvTYPE(into) == SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(into) == SVt_PVGV) 0 255 255
863 0 croak("Sereal: Error: cannot decode into a GLOB");
864 /* Perl 5.24's sv_setsv() can mortalize the old referent instead of
865 * releasing it immediately. Run a strong referent's DESTROY now,
866 * before capturing the input buffer, on every supported Perl. Weak
867 * references do not own their referents and need only the reset below. */
868 255 ▶ if (SvROK(into) && !SvWEAKREF(into))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(into) && !SvWEAKREF(into)) 14 241 255
if (SvROK(into) && !SvWEAKREF(into)) 14 0 14
869 14 sv_unref_flags(into, SV_IMMEDIATE_UNREF);
870 255 ▶ if (SvTYPE(into) != SVt_NULL || SvOK(into))
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(into) != SVt_NULL || SvOK(into)) 32 223 255
if (SvTYPE(into) != SVt_NULL || SvOK(into)) 0 32 32
871 223 sv_setsv(into, &PL_sv_undef);
872 /* Assignment turns a regexp copy back into a plain scalar; anything
873 * still not one (an array, hash, code or compiled regexp body) would
874 * be written through by the value readers. */
875 255 ▶ if (expect_false( SvTYPE(into) > SVt_PVMG ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SvTYPE(into) > SVt_PVMG )) 0 255 255
876 0 croak("Sereal: Error: cannot decode into a %s", sv_reftype(into, 0));
877 }
878  
879 /* Logic shared by the various decoder entry points. */
880 SRL_STATIC_INLINE void
881 245 srl_decode_into_internal(pTHX_ srl_decoder_t *origdec, SV *src, SV *header_into, SV *body_into, UV start_offset)
882 {
883 srl_decoder_t *dec;
884 int aliased;
885  
886 assert(origdec != NULL);
887 /* Resetting either output can run DESTROY and remove the last owning
888 * reference to any of these scalars. Retain the arguments before that
889 * happens; capture the input PV only after the resets, as before. */
890 245 SvREFCNT_inc_simple_void_NN(src);
891 245 SAVEFREESV(src);
892 245 ▶ if (header_into) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_into) { 6 239 245
893 6 SvREFCNT_inc_simple_void_NN(header_into);
894 6 SAVEFREESV(header_into);
895 }
896 245 ▶ if (body_into) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_into) { 245 0 245
897 245 SvREFCNT_inc_simple_void_NN(body_into);
898 245 SAVEFREESV(body_into);
899 }
900 245 ▶ aliased = (src == body_into || src == header_into);
Branch: 2 branches
BranchTrueFalseTotal
aliased = (src == body_into || src == header_into); 245 0 245
aliased = (src == body_into || src == header_into); 0 245 245
901 245 ▶ if (expect_false( SRL_DEC_HAVE_OPTION(origdec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL)
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( SRL_DEC_HAVE_OPTION(origdec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL) 0 245 245
if (expect_false( SRL_DEC_HAVE_OPTION(origdec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL) 0 0 0
if (expect_false( SRL_DEC_HAVE_OPTION(origdec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL) 0 245 245
902 && aliased ))
903 /* No input buffer is set up yet (origdec's may be NULL), so report
904 * offset 1 directly as SRL_RDR_ERROR() would for a fresh buffer. */
905 0 croak(SRL_RDR_BASE_ERROR_FORMAT("%s"),
906 "Incremental decode input and output must be different scalars",
907 (UV)1, __FILE__, __LINE__);
908 /* Copy an aliased input and reset the outputs before the input buffer
909 * is captured; see srl_reset_output_sv(). */
910 245 ▶ if (aliased)
Branch: 1 branch
BranchTrueFalseTotal
if (aliased) 0 245 245
911 0 src = sv_2mortal(newSVsv(src));
912 245 srl_reset_output_sv(aTHX_ header_into);
913 245 srl_reset_output_sv(aTHX_ body_into);
914 245 dec = srl_begin_decoding(aTHX_ origdec, src, start_offset, 0);
915 245 srl_read_header(aTHX_ dec, header_into);
916 /* Header user data may have run callbacks (THAW, thaw_allow_classes,
917 * DESTROY) that modified the input; the body is read from it next. */
918 245 srl_assert_input_unchanged(aTHX_ dec);
919 245 ▶ if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_SNAPPY) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_SNAPPY) )) { 0 245 245
920 0 dec->bytes_consumed = srl_decompress_body_snappy(aTHX_ dec->pbuf, dec->encoding_flags, NULL, dec->max_uncompressed_size);
921 0 origdec->bytes_consumed = dec->bytes_consumed;
922 245 ▶ } else if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_ZLIB) )) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_ZLIB) )) { 1 244 245
923 1 dec->bytes_consumed = srl_decompress_body_zlib(aTHX_ dec->pbuf, NULL, dec->max_uncompressed_size);
924 0 origdec->bytes_consumed = dec->bytes_consumed;
925 244 ▶ } else if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_ZSTD) )) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DECOMPRESS_ZSTD) )) { 1 243 244
926 1 dec->bytes_consumed = srl_decompress_body_zstd_ctx(aTHX_ dec->pbuf, NULL, dec->max_uncompressed_size,
927 1 ▶ SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REUSE) ? &dec->zstd_dctx : NULL);
Branch: 1 branch
BranchTrueFalseTotal
SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REUSE) ? &dec->zstd_dctx : NULL); 1 0 1
928 0 origdec->bytes_consumed = dec->bytes_consumed;
929 }
930  
931 /* this function *MUST* be called right after srl_decompress* functions */
932 243 ▶ SRL_RDR_UPDATE_BODY_POS(dec->pbuf, dec->proto_version);
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_UPDATE_BODY_POS(dec->pbuf, dec->proto_version); 0 243 243
933 243 dec->min_backref_offset = (UV)(dec->buf.pos - dec->buf.body_pos);
934  
935 243 srl_read_declarations(aTHX_ dec->pbuf, dec->proto_version,
936 243 SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_ALLOW_UNKNOWN_LONG_DOUBLE) != 0);
937 243 dec->min_backref_offset = (UV)(dec->buf.pos - dec->buf.body_pos);
938  
939 /* The actual document body deserialization: */
940 243 srl_read_single_value(aTHX_ dec, body_into, NULL);
941 243 ▶ if (dec->proto_version >= 7 && SRL_RDR_SPACE_LEFT(dec->pbuf) >= 2
Branch: 2 branches
BranchTrueFalseTotal
if (dec->proto_version >= 7 && SRL_RDR_SPACE_LEFT(dec->pbuf) >= 2 0 243 243
if (dec->proto_version >= 7 && SRL_RDR_SPACE_LEFT(dec->pbuf) >= 2 0 0 0
942 0 ▶ && ((*dec->buf.pos & ~SRL_HDR_TRACK_FLAG) == SRL_HDR_EXTEND)
Branch: 1 branch
BranchTrueFalseTotal
&& ((*dec->buf.pos & ~SRL_HDR_TRACK_FLAG) == SRL_HDR_EXTEND) 0 0 0
943 0 ▶ && dec->buf.pos[1] == SRL_EXT_DECLARE)
Branch: 1 branch
BranchTrueFalseTotal
&& dec->buf.pos[1] == SRL_EXT_DECLARE) 0 0 0
944 0 SRL_RDR_ERROR(dec->pbuf, "Declaration outside document prefix");
945 243 ▶ if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE))) { 2 241 243
946 2 srl_finalize_structure(aTHX_ dec);
947 }
948  
949 /* If we aren't reading from a decompressed buffer we have to remember the number
950 * of bytes used for the user to query. */
951 243 ▶ if (dec->bytes_consumed == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->bytes_consumed == 0) { 243 0 243
952 243 dec->bytes_consumed = dec->buf.pos - dec->buf.start;
953 243 origdec->bytes_consumed = dec->bytes_consumed;
954 }
955  
956 243 ▶ if (expect_false(dec->packet_has_checksum
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(dec->packet_has_checksum 0 243 243
if (expect_false(dec->packet_has_checksum 0 0 0
if (expect_false(dec->packet_has_checksum 0 243 243
957 && dec->bytes_consumed != dec->checksummed_packet_length))
958 0 SRL_RDR_ERRORf2(dec->pbuf,
959 "Checksummed Sereal payload length mismatch (%"UVuf" bytes consumed, packet length is %"UVuf" bytes)",
960 dec->bytes_consumed, dec->checksummed_packet_length);
961  
962 243 ▶ if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL)) { 0 243 243
963 STRLEN len;
964 0 char *pv = SvPVbyte(src, len);
965 0 ▶ if (start_offset > len || dec->bytes_consumed > len - start_offset)
Branch: 2 branches
BranchTrueFalseTotal
if (start_offset > len || dec->bytes_consumed > len - start_offset) 0 0 0
if (start_offset > len || dec->bytes_consumed > len - start_offset) 0 0 0
966 0 SRL_RDR_ERROR(dec->pbuf, "Consumed bytes exceed incremental input length");
967 0 sv_chop(src, pv + start_offset + dec->bytes_consumed);
968 }
969  
970 243 srl_clear_decoder(aTHX_ dec);
971 243 }
972  
973 /* This is the main routine to deserialize just the header of a document. */
974 SV *
975 4 srl_decode_header_into(pTHX_ srl_decoder_t *origdec, SV *src, SV* header_into, UV start_offset)
976 {
977 srl_decoder_t *dec;
978 assert(origdec != NULL);
979 4 SvREFCNT_inc_simple_void_NN(src);
980 4 SAVEFREESV(src);
981 4 ▶ if (header_into) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_into) { 4 0 4
982 4 SvREFCNT_inc_simple_void_NN(header_into);
983 4 SAVEFREESV(header_into);
984 }
985 4 ▶ if (header_into == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (header_into == NULL) { 0 4 4
986 0 header_into = sv_newmortal();
987 }
988 else {
989 /* Copy an aliased input and reset the output before the input
990 * buffer is captured; see srl_reset_output_sv(). */
991 4 ▶ if (src == header_into)
Branch: 1 branch
BranchTrueFalseTotal
if (src == header_into) 0 4 4
992 0 src = sv_2mortal(newSVsv(src));
993 4 srl_reset_output_sv(aTHX_ header_into);
994 }
995 4 dec = srl_begin_decoding(aTHX_ origdec, src, start_offset, 0);
996 4 srl_read_header(aTHX_ dec, header_into);
997 4 return header_into;
998 }
999  
1000 /* this SHOULD be newSV_type(SVt_NULL) but newSV(0) is faster :-( */
1001 #if 1
1002 #define FRESH_SV() newSV(0)
1003 #else
1004 #define FRESH_SV() newSV_type(SVt_NULL);
1005 #endif
1006  
1007 /* This is the main routine to deserialize a Sereal document
1008 * w/o data in header. */
1009 SV *
1010 239 srl_decode_into(pTHX_ srl_decoder_t *dec, SV *src, SV* body_into, UV start_offset)
1011 {
1012 239 ▶ if (expect_true(!body_into))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true(!body_into)) 0 239 239
1013 0 body_into= sv_2mortal(FRESH_SV());
1014 239 srl_decode_into_internal(aTHX_ dec, src, NULL, body_into, start_offset);
1015 237 return body_into;
1016 }
1017  
1018 /* This is the main routine to deserialize Sereal document body
1019 * and header all at once. */
1020 void
1021 6 srl_decode_all_into(pTHX_ srl_decoder_t *dec, SV *src, SV *header_into, SV *body_into, UV start_offset)
1022 {
1023 assert(header_into != NULL);
1024 assert(body_into != NULL);
1025 6 (void)srl_decode_into_internal(aTHX_ dec, src, header_into, body_into, start_offset);
1026 6 }
1027  
1028  
1029 /* TOP LEVEL PRIVATE ROUTINES */
1030  
1031 SRL_STATIC_INLINE void
1032 536 srl_clear_decoder(pTHX_ srl_decoder_t *dec)
1033 {
1034 536 ▶ if (dec->buf.start == dec->buf.end)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->buf.start == dec->buf.end) 287 249 536
1035 287 return;
1036  
1037 249 srl_clear_decoder_body_state(aTHX_ dec);
1038 249 SRL_DEC_RESET_VOLATILE_FLAGS(dec);
1039 249 dec->buf.body_pos = dec->buf.start = dec->buf.end = dec->buf.pos = dec->save_pos = NULL;
1040 }
1041  
1042 void
1043 258 srl_clear_decoder_body_state(pTHX_ srl_decoder_t *dec)
1044 {
1045 258 SRL_DEC_UNSET_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE);
1046  
1047 258 ▶ if (dec->weakref_av)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->weakref_av) 0 258 258
1048 0 av_clear(dec->weakref_av);
1049  
1050 258 PTABLE_clear(dec->ref_seenhash);
1051 258 ▶ if (dec->ref_stashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_stashes) { 2 256 258
1052 2 PTABLE_clear(dec->ref_stashes);
1053 2 PTABLE_clear(dec->ref_bless_av);
1054 }
1055  
1056 258 srl_clear_thaw_state(aTHX_ dec);
1057  
1058 258 dec->recursion_depth = 0;
1059 258 dec->owed_bytes = 0;
1060 258 }
1061  
1062 /* Discard the pending frozen-object bookkeeping.
1063 *
1064 * thaw_av holds the frozen items awaiting a THAW call, and ref_thawhash maps the
1065 * address of each frozen item's argument AV to its class stash (see the
1066 * PTABLE_store in srl_read_frozen_object). Both are consumed by
1067 * srl_finalize_structure, which frees those argument AVs as it goes.
1068 *
1069 * Neither may survive into another document. An entry left in ref_thawhash is
1070 * keyed on an address perl is free to recycle, so a later PTABLE_find can match
1071 * an unrelated AV and hand back a stale class stash -- or a mortal info AV that
1072 * died documents ago. An item left in thaw_av gets thawed as part of the *next*
1073 * document, operating on an SV that belongs to the previous one.
1074 *
1075 * Both happen in practice when a decode dies before reaching finalize, e.g. on a
1076 * truncated document: srl_read_frozen_object has already pushed onto thaw_av by
1077 * the time the buffer runs out. */
1078 SRL_STATIC_INLINE void
1079 507 srl_clear_thaw_state(pTHX_ srl_decoder_t *dec)
1080 {
1081 507 ▶ if (dec->ref_thawhash)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_thawhash) 0 507 507
1082 0 PTABLE_clear(dec->ref_thawhash);
1083 507 ▶ if (dec->thaw_av)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->thaw_av) 0 507 507
1084 0 av_clear(dec->thaw_av);
1085 507 }
1086  
1087 SRL_STATIC_INLINE srl_decoder_t *
1088 249 srl_begin_decoding(pTHX_ srl_decoder_t *dec, SV *src, UV start_offset, int copy_input)
1089 {
1090 STRLEN len;
1091 unsigned char *tmp;
1092  
1093 /* Check whether decoder is in use and create a new one on the
1094 * fly if necessary. Should only happen in edge cases such as
1095 * a THAW hook calling back into the same decoder. */
1096 249 ▶ if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DIRTY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DIRTY)) { 0 249 249
1097 0 srl_decoder_t * const proto = dec;
1098 0 dec = srl_build_decoder_struct_alike(aTHX_ proto);
1099 0 SRL_DEC_UNSET_OPTION(dec, SRL_F_DECODER_REUSE);
1100 }
1101  
1102 /* Needs to be before setting DIRTY because DIRTY is volatile. */
1103 249 SRL_DEC_RESET_VOLATILE_FLAGS(dec);
1104  
1105 /* Set to being in use. */;
1106 249 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_DIRTY);
1107  
1108 /* Start from a clean slate regardless of how the previous decode on this
1109 * decoder ended. srl_clear_decoder() returns early when buf.start ==
1110 * buf.end, so cleanup on the way out is not guaranteed to have run -- and
1111 * after a croak there may have been no cleanup at all. Leftover frozen-object
1112 * state is not merely stale, it is a use-after-free waiting to happen
1113 * (see srl_clear_thaw_state). */
1114 249 srl_clear_thaw_state(aTHX_ dec);
1115  
1116 /* Register our structure for destruction on scope exit */
1117 249 SAVEDESTRUCTOR_X(&srl_decoder_destructor_hook, (void *)dec);
1118  
1119 /* Copy only when the API's output aliases its input, or when a UTF-8
1120 * downgrade must leave the caller's scalar alone. newSVsv does not steal
1121 * the PV of a temporary input (unlike sv_mortalcopy on newer Perls).
1122 * Application callbacks must not modify input during decoding. */
1123 249 ▶ if (copy_input || (SvUTF8(src)
Branch: 2 branches
BranchTrueFalseTotal
if (copy_input || (SvUTF8(src) 249 0 249
if (copy_input || (SvUTF8(src) 0 249 249
1124 0 ▶ && !SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL)))
Branch: 1 branch
BranchTrueFalseTotal
&& !SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_DESTRUCTIVE_INCREMENTAL))) 0 0 0
1125 0 src = sv_2mortal(newSVsv(src));
1126 249 ▶ if (SvUTF8(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(src)) 0 249 249
1127 0 sv_utf8_downgrade(src, 0);
1128  
1129 /* Hold a reference to the input for the duration of the decode:
1130 * resetting an output scalar or application code running inside a
1131 * callback must not be able to free the scalar we are reading. */
1132 249 SvREFCNT_inc_simple_void_NN(src);
1133 249 SAVEFREESV(src);
1134  
1135 249 tmp = (unsigned char*)SvPV(src, len);
1136 249 ▶ if (expect_false( start_offset > len )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( start_offset > len )) { 0 249 249
1137 /* dec->buf is not set up for this input yet, so report offset 1
1138 * directly as SRL_RDR_ERROR() would for a fresh buffer. */
1139 0 croak(SRL_RDR_BASE_ERROR_FORMAT("Start offset %"UVuf" is beyond input string length %"UVuf),
1140 (UV)start_offset, (UV)len, (UV)1, __FILE__, __LINE__);
1141 }
1142 /* Only track the input when the buffer is its own PV (not, say, the
1143 * result of an overloaded stringification held elsewhere). */
1144 249 ▶ dec->input_sv = (SvPOKp(src) && (unsigned char *)SvPVX(src) == tmp) ? src : NULL;
Branch: 2 branches
BranchTrueFalseTotal
dec->input_sv = (SvPOKp(src) && (unsigned char *)SvPVX(src) == tmp) ? src : NULL; 249 0 249
dec->input_sv = (SvPOKp(src) && (unsigned char *)SvPVX(src) == tmp) ? src : NULL; 249 0 249
1145 249 dec->input_start = tmp;
1146 249 dec->input_len = len;
1147 249 dec->buf.start= dec->buf.pos= tmp + start_offset;
1148 249 dec->buf.end= dec->buf.start + len - start_offset;
1149 249 dec->packet_has_checksum = 0;
1150 249 dec->checksummed_packet_length = 0;
1151 249 dec->checksummed_payload = NULL;
1152 249 SRL_RDR_SET_BODY_POS(dec->pbuf, dec->buf.start);
1153 249 dec->bytes_consumed = 0;
1154  
1155 249 return dec;
1156 }
1157  
1158 IV
1159 0 srl_validate_header_version_pv_len(pTHX_ char *strdata, STRLEN len)
1160 {
1161 0 return srl_validate_header_version(aTHX_ (srl_reader_char_ptr) strdata, len);
1162 }
1163  
1164 SRL_STATIC_INLINE U32
1165 srl_read_u32_le(const U8 *p)
1166 {
1167 return ((U32)p[0]) | ((U32)p[1] << 8) |
1168 ((U32)p[2] << 16) | ((U32)p[3] << 24);
1169 }
1170  
1171 SRL_STATIC_INLINE void
1172 0 srl_process_checksum_envelope(pTHX_ srl_decoder_t *dec)
1173 {
1174 0 srl_reader_buffer_t *buf = dec->pbuf;
1175 srl_checksum_envelope_t envelope;
1176 0 srl_read_checksum_envelope(aTHX_ buf, &envelope);
1177 0 srl_checksum_apply_policy(aTHX_ buf, &envelope, &dec->checksum);
1178  
1179 0 dec->checksummed_packet_length = (UV)((envelope.payload - buf->start) + envelope.payload_len);
1180 0 dec->checksummed_payload = envelope.payload;
1181 0 buf->pos = (U8 *)envelope.payload;
1182 0 buf->end = (U8 *)envelope.payload + envelope.payload_len;
1183 0 }
1184  
1185 /* Inspect framing only: no decoder construction, decompression, callbacks,
1186 * policy-dependent fallback, or changes to the supplied packet. */
1187 SRL_STATIC_INLINE int
1188 0 srl_inspect_checksum(pTHX_ SV *src, UV offset, srl_reader_buffer_t *buf, srl_checksum_envelope_t *envelope)
1189 {
1190 STRLEN len;
1191 char *bytes;
1192 IV header;
1193 U8 version, encoding;
1194 UV suffix_len;
1195 0 SvGETMAGIC(src);
1196 0 ▶ if (SvROK(src))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(src)) 0 0 0
1197 0 croak("Cannot inspect a reference as a Sereal packet");
1198 0 bytes = SvPVbyte(src, len);
1199 0 ▶ if (offset > len)
Branch: 1 branch
BranchTrueFalseTotal
if (offset > len) 0 0 0
1200 0 croak("Sereal checksum offset is outside the input");
1201 0 SRL_RDR_CLEAR(buf);
1202 0 buf->start = buf->pos = (U8 *)bytes + offset;
1203 0 buf->end = (U8 *)bytes + len;
1204 0 header = srl_validate_header_version(aTHX_ buf->pos, (STRLEN)SRL_RDR_SPACE_LEFT(buf));
1205 0 ▶ if (header < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (header < 1) 0 0 0
1206 0 SRL_RDR_ERROR(buf, "Bad Sereal header while inspecting checksum");
1207 0 version = (U8)header & SRL_PROTOCOL_VERSION_MASK;
1208 0 encoding = (U8)header & SRL_PROTOCOL_ENCODING_BASE_MASK;
1209 0 ▶ if (version > SRL_PROTOCOL_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (version > SRL_PROTOCOL_VERSION) 0 0 0
1210 0 SRL_RDR_ERRORf1(buf, "Unsupported Sereal protocol version %u", version);
1211 0 ▶ if (encoding > SRL_PROTOCOL_ENCODING_ZSTD)
Branch: 1 branch
BranchTrueFalseTotal
if (encoding > SRL_PROTOCOL_ENCODING_ZSTD) 0 0 0
1212 0 SRL_RDR_ERROR(buf, "Unknown Sereal encoding while inspecting checksum");
1213 0 ▶ if (((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM) && version < 6)
Branch: 2 branches
BranchTrueFalseTotal
if (((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM) && version < 6) 0 0 0
if (((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM) && version < 6) 0 0 0
1214 0 SRL_RDR_ERROR(buf, "Checksummed Sereal documents require protocol version 6 or later");
1215 0 ▶ if ((encoding == SRL_PROTOCOL_ENCODING_SNAPPY && version >= 2
Branch: 2 branches
BranchTrueFalseTotal
if ((encoding == SRL_PROTOCOL_ENCODING_SNAPPY && version >= 2 0 0 0
if ((encoding == SRL_PROTOCOL_ENCODING_SNAPPY && version >= 2 0 0 0
1216 0 ▶ && !((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM))
Branch: 1 branch
BranchTrueFalseTotal
&& !((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM)) 0 0 0
1217 0 ▶ || (encoding == SRL_PROTOCOL_ENCODING_ZLIB && version < 3)
Branch: 2 branches
BranchTrueFalseTotal
|| (encoding == SRL_PROTOCOL_ENCODING_ZLIB && version < 3) 0 0 0
|| (encoding == SRL_PROTOCOL_ENCODING_ZLIB && version < 3) 0 0 0
1218 0 ▶ || (encoding == SRL_PROTOCOL_ENCODING_ZSTD && version < 4))
Branch: 2 branches
BranchTrueFalseTotal
|| (encoding == SRL_PROTOCOL_ENCODING_ZSTD && version < 4)) 0 0 0
|| (encoding == SRL_PROTOCOL_ENCODING_ZSTD && version < 4)) 0 0 0
1219 0 SRL_RDR_ERROR(buf, "Sereal compression is not supported by this protocol version");
1220 0 buf->pos += 5;
1221 0 suffix_len = srl_read_varint_uv(aTHX_ buf);
1222 0 ▶ if (suffix_len > (UV)SRL_RDR_SPACE_LEFT(buf))
Branch: 1 branch
BranchTrueFalseTotal
if (suffix_len > (UV)SRL_RDR_SPACE_LEFT(buf)) 0 0 0
1223 0 SRL_RDR_ERROR(buf, "Sereal header suffix exceeds the available input");
1224 0 buf->pos += suffix_len;
1225 0 ▶ if (!((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM))
Branch: 1 branch
BranchTrueFalseTotal
if (!((U8)header & SRL_PROTOCOL_ENCODING_CHECKSUM)) 0 0 0
1226 0 return 0;
1227 0 srl_read_checksum_envelope(aTHX_ buf, envelope);
1228 0 return 1;
1229 }
1230  
1231 SV *
1232 0 srl_get_sereal_checksum(pTHX_ SV *src, UV offset)
1233 {
1234 srl_reader_buffer_t buf;
1235 srl_checksum_envelope_t envelope;
1236 0 ▶ if (!srl_inspect_checksum(aTHX_ src, offset, &buf, &envelope))
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_inspect_checksum(aTHX_ src, offset, &buf, &envelope)) 0 0 0
1237 0 return newSV(0);
1238 0 return newSVpvn((const char *)envelope.tag, envelope.tag_len);
1239 }
1240  
1241 int
1242 0 srl_validate_sereal_checksum(pTHX_ SV *src, SV *key, UV offset)
1243 {
1244 srl_reader_buffer_t buf;
1245 srl_checksum_envelope_t envelope;
1246 STRLEN key_len;
1247 U8 key_bytes[16];
1248 0 int has_key = 0;
1249 /* Key FETCH/stringification can execute Perl code and mutate src. Resolve
1250 * it before retaining pointers into the packet, and copy the key bytes. */
1251 0 ▶ if (!key)
Branch: 1 branch
BranchTrueFalseTotal
if (!key) 0 0 0
1252 0 key = srl_get_default_checksum_key(aTHX_ "Sereal::Decoder::DefaultKey");
1253 0 ▶ if (key) {
Branch: 1 branch
BranchTrueFalseTotal
if (key) { 0 0 0
1254 0 SvGETMAGIC(key);
1255 0 ▶ if (SvOK(key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(key)) { 0 0 0
1256 0 const U8 *bytes = (const U8 *)SvPVbyte(key, key_len);
1257 0 ▶ if (key_len != sizeof(key_bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (key_len != sizeof(key_bytes)) 0 0 0
1258 0 croak("validate_sereal_checksum key must be exactly 16 bytes");
1259 0 Copy(bytes, key_bytes, sizeof(key_bytes), U8);
1260 0 has_key = 1;
1261 }
1262 }
1263 0 ▶ if (!srl_inspect_checksum(aTHX_ src, offset, &buf, &envelope))
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_inspect_checksum(aTHX_ src, offset, &buf, &envelope)) 0 0 0
1264 0 return 0;
1265 0 ▶ if (!srl_checksum_tag_length(envelope.checksum_type))
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_checksum_tag_length(envelope.checksum_type)) 0 0 0
1266 0 SRL_RDR_ERRORf1(&buf, "Unsupported Sereal checksum type %u", envelope.checksum_type);
1267 0 ▶ if (!has_key)
Branch: 1 branch
BranchTrueFalseTotal
if (!has_key) 0 0 0
1268 0 croak("validate_sereal_checksum requires a key or a defined Decoder or shared default checksum key");
1269 0 return srl_checksum_matches(aTHX_ &buf, &envelope, key_bytes);
1270 }
1271  
1272 /* Header user data is decoded and finalized (running THAW methods,
1273 * thaw_allow_classes callbacks and possibly DESTROY) before the body is
1274 * parsed, and the body is then read from the same input buffer. A callback
1275 * that modified the input scalar may have reallocated or freed that buffer,
1276 * so verify the PV pointer and length are unchanged before reading on.
1277 * Use SvPVX/SvCUR so that no magic is invoked. */
1278 SRL_STATIC_INLINE void
1279 245 srl_assert_input_unchanged(pTHX_ srl_decoder_t *dec)
1280 {
1281 245 SV * const sv = dec->input_sv;
1282 245 ▶ if (sv == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (sv == NULL) 0 245 245
1283 0 return;
1284 245 ▶ if (expect_false( !SvPOKp(sv)
Branch: 4 branches
BranchTrueFalseTotal
if (expect_false( !SvPOKp(sv) 245 0 245
if (expect_false( !SvPOKp(sv) 245 0 245
if (expect_false( !SvPOKp(sv) 0 245 245
if (expect_false( !SvPOKp(sv) 0 245 245
1285 || (const unsigned char *)SvPVX(sv) != dec->input_start
1286 || SvCUR(sv) != dec->input_len ))
1287 {
1288 0 SRL_RDR_ERROR(dec->pbuf, "Sereal input scalar was modified by a callback during decoding");
1289 }
1290 }
1291  
1292 SRL_STATIC_INLINE void
1293 249 srl_read_header(pTHX_ srl_decoder_t *dec, SV *header_user_data)
1294 {
1295 UV header_len;
1296 249 IV proto_version_and_encoding_flags_int= srl_validate_header_version(aTHX_ dec->buf.pos, SRL_RDR_SPACE_LEFT(dec->pbuf));
1297  
1298 249 dec->buf.long_double_size = 0;
1299 249 ▶ if ( expect_false(proto_version_and_encoding_flags_int < 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( expect_false(proto_version_and_encoding_flags_int < 1) ) { 0 249 249
1300 0 ▶ if (proto_version_and_encoding_flags_int == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (proto_version_and_encoding_flags_int == 0) 0 0 0
1301 0 SRL_RDR_ERROR(dec->pbuf, "Bad Sereal header: It seems your document was accidentally UTF-8 encoded");
1302 else
1303 0 SRL_RDR_ERROR(dec->pbuf, "Bad Sereal header: Not a valid Sereal document.");
1304 }
1305 else {
1306 249 dec->buf.pos += 5;
1307  
1308 249 dec->proto_version = (U8)(proto_version_and_encoding_flags_int & SRL_PROTOCOL_VERSION_MASK);
1309 249 dec->encoding_flags = (U8)(proto_version_and_encoding_flags_int & SRL_PROTOCOL_ENCODING_MASK);
1310 249 dec->packet_has_checksum = (dec->encoding_flags & SRL_PROTOCOL_ENCODING_CHECKSUM) != 0;
1311 249 dec->encoding_flags &= SRL_PROTOCOL_ENCODING_BASE_MASK;
1312  
1313 249 ▶ if (srl_checksum_required_mode(dec->checksum.policy) && !dec->packet_has_checksum)
Branch: 2 branches
BranchTrueFalseTotal
if (srl_checksum_required_mode(dec->checksum.policy) && !dec->packet_has_checksum) 0 249 249
if (srl_checksum_required_mode(dec->checksum.policy) && !dec->packet_has_checksum) 0 0 0
1314 0 SRL_RDR_ERRORf1(dec->pbuf, "checksum_policy '%s' requires a checksummed packet",
1315 srl_checksum_policy_name(dec->checksum.policy));
1316  
1317 249 ▶ if (expect_false(dec->packet_has_checksum && dec->proto_version < 6))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(dec->packet_has_checksum && dec->proto_version < 6)) 0 249 249
if (expect_false(dec->packet_has_checksum && dec->proto_version < 6)) 0 0 0
if (expect_false(dec->packet_has_checksum && dec->proto_version < 6)) 0 249 249
1318 0 SRL_RDR_ERROR(dec->pbuf, "Checksummed Sereal documents require protocol version 6 or later");
1319 249 ▶ if (expect_false( dec->proto_version == 1 ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( dec->proto_version == 1 )) 0 249 249
1320 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_PROTOCOL_V1); /* compat mode */
1321 249 ▶ else if (expect_false( dec->proto_version > SRL_PROTOCOL_VERSION || dec->proto_version < 1 ))
Branch: 3 branches
BranchTrueFalseTotal
else if (expect_false( dec->proto_version > SRL_PROTOCOL_VERSION || dec->proto_version < 1 )) 249 0 249
else if (expect_false( dec->proto_version > SRL_PROTOCOL_VERSION || dec->proto_version < 1 )) 0 249 249
else if (expect_false( dec->proto_version > SRL_PROTOCOL_VERSION || dec->proto_version < 1 )) 0 249 249
1322 0 SRL_RDR_ERRORf1(dec->pbuf, "Unsupported Sereal protocol version %u", dec->proto_version);
1323  
1324 249 ▶ if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_RAW) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_RAW) { 2 247 249
1325 /* no op */
1326 }
1327 else
1328 2 ▶ if ( dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY
Branch: 1 branch
BranchTrueFalseTotal
if ( dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY 2 0 2
1329 2 ▶ || dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL)
Branch: 1 branch
BranchTrueFalseTotal
|| dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL) 0 2 2
1330 {
1331 0 ▶ if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY
Branch: 1 branch
BranchTrueFalseTotal
if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY 0 0 0
1332 0 ▶ && dec->proto_version >= 2 && !dec->packet_has_checksum)
Branch: 2 branches
BranchTrueFalseTotal
&& dec->proto_version >= 2 && !dec->packet_has_checksum) 0 0 0
&& dec->proto_version >= 2 && !dec->packet_has_checksum) 0 0 0
1333 0 SRL_RDR_ERROR(dec->pbuf, "Non-incremental Snappy compression requires protocol version 1");
1334 0 ▶ if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_SNAPPY) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_SNAPPY) )) { 0 0 0
1335 0 SRL_RDR_ERROR(dec->pbuf, "Sereal document is compressed with Snappy, "
1336 "but this decoder is configured to refuse Snappy-compressed input.");
1337 }
1338 0 dec->flags |= SRL_F_DECODER_DECOMPRESS_SNAPPY;
1339 }
1340 else
1341 2 ▶ if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_ZLIB)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_ZLIB) 1 1 2
1342 {
1343 1 ▶ if (dec->proto_version < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->proto_version < 3) 0 1 1
1344 0 SRL_RDR_ERROR(dec->pbuf, "ZLIB compression requires protocol version 3 or later");
1345 1 ▶ if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_ZLIB) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_ZLIB) )) { 0 1 1
1346 0 SRL_RDR_ERROR(dec->pbuf, "Sereal document is compressed with ZLIB, "
1347 "but this decoder is configured to refuse ZLIB-compressed input.");
1348 }
1349 1 dec->flags |= SRL_F_DECODER_DECOMPRESS_ZLIB;
1350 }
1351 else
1352 1 ▶ if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_ZSTD)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->encoding_flags == SRL_PROTOCOL_ENCODING_ZSTD) 1 0 1
1353 {
1354 1 ▶ if (dec->proto_version < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->proto_version < 4) 0 1 1
1355 0 SRL_RDR_ERROR(dec->pbuf, "Zstd compression requires protocol version 4 or later");
1356 1 ▶ if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_ZSTD) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_ZSTD) )) { 0 1 1
1357 0 SRL_RDR_ERROR(dec->pbuf, "Sereal document is compressed with ZSTD, "
1358 "but this decoder is configured to refuse ZSTD-compressed input.");
1359 }
1360 1 dec->flags |= SRL_F_DECODER_DECOMPRESS_ZSTD;
1361 }
1362 else
1363 {
1364 0 SRL_RDR_ERRORf1(dec->pbuf, "Sereal document encoded in an unknown format '%d'",
1365 dec->encoding_flags >> SRL_PROTOCOL_VERSION_BITS);
1366 }
1367  
1368 /* Must do this via a temporary as it modifes dec->buf.pos itself */
1369 249 header_len= srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading header");
1370  
1371 249 ▶ if (dec->packet_has_checksum) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->packet_has_checksum) { 0 249 249
1372 0 const U8 * const suffix_start = dec->buf.pos;
1373 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len, " while reading checksummed header suffix");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len, " while reading checksummed header suffix"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len, " while reading checksummed header suffix"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len, " while reading checksummed header suffix"); 0 0 0
1374 0 dec->buf.pos += header_len;
1375 0 srl_process_checksum_envelope(aTHX_ dec);
1376 0 dec->buf.pos = suffix_start;
1377 }
1378  
1379 261 ▶ if (dec->proto_version > 1 && header_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (dec->proto_version > 1 && header_len) { 249 0 249
if (dec->proto_version > 1 && header_len) { 12 237 249
1380 /* We have a protocol V2+ extensible header:
1381 * - 8bit bitfield
1382 * - if lowest bit set, we have custom-header-user-data after the bitfield
1383 * => Only read header user data if an SV* was passed in to fill. */
1384  
1385 U8 bitfield;
1386  
1387 12 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading header flags");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading header flags"); 0 12 12
1388  
1389 12 bitfield = *(dec->buf.pos++);
1390 12 ▶ if (bitfield & SRL_PROTOCOL_HDR_USER_DATA && header_user_data != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (bitfield & SRL_PROTOCOL_HDR_USER_DATA && header_user_data != NULL) { 12 0 12
if (bitfield & SRL_PROTOCOL_HDR_USER_DATA && header_user_data != NULL) { 9 3 12
1391 9 const U8 * const packet_end = dec->buf.end;
1392 9 const U8 * const header_end = dec->buf.pos + (header_len - 1);
1393  
1394 /* Do an actual document body deserialization for the user data: */
1395 /* Keep this read within the declared header suffix. Otherwise a
1396 * truncated metadata value can consume bytes from the main body. */
1397 9 dec->buf.end = header_end;
1398 9 ▶ SRL_RDR_UPDATE_BODY_POS(dec->pbuf, dec->proto_version);
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_UPDATE_BODY_POS(dec->pbuf, dec->proto_version); 0 9 9
1399 9 dec->min_backref_offset = (UV)(dec->buf.pos - dec->buf.body_pos);
1400 9 srl_read_single_value(aTHX_ dec, header_user_data, NULL);
1401 9 ▶ if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE))) { 0 9 9
1402 0 srl_finalize_structure(aTHX_ dec);
1403 }
1404 9 srl_clear_decoder_body_state(aTHX_ dec); /* clean up for the main body decode */
1405 9 dec->buf.end = packet_end;
1406 9 dec->buf.pos = header_end;
1407 }
1408 else {
1409 /* Either off in bitfield or no user data wanted, skip to end of header */
1410 3 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len - 1, " while reading header packet");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len - 1, " while reading header packet"); 3 0 3
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len - 1, " while reading header packet"); 0 3 3
SRL_RDR_ASSERT_SPACE(dec->pbuf, header_len - 1, " while reading header packet"); 0 3 3
1411 3 dec->buf.pos += header_len - 1; /* header_len includes bitfield */
1412 }
1413 }
1414 else {
1415 /* Skip header since we don't have any defined header-content in this
1416 * protocol version. */
1417 237 dec->buf.pos += header_len;
1418 }
1419  
1420 249 ▶ if (dec->packet_has_checksum)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->packet_has_checksum) 0 249 249
1421 0 dec->buf.pos = (srl_reader_char_ptr)dec->checksummed_payload;
1422 }
1423 249 }
1424  
1425 #define AV_PUSH(into_av, value_sv) STMT_START { \
1426 av_push(into_av, value_sv); \
1427 SvREFCNT_inc(value_sv); \
1428 } STMT_END
1429  
1430 #define SAFE_NEW_AV(the_av) STMT_START { \
1431 the_av= newAV(); \
1432 if (!the_av) \
1433 croak("out of memory at %s line %d.", __FILE__, __LINE__); \
1434 } STMT_END
1435  
1436 #define SAFE_PTABLE_NEW(the_ptr_table) STMT_START { \
1437 the_ptr_table= PTABLE_new(); \
1438 if (!the_ptr_table) \
1439 croak("out of memory at %s line %d.", __FILE__, __LINE__); \
1440 } STMT_END
1441  
1442  
1443  
1444 /* Fetch or register a reference to an already seen frozen object.
1445 * Called during deserialization (push=1) to handle duplicate references
1446 * to the same frozen object. Called during finalization (push=) to get
1447 * the data required to THAW the item, in this case the 'push' argument
1448 * is false.
1449 *
1450 * Returns as an SV * either an HV or an AV (NULL is theoretically
1451 * possibly also but should not occur in the current use). If an HV
1452 * then it is the class stash for the item looked up, if an AV then it
1453 * is a mortal array that contains a class stash as the first element,
1454 * along with the additional items that need to be fixed to point at the
1455 * unfrozen item. */
1456 SRL_STATIC_INLINE SV *
1457 0 srl_fetch_register_frozen_object(pTHX_ srl_decoder_t *dec, SV *item, const int push)
1458 {
1459 0 ▶ if (dec->ref_thawhash) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_thawhash) { 0 0 0
1460 0 PTABLE_ENTRY_t *tblent = PTABLE_find(dec->ref_thawhash, SvRV(item));
1461 0 ▶ if (tblent) {
Branch: 1 branch
BranchTrueFalseTotal
if (tblent) { 0 0 0
1462 0 AV *info_av = (AV*)tblent->value;
1463 0 ▶ if (push) {
Branch: 1 branch
BranchTrueFalseTotal
if (push) { 0 0 0
1464 0 ▶ if (SvTYPE((SV*)info_av) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE((SV*)info_av) != SVt_PVAV) { 0 0 0
1465 /* we have an HV* class_stash in the structure right now.
1466 * so we have to upgrade this case to be an AV containing the
1467 * class stash. */
1468 0 HV *class_stash= (HV*)info_av;
1469 0 ▶ SAFE_NEW_AV(info_av);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_NEW_AV(info_av); 0 0 0
1470 0 sv_2mortal((SV*)info_av);
1471 0 AV_PUSH(info_av, (SV*)class_stash); /* push the old value into the array */
1472 0 tblent->value= (void *)info_av; /* point the tblent at this new value */
1473 }
1474 /* add the item as a duplicate reference for fixups later */
1475 0 AV_PUSH(info_av, item);
1476 }
1477 0 return (SV*)info_av;
1478 }
1479 }
1480 0 return NULL;
1481 }
1482  
1483 /* Check one class against either allow-list. */
1484 SRL_STATIC_INLINE int
1485 0 srl_class_allowed(pTHX_ HV *allow_hash, SV *allow_cb, HV *class_stash)
1486 {
1487 const char *classname;
1488 STRLEN classname_len;
1489 int is_utf8;
1490  
1491 0 ▶ if (!allow_hash && !allow_cb)
Branch: 2 branches
BranchTrueFalseTotal
if (!allow_hash && !allow_cb) 0 0 0
if (!allow_hash && !allow_cb) 0 0 0
1492 0 return 1; /* no allow-list configured: allow everything */
1493  
1494 0 ▶ classname = HvNAME_get(class_stash);
Branch: 6 branches
BranchTrueFalseTotal
classname = HvNAME_get(class_stash); 0 0 0
classname = HvNAME_get(class_stash); 0 0 0
classname = HvNAME_get(class_stash); 0 0 0
classname = HvNAME_get(class_stash); 0 0 0
classname = HvNAME_get(class_stash); 0 0 0
classname = HvNAME_get(class_stash); 0 0 0
1495 0 ▶ classname_len = HvNAMELEN_get(class_stash);
Branch: 6 branches
BranchTrueFalseTotal
classname_len = HvNAMELEN_get(class_stash); 0 0 0
classname_len = HvNAMELEN_get(class_stash); 0 0 0
classname_len = HvNAMELEN_get(class_stash); 0 0 0
classname_len = HvNAMELEN_get(class_stash); 0 0 0
classname_len = HvNAMELEN_get(class_stash); 0 0 0
classname_len = HvNAMELEN_get(class_stash); 0 0 0
1496 #if PERL_VERSION >= 16
1497 0 ▶ is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0;
Branch: 8 branches
BranchTrueFalseTotal
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(class_stash) ? 1 : 0; 0 0 0
1498 #else
1499 is_utf8 = 0;
1500 #endif
1501  
1502 0 ▶ if (allow_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_hash) { 0 0 0
1503 0 ▶ return hv_exists(allow_hash, classname,
Branch: 1 branch
BranchTrueFalseTotal
return hv_exists(allow_hash, classname, 0 0 0
1504 0 is_utf8 ? -(I32)classname_len : (I32)classname_len) ? 1 : 0;
1505 }
1506 else {
1507 /* code ref predicate: $cb->($classname) */
1508 0 int allowed = 0;
1509 int count;
1510 SV *name_sv;
1511 0 dSP;
1512  
1513 0 ENTER;
1514 0 SAVETMPS;
1515 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1516 0 name_sv = sv_2mortal(newSVpvn(classname, classname_len));
1517 0 ▶ if (is_utf8) SvUTF8_on(name_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8) SvUTF8_on(name_sv); 0 0 0
1518 0 ▶ XPUSHs(name_sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(name_sv); 0 0 0
1519 0 PUTBACK;
1520 0 count = call_sv(allow_cb, G_SCALAR);
1521 0 SPAGAIN;
1522 0 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 0 0 0
1523 0 SV *res = POPs;
1524 0 allowed = SvTRUE(res) ? 1 : 0;
1525 }
1526 0 PUTBACK;
1527 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1528 0 LEAVE;
1529 0 return allowed;
1530 }
1531 }
1532  
1533 /* Count object tags, not references to objects. class_entry may still be a
1534 * class-name scalar because ordinary blessing is deferred until finalize. */
1535 SRL_STATIC_INLINE void
1536 4 srl_track_class(pTHX_ srl_decoder_t *dec, SV *class_entry)
1537 {
1538 const char *name;
1539 STRLEN len;
1540 int is_utf8;
1541 SV **count;
1542 4 ▶ if (!dec->track_classes)
Branch: 1 branch
BranchTrueFalseTotal
if (!dec->track_classes) 4 0 4
1543 4 return;
1544 0 ▶ if (SvTYPE(class_entry) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(class_entry) == SVt_PVHV) { 0 0 0
1545 0 HV *stash = (HV *)class_entry;
1546 0 ▶ name = HvNAME_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
name = HvNAME_get(stash); 0 0 0
1547 0 ▶ len = HvNAMELEN_get(stash);
Branch: 6 branches
BranchTrueFalseTotal
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
len = HvNAMELEN_get(stash); 0 0 0
1548 #if PERL_VERSION >= 16
1549 0 ▶ is_utf8 = HvNAMEUTF8(stash) ? 1 : 0;
Branch: 8 branches
BranchTrueFalseTotal
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
is_utf8 = HvNAMEUTF8(stash) ? 1 : 0; 0 0 0
1550 #else
1551 is_utf8 = 0;
1552 #endif
1553 } else {
1554 0 name = SvPV(class_entry, len);
1555 0 is_utf8 = SvUTF8(class_entry) ? 1 : 0;
1556 }
1557 0 ▶ count = hv_fetch(dec->track_classes, name,
Branch: 1 branch
BranchTrueFalseTotal
count = hv_fetch(dec->track_classes, name, 0 0 0
1558 is_utf8 ? -(I32)len : (I32)len, 1);
1559 0 ▶ if (!count)
Branch: 1 branch
BranchTrueFalseTotal
if (!count) 0 0 0
1560 0 croak("Could not update track_classes");
1561 0 sv_inc(*count);
1562 }
1563  
1564 /* Replace the complete value: THAW may return a scalar as well as a reference.
1565 * sv_setsv also releases old strong ownership or unregisters a weak backref. */
1566 SRL_STATIC_INLINE void
1567 0 srl_replace_thawed_object(pTHX_ SV *into, SV *replacement)
1568 {
1569 0 const int readonly = SvREADONLY(into);
1570 0 const int weak = SvWEAKREF(into);
1571 0 SvREADONLY_off(into);
1572 /* A mortal return value must survive every fixup; normal sv_setsv can
1573 * steal a temporary's contents on the first assignment. */
1574 0 sv_setsv_flags(into, replacement, SV_GMAGIC|SV_NOSTEAL);
1575 0 ▶ if (weak && SvROK(into))
Branch: 2 branches
BranchTrueFalseTotal
if (weak && SvROK(into)) 0 0 0
if (weak && SvROK(into)) 0 0 0
1576 0 sv_rvweaken(into);
1577 0 ▶ if (readonly)
Branch: 1 branch
BranchTrueFalseTotal
if (readonly) 0 0 0
1578 0 SvREADONLY_on(into);
1579 0 }
1580  
1581 SRL_STATIC_INLINE void
1582 2 srl_finalize_structure(pTHX_ srl_decoder_t *dec)
1583 {
1584 2 int nobless = SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NO_BLESS_OBJECTS);
1585 2 int nothaw = SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NO_THAW_OBJECTS);
1586  
1587 /* At this point the structure should be more or less reconstructed.
1588 * The main exception is objects that need to be blessed. We defer
1589 * this to as late as possible to avoid triggering DESTROY methods
1590 * as part of our deserialization. The blessed objects can be
1591 * divided into those which are frozen and thus require a call to
1592 * THAW, and those which only required a call to bless.
1593 *
1594 * We handle the simple case first, as the frozen form of an object
1595 * of one class might contain a simple object. In this case the
1596 * order we call bless doesn't matter.
1597 *
1598 * Once all the simple bless operations have been performed we thaw
1599 * the frozen items in LIFO order, replacing the referent of the
1600 * reference to the frozen form with the thawed replacement. We may
1601 * have to do this replacement operation multiple times if there are
1602 * multiple references to the same frozen object in the data
1603 * structure as the thaw operation occurs so late we cant use the
1604 * normal cache mechanism to handle multiple references.
1605 */
1606  
1607 2 ▶ if (dec->ref_stashes) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->ref_stashes) { 2 0 2
1608 /* The iterator could be leaked on exceptions if not for PTABLE_FLAG_AUTOCLEAN. */
1609 2 PTABLE_ITER_t *it = PTABLE_iter_new_flags(dec->ref_stashes, PTABLE_FLAG_AUTOCLEAN);
1610 PTABLE_ENTRY_t *ent;
1611  
1612 /* On modern Perl, check every class before blessing any ordinary
1613 * object. The 5.8 workaround checked when it blessed during parsing. */
1614 #if !USE_588_WORKAROUND
1615 2 ▶ if (!nobless && (dec->allow_hash || dec->allow_cb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!nobless && (dec->allow_hash || dec->allow_cb)) { 2 0 2
if (!nobless && (dec->allow_hash || dec->allow_cb)) { 2 0 2
if (!nobless && (dec->allow_hash || dec->allow_cb)) { 0 2 2
1616 0 ▶ while (NULL != (ent = PTABLE_iter_next(it))) {
Branch: 1 branch
BranchTrueFalseTotal
while (NULL != (ent = PTABLE_iter_next(it))) { 0 0 0
1617 0 SV *class_entry = (SV *)ent->value;
1618 HV *stash;
1619 0 ▶ if (expect_false(!class_entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!class_entry)) { 0 0 0
1620 0 PTABLE_iter_free(it);
1621 0 SRL_RDR_ERROR(dec->pbuf, "missing class entry!");
1622 }
1623 0 stash = SvTYPE(class_entry) == SVt_PVHV
1624 0 ▶ ? (HV *)class_entry : gv_stashsv(class_entry, GV_ADD);
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)class_entry : gv_stashsv(class_entry, GV_ADD); 0 0 0
1625 0 ▶ if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, stash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, stash)) { 0 0 0
1626 0 PTABLE_iter_free(it);
1627 0 ▶ SRL_RDR_ERRORf1(dec->pbuf,
Branch: 6 branches
BranchTrueFalseTotal
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
1628 "Object class '%s' is not in allow_classes", HvNAME(stash));
1629 }
1630 }
1631 0 PTABLE_iter_free(it);
1632 0 it = PTABLE_iter_new_flags(dec->ref_stashes, PTABLE_FLAG_AUTOCLEAN);
1633 }
1634 #endif
1635  
1636 /* We have gotten here without error, so bless all the objects.
1637 * We defer to the end like this so that we only bless data structures
1638 * if the entire deserialization completes. */
1639 6 ▶ while ( NULL != (ent = PTABLE_iter_next(it)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( NULL != (ent = PTABLE_iter_next(it)) ) { 4 2 6
1640 4 SV *class_entry = (SV *)ent->value; /* see srl_class_stash() */
1641 4 HV *stash = NULL;
1642 4 AV *ref_bless_av = (AV *) PTABLE_fetch(dec->ref_bless_av, ent->key);
1643 SSize_t len;
1644 4 ▶ if (expect_false( !class_entry || !ref_bless_av )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( !class_entry || !ref_bless_av )) { 4 0 4
if (expect_false( !class_entry || !ref_bless_av )) { 0 4 4
if (expect_false( !class_entry || !ref_bless_av )) { 0 4 4
1645 0 PTABLE_iter_free(it);
1646 0 SRL_RDR_ERROR(dec->pbuf, "missing stash or ref_bless_av!");
1647 }
1648 8 ▶ for( len= av_len(ref_bless_av) ; len >= 0 ; len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
for( len= av_len(ref_bless_av) ; len >= 0 ; len-- ) { 4 4 8
1649 4 SV* obj= av_pop(ref_bless_av); /*note that av_pop does NOT refcnt dec the sv*/
1650 4 ▶ if (expect_false(!obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!obj)) { 0 4 4
1651 0 PTABLE_iter_free(it);
1652 0 SRL_RDR_ERROR(dec->pbuf, "object missing from ref_bless_av array?");
1653 }
1654 /* Keep weak targets alive until blessing is finished. A weak
1655 * target with refcount one is owned only by weakref_av and will
1656 * disappear when that keepalive is released. Do not bless it
1657 * (and thereby run DESTROY), or inspect an expired reference. */
1658 4 ▶ if (!SvROK(obj)
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj) 4 0 4
1659 4 ▶ || (SvWEAKREF(obj) && SvREFCNT(SvRV(obj)) == 1)) {
Branch: 2 branches
BranchTrueFalseTotal
|| (SvWEAKREF(obj) && SvREFCNT(SvRV(obj)) == 1)) { 0 4 4
|| (SvWEAKREF(obj) && SvREFCNT(SvRV(obj)) == 1)) { 0 0 0
1660 0 SvREFCNT_dec(obj);
1661 0 continue;
1662 }
1663 4 ▶ if (SvREFCNT(obj)>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvREFCNT(obj)>1) { 4 0 4
1664 /* It is possible that someone handcrafts a hash with a key collision,
1665 * which could trick us into effectively blessing an object and then
1666 * calling DESTROY on it. So we track the refcount of the objects
1667 * popped off the ref_bless_av, and only bless if their refcount *before*
1668 * we refcount dec is higher than 1. If it is 1 then we just destroy the
1669 * object.
1670 * */
1671 #if USE_588_WORKAROUND
1672 /* was blessed early, don't rebless */
1673 #else
1674 4 ▶ if (!nobless) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nobless) { 4 0 4
1675 4 ▶ if (!stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) { 4 0 4
1676 /* updating the current entry's value is safe while iterating */
1677 4 ▶ if (SvTYPE(class_entry) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(class_entry) != SVt_PVHV) 2 2 4
1678 2 ent->value = (void *)gv_stashsv(class_entry, GV_ADD);
1679 4 stash = (HV *)ent->value;
1680 }
1681 4 ▶ if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(SvRV(obj))) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(SvRV(obj))) { 0 4 4
if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(SvRV(obj))) { 0 0 0
1682 /* the referenced scalar was readonly, temporary
1683 set it rw to bless its reference */
1684 0 SvREADONLY_off(SvRV(obj));
1685 0 sv_bless(obj, stash);
1686 0 SvREADONLY_on(SvRV(obj));
1687 } else {
1688 4 sv_bless(obj, stash);
1689 }
1690 }
1691 #endif
1692 } else {
1693 0 warn("Not blessing object that would be DESTROYed immediately. Malformed packet?");
1694 }
1695 4 SvREFCNT_dec(obj);
1696 }
1697 }
1698 2 PTABLE_iter_free(it);
1699 }
1700 /* Release temporary ownership only after surviving ordinary objects have
1701 * been blessed and their readonly flags restored. Weak-only targets were
1702 * deliberately left unblessed, so their release cannot invoke DESTROY. */
1703 2 ▶ if (dec->weakref_av)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->weakref_av) 0 2 2
1704 0 av_clear(dec->weakref_av);
1705  
1706 /* And finally after all the simple blessing is done we need to handle any FROZEN
1707 * objects by calling their THAW method. We have to do this last as their frozen
1708 * representation may contain simple objects created during the bless phase above.
1709 */
1710 2 ▶ if (dec->thaw_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->thaw_av) { 0 2 2
1711 0 SSize_t thaw_av_len= av_len(dec->thaw_av);
1712 0 HV *debug_class= NULL;
1713  
1714 /* Everything should be blessed now, so call thaw on each FROZEN
1715 * object in LIFO order. Each frozen object will have at least one
1716 * reference to it that must be fixed, there may be more if the same
1717 * object was referenced multiple times in the data structure.
1718 * For each item in thaw_av there should be a corresponding av with
1719 * the class stash and any additional items that need to be fixed
1720 * in it.
1721 */
1722 0 ▶ for ( ; thaw_av_len >= 0 ; thaw_av_len-- ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; thaw_av_len >= 0 ; thaw_av_len-- ) { 0 0 0
1723 /* Once popped, this ownership must also be released if a THAW
1724 * method or allow-list callback croaks before the loop ends. */
1725 0 SV *sv = sv_2mortal(av_pop(dec->thaw_av));
1726 HV *class_stash;
1727 0 AV *additional_refs= NULL;
1728 0 SSize_t fixups = 0;
1729 0 int thaw_this = 1; /* cleared if class is denied but fallback allowed */
1730 /* Releasing weakref_av can expire a weak-only frozen argument
1731 * array too. There is no object left to thaw in that case. */
1732 0 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 0 0 0
1733 0 continue;
1734 }
1735 0 class_stash = (HV*)srl_fetch_register_frozen_object(aTHX_ dec, sv, 0);
1736 0 ▶ if (expect_false(!class_stash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!class_stash)) { 0 0 0
1737 0 SRL_RDR_ERROR(dec->pbuf, "Missing class for frozen object");
1738 }
1739 0 ▶ if (SvTYPE(class_stash) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(class_stash) == SVt_PVAV) { 0 0 0
1740 0 additional_refs = (AV*)class_stash;
1741 0 fixups = av_len(additional_refs); /* no +1 because we do it before the class_stash shift */
1742 0 class_stash = (HV *)av_shift((AV*)additional_refs);
1743 0 SvREFCNT_dec(class_stash);
1744 }
1745 /* Enforce the thaw allow-list (if any). A disallowed class either
1746 * croaks (default) or falls back to the raw-args behavior below. */
1747 0 ▶ if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, class_stash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, class_stash)) { 0 0 0
1748 0 ▶ SRL_RDR_ERRORf1(dec->pbuf,
Branch: 6 branches
BranchTrueFalseTotal
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
1749 "Object class '%s' is not in allow_classes", HvNAME(class_stash));
1750 }
1751 0 ▶ if (!nothaw && !srl_class_allowed(aTHX_ dec->thaw_allow_hash, dec->thaw_allow_cb, class_stash)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!nothaw && !srl_class_allowed(aTHX_ dec->thaw_allow_hash, dec->thaw_allow_cb, class_stash)) { 0 0 0
if (!nothaw && !srl_class_allowed(aTHX_ dec->thaw_allow_hash, dec->thaw_allow_cb, class_stash)) { 0 0 0
1752 0 ▶ if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_THAW_DENY_RAW)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_THAW_DENY_RAW)) { 0 0 0
1753 0 thaw_this = 0;
1754 } else {
1755 0 ▶ SRL_RDR_ERRORf1(dec->pbuf,
Branch: 6 branches
BranchTrueFalseTotal
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
SRL_RDR_ERRORf1(dec->pbuf, 0 0 0
1756 "THAW not allowed for class '%s' (not in thaw_allow_classes)",
1757 HvNAME(class_stash));
1758 }
1759 }
1760 0 ▶ if (nothaw || !thaw_this) {
Branch: 2 branches
BranchTrueFalseTotal
if (nothaw || !thaw_this) { 0 0 0
if (nothaw || !thaw_this) { 0 0 0
1761 /* do not actually thaw. In order to make it easier to find these cases in a dump
1762 * we push the class name into the AV, and then bless it into a special class
1763 * private to the Sereal project. */
1764 0 ▶ char *classname = HvNAME(class_stash);
Branch: 6 branches
BranchTrueFalseTotal
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
1765 0 AV *av= (AV*)SvRV(sv);
1766 0 const int readonly = SvREADONLY((SV*)av);
1767 0 SV *name_sv = newSVpvn(classname, strlen(classname));
1768 0 ▶ if (!debug_class) {
Branch: 1 branch
BranchTrueFalseTotal
if (!debug_class) { 0 0 0
1769 0 debug_class = gv_stashpvs("Sereal::Decoder::THAW_args",1);
1770 }
1771 0 ▶ if (dec->flags_readonly)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->flags_readonly) 0 0 0
1772 0 SvREADONLY_on(name_sv);
1773 0 SvREADONLY_off((SV*)av);
1774 0 av_push(av, name_sv);
1775 0 sv_bless(sv,debug_class);
1776 0 ▶ if (readonly)
Branch: 1 branch
BranchTrueFalseTotal
if (readonly) 0 0 0
1777 0 SvREADONLY_on((SV*)av);
1778 } else {
1779 /* Keep the returned value alive until all weak and strong
1780 * references have been replaced. */
1781 0 SV *replacement = srl_thaw_object(aTHX_ dec, class_stash, sv);
1782 0 srl_replace_thawed_object(aTHX_ sv, replacement);
1783 /* if there are any additional refs then stitch them in */
1784 0 ▶ for ( ; fixups > 0; fixups--) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; fixups > 0; fixups--) { 0 0 0
1785 0 SV* into = sv_2mortal(av_pop(additional_refs));
1786 0 srl_replace_thawed_object(aTHX_ into, replacement);
1787 }
1788 }
1789 }
1790 }
1791 2 }
1792  
1793  
1794 /* PRIVATE UTILITY FUNCTIONS */
1795  
1796 SRL_STATIC_INLINE void
1797 0 srl_track_sv(pTHX_ srl_decoder_t *dec, const U8 *track_pos, SV *sv)
1798 {
1799 /* COPY creates a new value at its own offset. Re-reading its source
1800 * must neither replace an existing alias target nor register a key as
1801 * a value. The COPY tag's own tracking is handled before save_pos is set. */
1802 0 ▶ if (dec->save_pos)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->save_pos) 0 0 0
1803 0 return;
1804  
1805 /* A run of PADs can have several tracked offsets for the same value.
1806 * Update all of them when aliasing replaces the destination scalar. */
1807 do {
1808 0 ▶ if (*track_pos & SRL_HDR_TRACK_FLAG)
Branch: 1 branch
BranchTrueFalseTotal
if (*track_pos & SRL_HDR_TRACK_FLAG) 0 0 0
1809 0 PTABLE_store(dec->ref_seenhash, (void *)(track_pos - dec->buf.body_pos), (void *)sv);
1810 0 ▶ if ((*track_pos & ~SRL_HDR_TRACK_FLAG) != SRL_HDR_PAD)
Branch: 1 branch
BranchTrueFalseTotal
if ((*track_pos & ~SRL_HDR_TRACK_FLAG) != SRL_HDR_PAD) 0 0 0
1811 0 break;
1812 0 ++track_pos;
1813 0 ▶ } while (track_pos < dec->buf.pos);
Branch: 1 branch
BranchTrueFalseTotal
} while (track_pos < dec->buf.pos); 0 0 0
1814 }
1815  
1816  
1817 SRL_STATIC_INLINE SV *
1818 0 srl_fetch_item(pTHX_ srl_decoder_t *dec, UV item, const char * const tag_name)
1819 {
1820 0 SV *sv= (SV *)PTABLE_fetch(dec->ref_seenhash, (void *)item);
1821 #ifndef FOLLOW_REFERENCES_IF_NOT_STASHED
1822 0 ▶ if (expect_false( !sv )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !sv )) { 0 0 0
1823 /*srl_ptable_debug_dump(aTHX_ dec->ref_seenhash);*/
1824 0 SRL_RDR_ERRORf2(dec->pbuf, "%s(%"UVuf") references an unknown item", tag_name, item);
1825 }
1826 #endif
1827 0 return sv;
1828 }
1829  
1830 /****************************************************************************
1831 * PRIVATE WORKER SUBS FOR DEPARSING *
1832 ****************************************************************************/
1833 static SRL_NOINLINE void
1834 0 srl_alias_iv(pTHX_ srl_decoder_t *dec, SV **container, const U8 *track_it, IV iv)
1835 {
1836 SV *alias;
1837 0 SV **av_array= AvARRAY(dec->alias_cache);
1838 0 U32 ofs = iv + 16; /* we always cover from -16 up so we add 16 */
1839  
1840 assert( IS_IV_ALIAS(dec,iv) );
1841  
1842 0 ▶ if (!av_array[ofs] || av_array[ofs] == &PL_sv_undef) {
Branch: 2 branches
BranchTrueFalseTotal
if (!av_array[ofs] || av_array[ofs] == &PL_sv_undef) { 0 0 0
if (!av_array[ofs] || av_array[ofs] == &PL_sv_undef) { 0 0 0
1843 0 alias= newSViv(iv);
1844 /* mark it as readonly so people dont try to modify it */
1845 0 SvREADONLY_on(alias);
1846 /* store it in the alias_cache array */
1847 0 av_array[ofs]= alias;
1848 } else {
1849 0 alias= av_array[ofs];
1850 }
1851  
1852 0 SvREFCNT_inc(alias);
1853  
1854 0 ▶ if (*container && *container != &PL_sv_undef)
Branch: 2 branches
BranchTrueFalseTotal
if (*container && *container != &PL_sv_undef) 0 0 0
if (*container && *container != &PL_sv_undef) 0 0 0
1855 0 SvREFCNT_dec(*container);
1856 0 *container= alias;
1857 0 ▶ if (track_it)
Branch: 1 branch
BranchTrueFalseTotal
if (track_it) 0 0 0
1858 0 srl_track_sv(aTHX_ dec, track_it, alias);
1859 0 }
1860  
1861  
1862  
1863 SRL_STATIC_INLINE void
1864 14 srl_setiv(pTHX_ srl_decoder_t *dec, SV *into, SV **container, const U8 *track_it, IV iv)
1865 {
1866 14 ▶ if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) {
Branch: 5 branches
BranchTrueFalseTotal
if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) { 7 7 14
if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) { 0 7 7
if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) { 0 0 0
if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) { 0 0 0
if ( expect_false( container && IS_IV_ALIAS(dec,iv) )) { 0 14 14
1867 0 srl_alias_iv(aTHX_ dec, container, track_it, iv);
1868 } else {
1869 /* unroll sv_setiv() for the SVt_NULL case, which we will
1870 * see regularly - this wins about 35% speedup for us
1871 * but involve gratuitious intimacy with the internals.
1872 * */
1873 #ifdef FAST_IV
1874 14 ▶ if ( SvTYPE(into) == SVt_NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvTYPE(into) == SVt_NULL ) { 8 6 14
1875 /* XXX: dont need to do this, we are null already */
1876 /* SvFLAGS(into) &= ~SVTYPEMASK; */
1877 assert(
1878 (SVt_NULL == 0) &&
1879 ((SvFLAGS(into) & (SVTYPEMASK|SVf_OOK|SVf_OK|SVf_IVisUV|SVf_UTF8)) == 0)
1880 );
1881 8 SvANY(into) = (XPVIV*)((char*)&(into->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1882 /* replace this: */
1883 /* SvIOK_only(into); */
1884 /* with this: */
1885 8 SvFLAGS(into) |= (SVt_IV | SVf_IOK | SVp_IOK);
1886 8 SvIV_set(into, iv);
1887 } else
1888 #endif
1889 {
1890 6 sv_setiv(into, iv);
1891 }
1892 }
1893 14 }
1894  
1895 SRL_STATIC_INLINE void
1896 0 srl_read_varint_into(pTHX_ srl_decoder_t *dec, SV* into, SV **container, const U8 *track_it)
1897 {
1898 0 UV uv= srl_read_varint_uv(aTHX_ dec->pbuf);
1899 0 ▶ if (expect_true(uv <= (UV)IV_MAX)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true(uv <= (UV)IV_MAX)) { 0 0 0
1900 0 srl_setiv(aTHX_ dec, into, container, track_it, (IV)uv);
1901 } else {
1902 /* grr, this is ridiculous! */
1903 0 sv_setiv(into, 0);
1904 0 SvIsUV_on(into);
1905 0 SvUV_set(into, uv);
1906 }
1907 0 }
1908  
1909  
1910 SRL_STATIC_INLINE IV
1911 0 srl_read_zigzag_iv(pTHX_ srl_decoder_t *dec)
1912 {
1913 0 UV n= srl_read_varint_uv(aTHX_ dec->pbuf);
1914 0 IV i= (n >> 1) ^ (-(n & 1));
1915 0 return i;
1916 }
1917  
1918 SRL_STATIC_INLINE void
1919 0 srl_read_zigzag_into(pTHX_ srl_decoder_t *dec, SV* into, SV **container, const U8 *track_it)
1920 {
1921 0 srl_setiv(aTHX_ dec, into, container, track_it, srl_read_zigzag_iv(aTHX_ dec));
1922 0 }
1923  
1924 SRL_STATIC_INLINE IV
1925 0 srl_read_neg_varint_iv(pTHX_ srl_decoder_t *dec)
1926 {
1927 0 UV magnitude = srl_read_varint_uv(aTHX_ dec->pbuf);
1928 0 const UV iv_min_magnitude = (UV)IV_MAX + 1;
1929 0 ▶ if (expect_false(magnitude == 0 || magnitude > iv_min_magnitude)) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(magnitude == 0 || magnitude > iv_min_magnitude)) { 0 0 0
if (expect_false(magnitude == 0 || magnitude > iv_min_magnitude)) { 0 0 0
if (expect_false(magnitude == 0 || magnitude > iv_min_magnitude)) { 0 0 0
1930 0 SRL_RDR_ERRORf1(dec->pbuf, "Invalid NEG_VARINT magnitude %"UVuf, magnitude);
1931 }
1932 0 ▶ if (expect_false(magnitude == iv_min_magnitude))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(magnitude == iv_min_magnitude)) 0 0 0
1933 0 return IV_MIN;
1934 0 return -(IV)magnitude;
1935 }
1936  
1937 SRL_STATIC_INLINE void
1938 0 srl_read_neg_varint_into(pTHX_ srl_decoder_t *dec, SV* into, SV **container, const U8 *track_it)
1939 {
1940 0 srl_setiv(aTHX_ dec, into, container, track_it, srl_read_neg_varint_iv(aTHX_ dec));
1941 0 }
1942  
1943  
1944 SRL_STATIC_INLINE void
1945 2 srl_read_string(pTHX_ srl_decoder_t *dec, int is_utf8, SV* into)
1946 {
1947 2 UV len= srl_read_varint_uv(aTHX_ dec->pbuf);
1948 /* Limit the maximum size of the string that we accept to whatever was configured */
1949 2 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 2 2
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 2 2
1950 2 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading string"); 2 0 2
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading string"); 0 2 2
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading string"); 0 2 2
1951 2 ▶ if (expect_false(is_utf8 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8))) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(is_utf8 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8))) { 0 2 2
if (expect_false(is_utf8 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8))) { 0 0 0
if (expect_false(is_utf8 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_VALIDATE_UTF8))) { 0 2 2
1952 /* checks for invalid byte sequences. */
1953 0 ▶ if (expect_false( len > 0 && !is_utf8_string((U8*)dec->buf.pos, len) )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( len > 0 && !is_utf8_string((U8*)dec->buf.pos, len) )) { 0 0 0
if (expect_false( len > 0 && !is_utf8_string((U8*)dec->buf.pos, len) )) { 0 0 0
if (expect_false( len > 0 && !is_utf8_string((U8*)dec->buf.pos, len) )) { 0 0 0
1954 0 SRL_RDR_ERROR(dec->pbuf, "Invalid UTF8 byte sequence");
1955 }
1956 }
1957 2 sv_setpvn(into,(char *)dec->buf.pos,len);
1958 2 ▶ if (is_utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_utf8) { 0 2 2
1959 0 SvUTF8_on(into);
1960 } else {
1961 2 SvUTF8_off(into);
1962 }
1963 2 dec->buf.pos+= len;
1964 2 }
1965  
1966 static SRL_NOINLINE void
1967 0 srl_read_vstring(pTHX_ srl_decoder_t *dec, SV* into, int is_utf8)
1968 {
1969 0 UV len = srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading extended VSTRING");
1970 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, len, "v-string spelling");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "v-string spelling"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "v-string spelling"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "v-string spelling"); 0 0 0
1971 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading extended VSTRING payload");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading extended VSTRING payload"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading extended VSTRING payload"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading extended VSTRING payload"); 0 0 0
1972 0 ▶ if (expect_false(len == 0 || len >= I32_MAX))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(len == 0 || len >= I32_MAX)) 0 0 0
if (expect_false(len == 0 || len >= I32_MAX)) 0 0 0
if (expect_false(len == 0 || len >= I32_MAX)) 0 0 0
1973 0 SRL_RDR_ERROR(dec->pbuf, "Invalid extended VSTRING spelling length");
1974 0 (void)sv_grow(into, len + 1);
1975 {
1976 /* scan_vstring() can inspect the byte at its end pointer. Isolate
1977 * the spelling from subsequent tags and supply a terminating NUL. */
1978 0 SV * const spelling_sv = sv_2mortal(newSVpvn((const char *)dec->buf.pos, len));
1979 0 const char *spelling = SvPVX(spelling_sv);
1980 0 const char *spelling_end = spelling + len;
1981 0 const char *parsed = scan_vstring(spelling, spelling_end, into);
1982 0 ▶ if (expect_false(parsed != spelling_end))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(parsed != spelling_end)) 0 0 0
1983 0 SRL_RDR_ERROR(dec->pbuf, "Invalid extended VSTRING spelling");
1984 }
1985 0 ▶ if (expect_false(!!SvUTF8(into) != !!is_utf8))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!!SvUTF8(into) != !!is_utf8)) 0 0 0
1986 0 SRL_RDR_ERROR(dec->pbuf, "VSTRING extension ID does not match the decoded string encoding");
1987 0 dec->buf.pos += len;
1988 0 }
1989  
1990 /* declare a union so that we are guaranteed the right alignment
1991 * rules - this is required for e.g. ARM */
1992 union myfloat {
1993 U8 c[sizeof(long double)];
1994 float f;
1995 double d;
1996 long double ld;
1997 #ifdef HAS_QUADMATH
1998 __float128 nv;
1999 #endif
2000 };
2001  
2002 SRL_STATIC_INLINE int
2003 0 srl_native_float_payload(srl_decoder_t *dec)
2004 {
2005 0 ▶ if (dec->proto_version >= 7) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (dec->proto_version >= 7) return 0; 0 0 0
2006 0 ▶ if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_FLOAT_BYTEORDER_SET))
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_FLOAT_BYTEORDER_SET)) 0 0 0
2007 0 return SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_USE_NATIVE_FLOATS) != 0;
2008 0 return 1;
2009 }
2010  
2011 /* Legacy packets carry no byte-order marker, so an explicit source order
2012 * comes from the caller's knowledge of the producer. Normalize that order
2013 * for the portable float readers. The override cannot affect v7 portable
2014 * values because those already have a defined little-endian wire order. */
2015 SRL_STATIC_INLINE const U8 *
2016 0 srl_ordered_float_payload(srl_decoder_t *dec, U8 *scratch, STRLEN len)
2017 {
2018 STRLEN i;
2019 0 ▶ if (dec->proto_version < 7 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_BIG_ENDIAN_FLOATS)) {
Branch: 2 branches
BranchTrueFalseTotal
if (dec->proto_version < 7 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_BIG_ENDIAN_FLOATS)) { 0 0 0
if (dec->proto_version < 7 && SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_BIG_ENDIAN_FLOATS)) { 0 0 0
2020 0 ▶ for (i = 0; i < len; i++) scratch[i] = dec->buf.pos[len - 1 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) scratch[i] = dec->buf.pos[len - 1 - i]; 0 0 0
2021 0 return scratch;
2022 }
2023 0 return dec->buf.pos;
2024 }
2025  
2026 /* Wire payloads may start at any byte offset. Copy into aligned storage
2027 * even on CPUs that support unaligned loads: a misaligned typed pointer
2028 * dereference is undefined in C. Constant-size copies can be optimized
2029 * into unaligned loads where the target supports them. */
2030  
2031 SRL_STATIC_INLINE void
2032 0 srl_read_float(pTHX_ srl_decoder_t *dec, SV* into)
2033 {
2034 union myfloat val;
2035 U8 scratch[sizeof(float)];
2036 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(float), " while reading FLOAT");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(float), " while reading FLOAT"); 0 0 0
2037 0 ▶ if (srl_native_float_payload(dec))
Branch: 1 branch
BranchTrueFalseTotal
if (srl_native_float_payload(dec)) 0 0 0
2038 0 Copy(dec->buf.pos,val.c,sizeof(float),U8);
2039 else
2040 0 srl_copy_float_little_endian(aTHX_ val.c, srl_ordered_float_payload(dec, scratch, sizeof(float)));
2041 0 sv_setnv(into, (NV)val.f);
2042 0 dec->buf.pos+= sizeof(float);
2043 0 }
2044  
2045 static SRL_NOINLINE void
2046 0 srl_read_float_128(pTHX_ srl_decoder_t *dec, SV* into)
2047 {
2048 U8 bytes[16], scratch[16];
2049 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 16, " while reading FLOAT_128");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 16, " while reading FLOAT_128"); 0 0 0
2050 0 ▶ if (srl_native_float_payload(dec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (srl_native_float_payload(dec)) { 0 0 0
2051 /* Legacy quadmath used native byte order. */
2052 0 srl_copy_little_endian(bytes, dec->buf.pos, 16);
2053 } else {
2054 0 Copy(srl_ordered_float_payload(dec, scratch, 16), bytes, 16, U8);
2055 }
2056 0 sv_setnv(into, srl_binary128_to_nv(bytes));
2057 0 dec->buf.pos += 16;
2058 0 }
2059  
2060 SRL_STATIC_INLINE double
2061 0 srl_read_double_val(pTHX_ srl_decoder_t *dec)
2062 {
2063 union myfloat val;
2064 U8 scratch[sizeof(double)];
2065 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(double), " while reading DOUBLE");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(double), " while reading DOUBLE"); 0 0 0
2066 0 ▶ if (srl_native_float_payload(dec))
Branch: 1 branch
BranchTrueFalseTotal
if (srl_native_float_payload(dec)) 0 0 0
2067 0 Copy(dec->buf.pos, val.c, sizeof(double), U8);
2068 else
2069 0 srl_copy_double_little_endian(aTHX_ val.c, srl_ordered_float_payload(dec, scratch, sizeof(double)));
2070 0 dec->buf.pos += sizeof(double);
2071 0 return val.d;
2072 }
2073  
2074 SRL_STATIC_INLINE void
2075 0 srl_read_double(pTHX_ srl_decoder_t *dec, SV* into)
2076 {
2077 0 sv_setnv(into, (NV)srl_read_double_val(aTHX_ dec));
2078 0 }
2079  
2080  
2081 static SRL_NOINLINE void
2082 0 srl_read_long_double(pTHX_ srl_decoder_t *dec, SV* into)
2083 {
2084 union myfloat val;
2085 0 ▶ if (dec->proto_version >= 7 && !dec->buf.long_double_size)
Branch: 2 branches
BranchTrueFalseTotal
if (dec->proto_version >= 7 && !dec->buf.long_double_size) 0 0 0
if (dec->proto_version >= 7 && !dec->buf.long_double_size) 0 0 0
2086 0 SRL_RDR_ERROR(dec->pbuf, "LONG_DOUBLE requires a declaration");
2087 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(long double), " while reading LONG_DOUBLE");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, sizeof(long double), " while reading LONG_DOUBLE"); 0 0 0
2088 0 ▶ if (dec->proto_version >= 7) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->proto_version >= 7) { 0 0 0
2089 0 Copy(dec->buf.pos,val.c,sizeof(long double),U8);
2090 0 ▶ } else if (srl_native_float_payload(dec))
Branch: 1 branch
BranchTrueFalseTotal
} else if (srl_native_float_payload(dec)) 0 0 0
2091 0 Copy(dec->buf.pos,val.c,sizeof(long double),U8);
2092 else {
2093 0 U8 format = srl_native_long_double_format();
2094 0 ▶ STRLEN i, value_bytes = (format & 127) == SRL_LD_X87 ? 10 : sizeof(long double);
Branch: 1 branch
BranchTrueFalseTotal
STRLEN i, value_bytes = (format & 127) == SRL_LD_X87 ? 10 : sizeof(long double); 0 0 0
2095 0 int source_big = SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_BIG_ENDIAN_FLOATS) != 0;
2096 0 ▶ if (!format)
Branch: 1 branch
BranchTrueFalseTotal
if (!format) 0 0 0
2097 0 SRL_RDR_ERROR(dec->pbuf, "Cannot override unknown legacy LONG_DOUBLE layout");
2098 0 Zero(val.c, sizeof(long double), U8);
2099 0 ▶ if (source_big == ((format & SRL_LD_BIG_ENDIAN) != 0))
Branch: 1 branch
BranchTrueFalseTotal
if (source_big == ((format & SRL_LD_BIG_ENDIAN) != 0)) 0 0 0
2100 0 Copy(dec->buf.pos, val.c, value_bytes, U8);
2101 else
2102 0 ▶ for (i = 0; i < value_bytes; i++) val.c[i] = dec->buf.pos[value_bytes - 1 - i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < value_bytes; i++) val.c[i] = dec->buf.pos[value_bytes - 1 - i]; 0 0 0
2103 }
2104 0 sv_setnv(into, (NV)val.ld);
2105 0 dec->buf.pos+= sizeof(long double);
2106 0 }
2107  
2108  
2109 SRL_STATIC_INLINE void
2110 0 srl_array_set_iv(pTHX_ srl_decoder_t *dec, SV **slot, IV iv)
2111 {
2112 0 ▶ if (expect_false(IS_IV_ALIAS(dec, iv))) {
Branch: 4 branches
BranchTrueFalseTotal
if (expect_false(IS_IV_ALIAS(dec, iv))) { 0 0 0
if (expect_false(IS_IV_ALIAS(dec, iv))) { 0 0 0
if (expect_false(IS_IV_ALIAS(dec, iv))) { 0 0 0
if (expect_false(IS_IV_ALIAS(dec, iv))) { 0 0 0
2113 0 *slot = NULL;
2114 0 srl_alias_iv(aTHX_ dec, slot, NULL, iv);
2115 } else {
2116 0 SV * const sv = newSViv(iv);
2117 0 ▶ if (expect_false(dec->flags_readonly))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->flags_readonly)) 0 0 0
2118 0 SvREADONLY_on(sv);
2119 0 *slot = sv;
2120 }
2121 0 }
2122  
2123 static SRL_NOINLINE void
2124 6 srl_read_array(pTHX_ srl_decoder_t *dec, SV *into, U8 tag) {
2125 UV len;
2126 6 ▶ if (tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag) { 6 0 6
2127 6 SV *referent= (SV *)newAV();
2128 6 len= tag & 15;
2129 6 ▶ SRL_sv_set_rv_to(into, referent);
Branch: 15 branches
BranchTrueFalseTotal
SRL_sv_set_rv_to(into, referent); 6 0 6
SRL_sv_set_rv_to(into, referent); 6 0 6
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
2130 6 into= referent;
2131 } else {
2132 0 len= srl_read_varint_uv_count(aTHX_ dec->pbuf, " while reading ARRAY");
2133 0 ▶ (void)SvUPGRADE(into, SVt_PVAV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(into, SVt_PVAV); 0 0 0
2134 }
2135  
2136 /* Limit the maximum number of elements that we accept to whatever was configured */
2137 6 ▶ if (expect_false( dec->max_num_array_entries != 0 && len > dec->max_num_array_entries )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( dec->max_num_array_entries != 0 && len > dec->max_num_array_entries )) { 0 6 6
if (expect_false( dec->max_num_array_entries != 0 && len > dec->max_num_array_entries )) { 0 0 0
if (expect_false( dec->max_num_array_entries != 0 && len > dec->max_num_array_entries )) { 0 6 6
2138 0 SRL_RDR_ERRORf2(dec->pbuf, "Got input array with %u entries, but the configured maximum is just %u",
2139 (int)len, (int)dec->max_num_array_entries);
2140 6 ▶ } else if (len) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len) { 6 0 6
2141 SV **av_array;
2142 SV **av_end;
2143  
2144 6 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,len," while reading array contents, insufficient remaining tags for specified array size");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,len," while reading array contents, insufficient remaining tags for specified array size"); 6 0 6
SRL_RDR_ASSERT_SPACE(dec->pbuf,len," while reading array contents, insufficient remaining tags for specified array size"); 0 6 6
SRL_RDR_ASSERT_SPACE(dec->pbuf,len," while reading array contents, insufficient remaining tags for specified array size"); 0 6 6
2145 /* Nested containers must fit in the same remaining input. */
2146 6 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,len + dec->owed_bytes," while reading array contents, insufficient remaining tags for enclosing containers");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,len + dec->owed_bytes," while reading array contents, insufficient remaining tags for enclosing containers"); 6 0 6
SRL_RDR_ASSERT_SPACE(dec->pbuf,len + dec->owed_bytes," while reading array contents, insufficient remaining tags for enclosing containers"); 0 6 6
SRL_RDR_ASSERT_SPACE(dec->pbuf,len + dec->owed_bytes," while reading array contents, insufficient remaining tags for enclosing containers"); 0 6 6
2147  
2148 /* make sure the array has room */
2149 6 av_extend((AV*)into, len-1);
2150 /* set the size */
2151 6 AvFILLp(into)= len - 1;
2152  
2153 6 av_array= AvARRAY((AV*)into);
2154 6 av_end= av_array + len;
2155  
2156 6 dec->owed_bytes += len;
2157  
2158 /* Match DEPTH_INCREMENT parity: if entering an element would exceed
2159 * max_recursion_depth, let srl_read_single_value handle it identically. */
2160 6 ▶ if (expect_false(dec->recursion_depth >= dec->max_recursion_depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->recursion_depth >= dec->max_recursion_depth)) { 0 6 6
2161 0 ▶ for ( ; av_array < av_end ; av_array++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; av_array < av_end ; av_array++) { 0 0 0
2162 0 dec->owed_bytes--;
2163 0 *av_array = FRESH_SV();
2164 0 srl_read_single_value(aTHX_ dec, *av_array, av_array);
2165 }
2166 0 return;
2167 }
2168  
2169 14 ▶ for ( ; av_array < av_end ; av_array++) {
Branch: 1 branch
BranchTrueFalseTotal
for ( ; av_array < av_end ; av_array++) { 8 6 14
2170 U8 elem_tag;
2171 8 dec->owed_bytes--;
2172 8 ▶ if (expect_false(SRL_RDR_DONE(dec->pbuf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(SRL_RDR_DONE(dec->pbuf))) { 0 8 8
2173 0 *av_array = FRESH_SV();
2174 0 srl_read_single_value(aTHX_ dec, *av_array, av_array);
2175 0 continue;
2176 }
2177 8 elem_tag = *dec->buf.pos;
2178 8 ▶ if (elem_tag <= SRL_HDR_NEG_HIGH) {
Branch: 1 branch
BranchTrueFalseTotal
if (elem_tag <= SRL_HDR_NEG_HIGH) { 0 8 8
2179 0 const IV iv = (elem_tag <= SRL_HDR_POS_HIGH)
2180 ? (IV)elem_tag
2181 0 ▶ : (IV)elem_tag - 32;
Branch: 1 branch
BranchTrueFalseTotal
: (IV)elem_tag - 32; 0 0 0
2182 0 dec->buf.pos++;
2183 0 srl_array_set_iv(aTHX_ dec, av_array, iv);
2184 8 ▶ } else if (elem_tag >= SRL_HDR_SHORT_BINARY_LOW
Branch: 1 branch
BranchTrueFalseTotal
} else if (elem_tag >= SRL_HDR_SHORT_BINARY_LOW 0 8 8
2185 0 ▶ && elem_tag <= SRL_HDR_SHORT_BINARY_HIGH) {
Branch: 1 branch
BranchTrueFalseTotal
&& elem_tag <= SRL_HDR_SHORT_BINARY_HIGH) { 0 0 0
2186 0 const STRLEN slen = (STRLEN)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(elem_tag);
2187 SV *sv;
2188 0 dec->buf.pos++;
2189 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, slen, "string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, slen, "string"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, slen, "string"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, slen, "string"); 0 0 0
2190 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, slen, " while reading ascii string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, slen, " while reading ascii string"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, slen, " while reading ascii string"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf, slen, " while reading ascii string"); 0 0 0
2191 0 sv = newSVpvn((const char *)dec->buf.pos, slen);
2192 0 dec->buf.pos += slen;
2193 0 ▶ if (expect_false(dec->flags_readonly))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->flags_readonly)) 0 0 0
2194 0 SvREADONLY_on(sv);
2195 0 *av_array = sv;
2196 } else {
2197 8 switch (elem_tag) {
2198 0 case SRL_HDR_VARINT:
2199 {
2200 UV uv;
2201 0 dec->buf.pos++;
2202 0 uv = srl_read_varint_uv(aTHX_ dec->pbuf);
2203 0 ▶ if (expect_true(uv <= (UV)IV_MAX)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true(uv <= (UV)IV_MAX)) { 0 0 0
2204 0 srl_array_set_iv(aTHX_ dec, av_array, (IV)uv);
2205 } else {
2206 0 SV * const sv = newSVuv(uv);
2207 0 ▶ if (expect_false(dec->flags_readonly))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->flags_readonly)) 0 0 0
2208 0 SvREADONLY_on(sv);
2209 0 *av_array = sv;
2210 }
2211 0 break;
2212 }
2213 0 case SRL_HDR_DOUBLE:
2214 {
2215 SV *sv;
2216 0 dec->buf.pos++;
2217 0 sv = newSVnv((NV)srl_read_double_val(aTHX_ dec));
2218 0 ▶ if (expect_false(dec->flags_readonly))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->flags_readonly)) 0 0 0
2219 0 SvREADONLY_on(sv);
2220 0 *av_array = sv;
2221 0 break;
2222 }
2223 0 case SRL_HDR_CANONICAL_UNDEF:
2224 0 ▶ if (expect_false(dec->proto_version < 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 3)) { 0 0 0
2225 0 dec->buf.pos++;
2226 0 SRL_RDR_ERROR(dec->pbuf, "CANONICAL_UNDEF tag requires protocol version 3 or later");
2227 }
2228 /* fall through */
2229 case SRL_HDR_UNDEF:
2230 0 dec->buf.pos++;
2231 0 ▶ if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_USE_UNDEF)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_USE_UNDEF)) { 0 0 0
2232 0 *av_array = &PL_sv_undef;
2233 } else {
2234 0 SV * const sv = FRESH_SV();
2235 0 ▶ if (expect_false(dec->flags_readonly))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->flags_readonly)) 0 0 0
2236 0 SvREADONLY_on(sv);
2237 0 *av_array = sv;
2238 }
2239 0 break;
2240 8 default:
2241 8 *av_array = FRESH_SV();
2242 8 srl_read_single_value(aTHX_ dec, *av_array, av_array);
2243 8 break;
2244 }
2245 }
2246 }
2247 }
2248 }
2249  
2250 #ifndef HV_FETCH_LVALUE
2251 # define OLDHASH
2252 # define IS_LVALUE 1
2253 # define KEYLENTYPE IV
2254 #else
2255 # define KEYLENTYPE STRLEN
2256 #endif
2257  
2258 #ifndef HvRITER_set
2259 # define HvRITER_set(sv,v) HvRITER(sv) = v
2260 #endif
2261  
2262 /* Escape a key for a single-quoted diagnostic. If the escaped form would
2263 * exceed limit, return limit + 1 without writing past the caller's buffer. */
2264 SRL_STATIC_INLINE STRLEN
2265 0 srl_escape_key_for_error(char *dst, const U8 *src, UV len, STRLEN limit)
2266 {
2267 static const char hex[] = "0123456789ABCDEF";
2268 0 STRLEN escaped = 0;
2269 UV i;
2270  
2271 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
2272 0 const U8 c = src[i];
2273 0 ▶ STRLEN width = (c >= 0x20 && c <= 0x7E && c != '\\' && c != '\'') ? 1
Branch: 3 branches
BranchTrueFalseTotal
STRLEN width = (c >= 0x20 && c <= 0x7E && c != '\\' && c != '\'') ? 1 0 0 0
STRLEN width = (c >= 0x20 && c <= 0x7E && c != '\\' && c != '\'') ? 1 0 0 0
STRLEN width = (c >= 0x20 && c <= 0x7E && c != '\\' && c != '\'') ? 1 0 0 0
2274 0 ▶ : (c == '\\' || c == '\'') ? 2 : 4;
Branch: 3 branches
BranchTrueFalseTotal
: (c == '\\' || c == '\'') ? 2 : 4; 0 0 0
: (c == '\\' || c == '\'') ? 2 : 4; 0 0 0
: (c == '\\' || c == '\'') ? 2 : 4; 0 0 0
2275 0 ▶ if (width > limit - escaped)
Branch: 1 branch
BranchTrueFalseTotal
if (width > limit - escaped) 0 0 0
2276 0 return limit + 1;
2277 0 ▶ if (dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst) { 0 0 0
2278 0 ▶ if (width == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (width == 1) { 0 0 0
2279 0 dst[escaped] = (char)c;
2280 0 ▶ } else if (width == 2) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (width == 2) { 0 0 0
2281 0 dst[escaped] = '\\';
2282 0 dst[escaped + 1] = (char)c;
2283 } else {
2284 0 dst[escaped] = '\\';
2285 0 dst[escaped + 1] = 'x';
2286 0 dst[escaped + 2] = hex[c >> 4];
2287 0 dst[escaped + 3] = hex[c & 0x0F];
2288 }
2289 }
2290 0 escaped += width;
2291 }
2292 0 ▶ if (dst)
Branch: 1 branch
BranchTrueFalseTotal
if (dst) 0 0 0
2293 0 dst[escaped] = '\0';
2294 0 return escaped;
2295 }
2296  
2297 /* Out of line: its buffers would otherwise be part of every recursive
2298 * srl_read_hash frame and shrink the depth that fits on the C stack. */
2299 SRL_NOINLINE static void
2300 0 srl_croak_duplicate_key(pTHX_ srl_decoder_t *dec, const U8 *from, UV key_len_uv)
2301 {
2302 char prefix[65], suffix[65], display[192], full_key[192];
2303 STRLEN full_key_len;
2304 int display_len;
2305  
2306 0 ▶ if (key_len_uv > 32) {
Branch: 1 branch
BranchTrueFalseTotal
if (key_len_uv > 32) { 0 0 0
2307 0 STRLEN prefix_len = srl_escape_key_for_error(prefix, from, 16, 64);
2308 0 STRLEN suffix_len = srl_escape_key_for_error(suffix,
2309 0 from + key_len_uv - 16, 16, 64);
2310  
2311 0 display_len = my_snprintf(display, sizeof(display),
2312 "'%.*s' ... %"UVuf" bytes elided ... '%.*s'",
2313 (int)prefix_len, prefix, key_len_uv - 32,
2314 (int)suffix_len, suffix);
2315 0 ▶ if (expect_false(display_len < 0
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(display_len < 0 0 0 0
if (expect_false(display_len < 0 0 0 0
if (expect_false(display_len < 0 0 0 0
2316 || (size_t)display_len >= sizeof(display)))
2317 0 SRL_RDR_ERROR(dec->pbuf, "duplicate key in hash");
2318 0 full_key_len = srl_escape_key_for_error(NULL, from,
2319 0 key_len_uv, (STRLEN)display_len - 2);
2320 0 ▶ if ((STRLEN)display_len < full_key_len + 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((STRLEN)display_len < full_key_len + 2) 0 0 0
2321 0 SRL_RDR_ERRORf2(dec->pbuf,
2322 "duplicate key %.*s in hash",
2323 display_len, display);
2324 }
2325 0 full_key_len = srl_escape_key_for_error(full_key, from,
2326 key_len_uv, sizeof(full_key) - 3);
2327 0 ▶ if (expect_false(full_key_len >= sizeof(full_key) - 2))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(full_key_len >= sizeof(full_key) - 2)) 0 0 0
2328 0 SRL_RDR_ERROR(dec->pbuf, "duplicate key in hash");
2329 0 display_len = my_snprintf(display, sizeof(display),
2330 "'%.*s'", (int)full_key_len, full_key);
2331 0 ▶ if (expect_false(display_len < 0
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(display_len < 0 0 0 0
if (expect_false(display_len < 0 0 0 0
if (expect_false(display_len < 0 0 0 0
2332 || (size_t)display_len >= sizeof(display)))
2333 0 SRL_RDR_ERROR(dec->pbuf, "duplicate key in hash");
2334 0 SRL_RDR_ERRORf2(dec->pbuf,
2335 "duplicate key %.*s in hash", display_len, display);
2336 }
2337  
2338 SRL_STATIC_INLINE void
2339 15 srl_read_hash(pTHX_ srl_decoder_t *dec, SV* into, U8 tag) {
2340 UV num_keys;
2341 15 ▶ if (tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag) { 15 0 15
2342 15 SV *referent= (SV *)newHV();
2343 15 num_keys= tag & 15;
2344 15 ▶ SRL_sv_set_rv_to(into, referent);
Branch: 15 branches
BranchTrueFalseTotal
SRL_sv_set_rv_to(into, referent); 10 5 15
SRL_sv_set_rv_to(into, referent); 8 2 10
SRL_sv_set_rv_to(into, referent); 5 2 7
SRL_sv_set_rv_to(into, referent); 1 4 5
SRL_sv_set_rv_to(into, referent); 0 1 1
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 1 1
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 1 0 1
SRL_sv_set_rv_to(into, referent); 0 1 1
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
2345 15 into= referent;
2346 } else {
2347 0 num_keys= srl_read_varint_uv_count(aTHX_ dec->pbuf, " while reading HASH");
2348 0 ▶ (void)SvUPGRADE(into, SVt_PVHV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(into, SVt_PVHV); 0 0 0
2349 }
2350 /* in some versions of Perl HvRITER() is not properly set on an upgrade SV
2351 * so we explicitly set it ourselves */
2352 #ifdef FIXUP_RITER
2353 HvRITER_set(into,-1);
2354 #endif
2355  
2356 /* Limit the maximum number of hash keys that we accept to whetever was configured */
2357 15 ▶ if (expect_false( dec->max_num_hash_entries != 0 && num_keys > dec->max_num_hash_entries )) {
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( dec->max_num_hash_entries != 0 && num_keys > dec->max_num_hash_entries )) { 0 15 15
if (expect_false( dec->max_num_hash_entries != 0 && num_keys > dec->max_num_hash_entries )) { 0 0 0
if (expect_false( dec->max_num_hash_entries != 0 && num_keys > dec->max_num_hash_entries )) { 0 15 15
2358 0 SRL_RDR_ERRORf2(dec->pbuf, "Got input hash with %u entries, but the configured maximum is just %u",
2359 (int)num_keys, (int)dec->max_num_hash_entries);
2360 }
2361  
2362 15 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2," while reading hash contents, insufficient remaining tags for number of keys specified");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2," while reading hash contents, insufficient remaining tags for number of keys specified"); 15 0 15
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2," while reading hash contents, insufficient remaining tags for number of keys specified"); 0 15 15
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2," while reading hash contents, insufficient remaining tags for number of keys specified"); 0 15 15
2363 15 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2 + dec->owed_bytes," while reading hash contents, insufficient remaining tags for enclosing containers");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2 + dec->owed_bytes," while reading hash contents, insufficient remaining tags for enclosing containers"); 15 0 15
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2 + dec->owed_bytes," while reading hash contents, insufficient remaining tags for enclosing containers"); 0 15 15
SRL_RDR_ASSERT_SPACE(dec->pbuf,num_keys*2 + dec->owed_bytes," while reading hash contents, insufficient remaining tags for enclosing containers"); 0 15 15
2364  
2365 15 HvSHAREKEYS_on(into); /* apparently required on older perls */
2366  
2367 15 hv_ksplit((HV *)into, num_keys); /* make sure we have enough room */
2368 /* NOTE: contents of hash are stored VALUE/KEY, reverse from normal perl
2369 * storage, this is because it simplifies the hash storage logic somewhat */
2370 15 dec->owed_bytes += num_keys * 2;
2371 32 ▶ for (; num_keys > 0 ; num_keys--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; num_keys > 0 ; num_keys--) { 17 15 32
2372 const U8 *from;
2373 U8 tag;
2374 SV **fetched_sv;
2375 UV key_len_uv;
2376 #ifndef OLDHASH
2377 17 U32 flags= 0;
2378 #endif
2379 KEYLENTYPE key_len;
2380 17 ▶ const UV keys_before = HvKEYS((HV *)into);
Branch: 1 branch
BranchTrueFalseTotal
const UV keys_before = HvKEYS((HV *)into); 0 17 17
2381  
2382 17 dec->owed_bytes -= 2;
2383 17 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,1," while reading key tag byte for HASH");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,1," while reading key tag byte for HASH"); 0 17 17
2384 17 tag= (*dec->buf.pos++)&127;
2385 17 ▶ if (IS_SRL_HDR_SHORT_BINARY(tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IS_SRL_HDR_SHORT_BINARY(tag)) { 17 0 17
2386 17 key_len_uv= SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
2387 17 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 17 17
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 17 17
2388 17 key_len= (KEYLENTYPE)key_len_uv;
2389 17 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string/SHORT_BINARY key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string/SHORT_BINARY key"); 17 0 17
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string/SHORT_BINARY key"); 0 17 17
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string/SHORT_BINARY key"); 0 17 17
2390 17 from= dec->buf.pos;
2391 17 dec->buf.pos += key_len;
2392 0 ▶ } else if (tag == SRL_HDR_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (tag == SRL_HDR_BINARY) { 0 0 0
2393 0 key_len_uv= srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading string/BINARY key");
2394 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
2395 0 key_len= (KEYLENTYPE)key_len_uv;
2396 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary key"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary key"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary key"); 0 0 0
2397 0 from= dec->buf.pos;
2398 0 dec->buf.pos += key_len;
2399 0 ▶ } else if (tag == SRL_HDR_STR_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (tag == SRL_HDR_STR_UTF8) { 0 0 0
2400 0 key_len_uv= srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading UTF8 key");
2401 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
2402 0 key_len= (KEYLENTYPE)key_len_uv;
2403 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string key"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string key"); 0 0 0
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading string key"); 0 0 0
2404 0 from= dec->buf.pos;
2405 0 dec->buf.pos += key_len;
2406 #ifdef OLDHASH
2407 key_len= -key_len;
2408 #else
2409 0 flags= HVhek_UTF8;
2410 #endif
2411 0 ▶ } else if (tag == SRL_HDR_COPY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (tag == SRL_HDR_COPY) { 0 0 0
2412 0 const U8 *copytag_start = dec->buf.pos - 1;
2413 0 UV ofs= srl_read_backref_offset(aTHX_ dec, " while reading COPY tag");
2414 0 from= dec->buf.body_pos + ofs;
2415 0 tag= *from++ & ~SRL_HDR_TRACK_FLAG;
2416 0 ▶ if (IS_SRL_HDR_SHORT_BINARY(tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IS_SRL_HDR_SHORT_BINARY(tag)) { 0 0 0
2417 0 key_len_uv= SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
2418 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
2419 0 key_len= (KEYLENTYPE)key_len_uv;
2420 }
2421 else
2422 0 ▶ if (tag == SRL_HDR_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_BINARY) { 0 0 0
2423 0 key_len_uv = S_read_varint_uv_length_char_ptr(
2424 aTHX_ &from, copytag_start,
2425 " while reading binary key via COPY"
2426 );
2427 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
2428 0 key_len = (KEYLENTYPE)key_len_uv;
2429 }
2430 else
2431 0 ▶ if (tag == SRL_HDR_STR_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_STR_UTF8) { 0 0 0
2432 0 key_len_uv = S_read_varint_uv_length_char_ptr(
2433 aTHX_ &from, copytag_start,
2434 " while reading UTF8-encoded key via COPY"
2435 );
2436 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "hash key"); 0 0 0
2437 0 key_len = (KEYLENTYPE)key_len_uv;
2438 #ifdef OLDHASH
2439 key_len= -key_len;
2440 #else
2441 0 flags= HVhek_UTF8;
2442 #endif
2443 }
2444 else {
2445 0 SRL_RDR_ERROR_BAD_COPY(dec->pbuf, SRL_HDR_HASH);
2446 }
2447 0 ▶ if (expect_false(from > copytag_start
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(from > copytag_start 0 0 0
if (expect_false(from > copytag_start 0 0 0
if (expect_false(from > copytag_start 0 0 0
2448 || (UV)(copytag_start - from) < key_len_uv)) {
2449 0 SRL_RDR_ERRORf2(dec->pbuf,
2450 COPY_OFFSET_ERROR_FMT_PREFIX " while reading key", (UV)ofs, key_len_uv);
2451 }
2452 } else {
2453 0 SRL_RDR_ERROR_UNEXPECTED(dec->pbuf, tag, "a stringish type");
2454 }
2455 17 ▶ if (SvREADONLY(into)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(into)) { 0 17 17
2456 0 SvREADONLY_off(into);
2457 }
2458 #ifdef OLDHASH
2459 fetched_sv= hv_fetch((HV *)into, (char *)from, key_len, IS_LVALUE);
2460 #else
2461 17 fetched_sv= (SV **) hv_common((HV *)into, NULL, (char *)from, key_len, flags, HV_FETCH_LVALUE|HV_FETCH_JUST_SV, NULL, 0);
2462 #endif
2463 17 ▶ if (expect_false( !fetched_sv )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !fetched_sv )) { 0 17 17
2464 0 SRL_RDR_ERROR_PANIC(dec->pbuf, "failed to hv_store");
2465 }
2466 else
2467 17 ▶ if ( expect_false( (UV)HvKEYS((HV *)into) == keys_before ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( expect_false( (UV)HvKEYS((HV *)into) == keys_before ) ) { 0 17 17
if ( expect_false( (UV)HvKEYS((HV *)into) == keys_before ) ) { 0 17 17
2468 0 srl_croak_duplicate_key(aTHX_ dec, from, key_len_uv);
2469 }
2470 17 srl_read_single_value(aTHX_ dec, *fetched_sv, fetched_sv );
2471 }
2472 15 }
2473  
2474 /* read reference to next thing */
2475 SRL_STATIC_INLINE void
2476 8 srl_read_refn(pTHX_ srl_decoder_t *dec, SV* into)
2477 {
2478 SV *referent;
2479 U8 tag;
2480 8 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading REFN referent");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading REFN referent"); 0 8 8
2481 8 tag= *(dec->buf.pos); /* Look ahead for special vars. */
2482 8 ▶ if (tag == SRL_HDR_TRUE) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_TRUE) { 0 8 8
2483 0 dec->buf.pos++;
2484 0 referent= &PL_sv_yes;
2485 }
2486 8 ▶ else if (tag == SRL_HDR_FALSE) {
Branch: 1 branch
BranchTrueFalseTotal
else if (tag == SRL_HDR_FALSE) { 0 8 8
2487 0 dec->buf.pos++;
2488 0 referent= &PL_sv_no;
2489 }
2490 /*
2491 * Note the below is guarded by an option as we have use SRL_HDR_UNDEF
2492 * also to represent "any SV which is undef", and using to represent
2493 * true PL_sv_undef will break things.
2494 *
2495 * We need a new, different tag for true perl undef.
2496 *
2497 */
2498 else
2499 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 8 0 8
2500 ( tag == SRL_HDR_CANONICAL_UNDEF )
2501 8 ||
2502 8 ▶ ( SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF) && tag == SRL_HDR_UNDEF )
Branch: 2 branches
BranchTrueFalseTotal
( SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF) && tag == SRL_HDR_UNDEF ) 0 8 8
( SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF) && tag == SRL_HDR_UNDEF ) 0 0 0
2503 ) {
2504 0 dec->buf.pos++;
2505 0 referent= &PL_sv_undef;
2506 }
2507 else {
2508 8 referent= FRESH_SV();
2509 8 SvTEMP_off(referent);
2510 8 tag = 0;
2511 }
2512 8 ▶ SRL_sv_set_rv_to(into, referent);
Branch: 15 branches
BranchTrueFalseTotal
SRL_sv_set_rv_to(into, referent); 8 0 8
SRL_sv_set_rv_to(into, referent); 8 0 8
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
2513 8 ▶ if (!tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tag) { 8 0 8
2514 8 srl_read_single_value_ex(aTHX_ dec, referent, NULL, 1);
2515 }
2516 8 }
2517  
2518 SRL_STATIC_INLINE SV *
2519 srl_follow_refp_alias_reference(pTHX_ srl_decoder_t *dec, UV offset, int is_refn_referent)
2520 {
2521 SV* into = sv_2mortal(FRESH_SV());
2522 srl_reader_char_ptr orig_pos = dec->buf.pos;
2523 srl_reader_char_ptr new_pos = dec->buf.body_pos + offset;
2524  
2525 if (new_pos >= orig_pos) {
2526 SRL_RDR_ERROR(dec->pbuf, "Corrupted packed. Reference offset points forward!");
2527 }
2528  
2529 /* Remember the target before reading it, as if it had been tracked:
2530 * a reference inside it to the same offset (a cycle) then finds it
2531 * instead of reading it again until the recursion limit, and so does
2532 * every later reference, which would otherwise read it again each
2533 * time (exponentially often for a chain of such references). */
2534 PTABLE_store(dec->ref_seenhash, (void *)offset, (void *)into);
2535  
2536 dec->buf.pos = new_pos;
2537 /* a REFP target is the referent of the original REFN, so it may be a
2538 * bare ARRAY or HASH tag; an ALIAS target never is */
2539 srl_read_single_value_ex(aTHX_ dec, into, NULL, is_refn_referent);
2540 dec->buf.pos = orig_pos;
2541 return into;
2542 }
2543  
2544 SRL_STATIC_INLINE AV *
2545 srl_follow_objectv_reference(pTHX_ srl_decoder_t *dec, UV offset)
2546 {
2547 AV *av= NULL;
2548 srl_reader_char_ptr orig_pos = dec->buf.pos;
2549 srl_reader_char_ptr new_pos = dec->buf.body_pos + offset;
2550  
2551 if (new_pos >= orig_pos) {
2552 SRL_RDR_ERROR(dec->pbuf, "Corrupted packed. Reference offset points forward!");
2553 }
2554  
2555 SRL_ASSERT_REF_PTR_TABLES(dec); /* init dec->ref_stashes and dec->ref_bless_av */
2556  
2557 dec->buf.pos = new_pos;
2558 /* call srl_read_object() with read_class_name_only=1 */
2559 /* into and obj_tag are not used in this case */
2560 srl_read_object(aTHX_ dec, NULL, 0, 1);
2561 av= (AV *)PTABLE_fetch(dec->ref_bless_av, (void *)offset);
2562 dec->buf.pos = orig_pos;
2563 return av;
2564 }
2565  
2566 /* reuse a reference to a previous item */
2567 SRL_STATIC_INLINE void
2568 0 srl_read_refp(pTHX_ srl_decoder_t *dec, SV* into)
2569 {
2570 /* something we did before */
2571 0 UV ofs= srl_read_backref_offset(aTHX_ dec, " while reading REFP tag");
2572 0 SV *referent= srl_fetch_item(aTHX_ dec, ofs, "REFP");
2573  
2574 #ifdef FOLLOW_REFERENCES_IF_NOT_STASHED
2575 if (referent == NULL)
2576 referent = srl_follow_refp_alias_reference(aTHX_ dec, ofs, 1);
2577 #endif
2578  
2579 0 (void)SvREFCNT_inc(referent);
2580  
2581 0 ▶ SRL_sv_set_rv_to(into, referent);
Branch: 15 branches
BranchTrueFalseTotal
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
SRL_sv_set_rv_to(into, referent); 0 0 0
2582  
2583 #if USE_588_WORKAROUND
2584 /* See 'define USE_588_WORKAROUND' above for a discussion of what this does. */
2585 if (SvOBJECT(referent)) {
2586 HV *stash = SvSTASH(referent);
2587 if (Gv_AMG(stash))
2588 SvAMAGIC_on(into);
2589 }
2590 #endif
2591 0 srl_fetch_register_frozen_object(aTHX_ dec, into, 1);
2592 0 }
2593  
2594  
2595 SRL_STATIC_INLINE void
2596 0 srl_read_weaken(pTHX_ srl_decoder_t *dec, SV* into)
2597 {
2598 SV* referent;
2599 /* TODO This really just wants a subset of the states that srl_read_single_value covers, right?
2600 * Optimization opportunity? Or robustness against invalid packets issue? */
2601 0 srl_read_single_value(aTHX_ dec, into, NULL);
2602 0 ▶ if (expect_false( !SvROK(into) ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !SvROK(into) )) 0 0 0
2603 0 SRL_RDR_ERROR(dec->pbuf, "WEAKEN op");
2604 0 referent= SvRV(into);
2605 /* we have to be careful to not allow the referent's refcount
2606 * to go to zero in the process of us weakening the ref.
2607 * For instance this may be aliased or reused later by a non-weakref
2608 * which will "fix" the refcount, however we need to be able to deserialize
2609 * in the opposite order, so if the referent's refcount is 1
2610 * we increment it and stuff it in the weakref_av before we call
2611 * sv_rvweaken(), right before we exit we clear any items from
2612 * that array, which does the REFCNT_dec for us, and everything
2613 * works out ok. */
2614 0 ▶ if (expect_true( SvREFCNT(referent)==1 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( SvREFCNT(referent)==1 )) { 0 0 0
2615 0 ▶ if (expect_false( !dec->weakref_av ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !dec->weakref_av )) 0 0 0
2616 0 dec->weakref_av= newAV();
2617 0 av_push(dec->weakref_av, SvREFCNT_inc(referent));
2618 0 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE);
2619 }
2620  
2621 /* If read-only reference, set to rw only to weaken it, otherwise "Modification of a read-only value attempted". */
2622 0 ▶ if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(into)) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(into)) { 0 0 0
if ( SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_READONLY_FLAGS) && SvREADONLY(into)) { 0 0 0
2623 0 SvREADONLY_off(into);
2624 0 sv_rvweaken(into);
2625 0 SvREADONLY_on(into);
2626 } else {
2627 0 sv_rvweaken(into);
2628 }
2629 0 }
2630  
2631 /* dec->ref_stashes maps the offset of a class name to its stash, or to
2632 * the class name itself (a mortal string) while no package of that name
2633 * exists: reading a class name must not create a package, as that is
2634 * permanent. Returns the stash, creating the package if necessary. */
2635 SRL_STATIC_INLINE HV *
2636 0 srl_class_stash(pTHX_ srl_decoder_t *dec, UV pos, SV *class_entry)
2637 {
2638 HV *stash;
2639 0 ▶ if (SvTYPE(class_entry) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(class_entry) == SVt_PVHV) 0 0 0
2640 0 return (HV *)class_entry;
2641 0 stash= gv_stashsv(class_entry, GV_ADD);
2642 0 PTABLE_store(dec->ref_stashes, (void *)pos, (void *)stash);
2643 0 return stash;
2644 }
2645  
2646 /* With alias_smallint/alias_varint_under, small integers decode to
2647 * read-only scalars from dec->alias_cache, shared by every decode on this
2648 * decoder and its clones. A REFP can make one the referent of an OBJECT,
2649 * and blessing it (set_readonly lifts the read-only flag to do so) would
2650 * leave the shared integer blessed for later decodes. */
2651 SRL_STATIC_INLINE void
2652 4 srl_refuse_blessing_alias(pTHX_ srl_decoder_t *dec, SV *referent)
2653 {
2654 4 ▶ if (dec->alias_cache && SvIOK(referent) && !SvIsUV(referent)
Branch: 3 branches
BranchTrueFalseTotal
if (dec->alias_cache && SvIOK(referent) && !SvIsUV(referent) 0 4 4
if (dec->alias_cache && SvIOK(referent) && !SvIsUV(referent) 0 0 0
if (dec->alias_cache && SvIOK(referent) && !SvIsUV(referent) 0 0 0
2655 0 ▶ && IS_IV_ALIAS(dec, SvIVX(referent))
Branch: 3 branches
BranchTrueFalseTotal
&& IS_IV_ALIAS(dec, SvIVX(referent)) 0 0 0
&& IS_IV_ALIAS(dec, SvIVX(referent)) 0 0 0
&& IS_IV_ALIAS(dec, SvIVX(referent)) 0 0 0
2656 0 ▶ && AvARRAY(dec->alias_cache)[SvIVX(referent) + 16] == referent)
Branch: 1 branch
BranchTrueFalseTotal
&& AvARRAY(dec->alias_cache)[SvIVX(referent) + 16] == referent) 0 0 0
2657 {
2658 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. Cannot bless a shared alias value");
2659 }
2660 4 }
2661  
2662 /* Perl changed qr// from a magical scalar to SVt_REGEXP. Check the
2663 * referent, not just the class name: a packet can claim any class. */
2664 SRL_STATIC_INLINE int
2665 0 srl_is_canonical_regexp_object(pTHX_ SV *value, SV *class_entry)
2666 {
2667 const char *class_name;
2668 STRLEN class_len;
2669  
2670 0 ▶ if (!SvROK(value) || !class_entry)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(value) || !class_entry) 0 0 0
if (!SvROK(value) || !class_entry) 0 0 0
2671 0 return 0;
2672 #ifdef MODERN_REGEXP
2673 0 ▶ if (SvTYPE(SvRV(value)) != SVt_REGEXP)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(value)) != SVt_REGEXP) 0 0 0
2674 0 return 0;
2675 #else
2676 if (SvTYPE(SvRV(value)) != SVt_PVMG ||
2677 !mg_find(SvRV(value), PERL_MAGIC_qr))
2678 return 0;
2679 #endif
2680 0 ▶ if (SvTYPE(class_entry) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(class_entry) == SVt_PVHV) { 0 0 0
2681 0 ▶ class_name = HvNAME_get((HV *)class_entry);
Branch: 6 branches
BranchTrueFalseTotal
class_name = HvNAME_get((HV *)class_entry); 0 0 0
class_name = HvNAME_get((HV *)class_entry); 0 0 0
class_name = HvNAME_get((HV *)class_entry); 0 0 0
class_name = HvNAME_get((HV *)class_entry); 0 0 0
class_name = HvNAME_get((HV *)class_entry); 0 0 0
class_name = HvNAME_get((HV *)class_entry); 0 0 0
2682 0 ▶ class_len = HvNAMELEN_get((HV *)class_entry);
Branch: 6 branches
BranchTrueFalseTotal
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
class_len = HvNAMELEN_get((HV *)class_entry); 0 0 0
2683 } else {
2684 0 class_name = SvPV(class_entry, class_len);
2685 }
2686 0 ▶ return class_name && class_len == 6 && memEQ(class_name, "Regexp", 6);
Branch: 3 branches
BranchTrueFalseTotal
return class_name && class_len == 6 && memEQ(class_name, "Regexp", 6); 0 0 0
return class_name && class_len == 6 && memEQ(class_name, "Regexp", 6); 0 0 0
return class_name && class_len == 6 && memEQ(class_name, "Regexp", 6); 0 0 0
2687 }
2688  
2689 SRL_STATIC_INLINE void
2690 0 srl_read_objectv(pTHX_ srl_decoder_t *dec, SV* into, U8 obj_tag)
2691 {
2692 0 AV *av= NULL;
2693 STRLEN ofs;
2694  
2695 0 ▶ if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE &&
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE && 0 0 0
if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE && 0 0 0
if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE && 0 0 0
2696 SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) ))
2697 0 SRL_RDR_ERROR_REFUSE_OBJECT(dec->pbuf);
2698  
2699 0 ofs= srl_read_backref_offset(aTHX_ dec, " while reading OBJECTV(_FREEZE) classname");
2700  
2701 0 ▶ if (expect_false( !dec->ref_bless_av )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( !dec->ref_bless_av )) { 0 0 0
2702 #ifdef FOLLOW_REFERENCES_IF_NOT_STASHED
2703 SRL_ASSERT_REF_PTR_TABLES(dec); /* init dec->ref_stashes and dec->ref_bless_av */
2704 #else
2705 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. OBJECTV(_FREEZE) used without "
2706 "preceding OBJECT(_FREEZE) to define classname");
2707 #endif
2708 }
2709  
2710 0 av= (AV *)PTABLE_fetch(dec->ref_bless_av, (void *)ofs);
2711 0 ▶ if (expect_false( NULL == av )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( NULL == av )) { 0 0 0
2712 #ifdef FOLLOW_REFERENCES_IF_NOT_STASHED
2713 av = srl_follow_objectv_reference(aTHX_ dec, (UV) ofs);
2714 if (expect_false( NULL == av ))
2715 #endif
2716 0 SRL_RDR_ERRORf1(dec->pbuf, "Corrupted packet. OBJECTV(_FREEZE) references unknown classname offset: %"UVuf, (UV)ofs);
2717 }
2718  
2719 {
2720 0 SV *class_entry = (SV *)PTABLE_fetch(dec->ref_stashes, (void *)ofs);
2721 0 ▶ if (expect_false(!class_entry))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!class_entry)) 0 0 0
2722 0 SRL_RDR_ERROR(dec->pbuf, "Missing class for OBJECTV(_FREEZE)");
2723 0 srl_track_class(aTHX_ dec, class_entry);
2724 }
2725  
2726 /* checking tag: SRL_HDR_OBJECTV_FREEZE or SRL_HDR_OBJECTV? */
2727 0 ▶ if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( obj_tag == SRL_HDR_OBJECTV_FREEZE )) { 0 0 0
2728 0 SV *class_entry= (SV *) PTABLE_fetch(dec->ref_stashes, (void *)ofs);
2729 0 ▶ if (expect_false( class_entry == NULL ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( class_entry == NULL )) 0 0 0
2730 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. OBJECTV(_FREEZE) used without "
2731 "preceding OBJECT(_FREEZE) to define classname");
2732 0 srl_read_frozen_object(aTHX_ dec, srl_class_stash(aTHX_ dec, ofs, class_entry), into);
2733 } else {
2734 /* SRL_HDR_OBJECTV, not SRL_HDR_OBJECTV_FREEZE */
2735 /* now deparse the thing we are going to bless */
2736 0 srl_read_single_value(aTHX_ dec, into, NULL);
2737  
2738 0 ▶ if (expect_false(!SvROK(into)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!SvROK(into))) 0 0 0
2739 0 SRL_RDR_ERROR(dec->pbuf, "OBJECT(V) must be followed by a reference");
2740 0 ▶ if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS))) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS))) { 0 0 0
2741 0 SV *class_entry = (SV *)PTABLE_fetch(dec->ref_stashes, (void *)ofs);
2742 0 ▶ if (!srl_is_canonical_regexp_object(aTHX_ into, class_entry))
Branch: 1 branch
BranchTrueFalseTotal
if (!srl_is_canonical_regexp_object(aTHX_ into, class_entry)) 0 0 0
2743 0 SRL_RDR_ERROR_REFUSE_OBJECT(dec->pbuf);
2744 }
2745 0 srl_refuse_blessing_alias(aTHX_ dec, SvRV(into));
2746  
2747 /* and also stuff it into the av - we dont have to do any more book-keeping */
2748 0 av_push(av, SvREFCNT_inc(into));
2749  
2750 #if USE_588_WORKAROUND
2751 {
2752 /* See 'define USE_588_WORKAROUND' above for a discussion of what this does. */
2753 SV *class_entry= PTABLE_fetch(dec->ref_stashes, (void *)ofs);
2754 if (expect_false( class_entry == NULL ))
2755 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. OBJECTV(_FREEZE) used without "
2756 "preceding OBJECT(_FREEZE) to define classname");
2757 if (!SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NO_BLESS_OBJECTS)) {
2758 HV *stash = srl_class_stash(aTHX_ dec, ofs, class_entry);
2759 if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, stash))
2760 SRL_RDR_ERRORf1(dec->pbuf, "Object class '%s' is not in allow_classes", HvNAME(stash));
2761 sv_bless(into, stash);
2762 }
2763 }
2764 #endif
2765 }
2766  
2767 0 }
2768  
2769 SRL_STATIC_INLINE void
2770 4 srl_read_object(pTHX_ srl_decoder_t *dec, SV* into, U8 obj_tag, int read_class_name_only)
2771 {
2772 4 SV *class_entry= NULL; /* stash or class name, see srl_class_stash() */
2773 4 AV *av= NULL;
2774 4 STRLEN storepos= 0;
2775 4 UV ofs= 0;
2776 4 UV key_len_uv= 0;
2777 4 I32 flags= GV_ADD;
2778 U8 tag;
2779 4 U32 key_len = 0;
2780 4 const U8 *from = NULL;
2781  
2782 4 ▶ if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE &&
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE && 0 4 4
if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE && 0 0 0
if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE && 0 4 4
2783 SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) ))
2784 0 SRL_RDR_ERROR_REFUSE_OBJECT(dec->pbuf);
2785  
2786 /* Now find the class name - first check if this is a copy op
2787 * this is bit tricky, as we could have a copy of a raw string.
2788 * We could also have a copy of a previously mentioned class
2789 * name. We have to handle both, which leads to some non-linear
2790 * code flow in the below code. */
2791 4 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,1," while reading classname tag");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,1," while reading classname tag"); 0 4 4
2792  
2793 /* Now read the class name and cache it */
2794 4 storepos= SRL_RDR_BODY_POS_OFS(dec->pbuf);
2795 4 tag= *dec->buf.pos++;
2796  
2797 4 ▶ if (IS_SRL_HDR_SHORT_BINARY(tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IS_SRL_HDR_SHORT_BINARY(tag)) { 4 0 4
2798 4 key_len_uv= SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
2799 4 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 4 4
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 4 4
2800 4 key_len= (U32)key_len_uv;
2801 4 from= dec->buf.pos;
2802 4 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading short binary");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading short binary"); 0 4 4
2803 4 dec->buf.pos += key_len;
2804 }
2805 else
2806 0 ▶ if (tag == SRL_HDR_STR_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_STR_UTF8) { 0 0 0
2807 0 key_len_uv= srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading UTF8 class name");
2808 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
2809 0 ▶ if (expect_false((UV)(U32)key_len_uv != key_len_uv))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((UV)(U32)key_len_uv != key_len_uv)) 0 0 0
2810 0 SRL_RDR_ERROR(dec->pbuf, "Class name is too large for this platform");
2811 0 key_len= (U32)key_len_uv;
2812 0 flags = flags | SVf_UTF8;
2813 0 from= dec->buf.pos;
2814 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading utf8 string");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading utf8 string"); 0 0 0
2815 0 dec->buf.pos += key_len;
2816 }
2817 else
2818 0 ▶ if (tag == SRL_HDR_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_BINARY) { 0 0 0
2819 0 key_len_uv= srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading string/BINARY class name");
2820 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
2821 0 ▶ if (expect_false((UV)(U32)key_len_uv != key_len_uv))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((UV)(U32)key_len_uv != key_len_uv)) 0 0 0
2822 0 SRL_RDR_ERROR(dec->pbuf, "Class name is too large for this platform");
2823 0 key_len= (U32)key_len_uv;
2824 0 from= dec->buf.pos;
2825 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf,key_len," while reading binary"); 0 0 0
2826 0 dec->buf.pos += key_len;
2827 }
2828 else
2829 0 ▶ if (tag == SRL_HDR_COPY) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_COPY) { 0 0 0
2830 0 const U8 *copytag_start = dec->buf.pos - 1;
2831 0 ofs= srl_read_backref_offset(aTHX_ dec, " while reading COPY class name");
2832 0 storepos= ofs;
2833 /* if this string was seen before as part of a classname then we expect
2834 * a stash available below. However it might have been serialized as a key
2835 * or something like that, which would mean we don't have an entry in ref_stashes
2836 * anymore. So first we check if we have a stash. If we do, then we can avoid
2837 * some work. */
2838 0 ▶ if (expect_true( dec->ref_stashes != NULL )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( dec->ref_stashes != NULL )) { 0 0 0
2839 0 class_entry= (SV *) PTABLE_fetch(dec->ref_stashes, (void *)ofs);
2840 }
2841 /* Check if we actually got a class_entry back. If we didn't then we need
2842 * to deserialize the class name */
2843 0 ▶ if (!class_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!class_entry) { 0 0 0
2844 0 from= dec->buf.body_pos + ofs;
2845 0 tag= *from++ & ~SRL_HDR_TRACK_FLAG;
2846 0 ▶ if (IS_SRL_HDR_SHORT_BINARY(tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (IS_SRL_HDR_SHORT_BINARY(tag)) { 0 0 0
2847 0 key_len_uv= SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
2848 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
2849 0 key_len= (U32)key_len_uv;
2850 }
2851 else
2852 0 ▶ if (tag == SRL_HDR_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_BINARY) { 0 0 0
2853 0 key_len_uv = S_read_varint_uv_length_char_ptr(
2854 aTHX_ &from, copytag_start,
2855 " while reading class name via COPY"
2856 );
2857 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
2858 0 ▶ if (expect_false((UV)(U32)key_len_uv != key_len_uv))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((UV)(U32)key_len_uv != key_len_uv)) 0 0 0
2859 0 SRL_RDR_ERROR(dec->pbuf, "Class name is too large for this platform");
2860 0 key_len = (U32)key_len_uv;
2861 }
2862 else
2863 0 ▶ if (tag == SRL_HDR_STR_UTF8) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == SRL_HDR_STR_UTF8) { 0 0 0
2864 0 key_len_uv = S_read_varint_uv_length_char_ptr(
2865 aTHX_ &from, copytag_start,
2866 " while reading UTF8 class name via COPY"
2867 );
2868 0 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, key_len_uv, "class name"); 0 0 0
2869 0 ▶ if (expect_false((UV)(U32)key_len_uv != key_len_uv))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false((UV)(U32)key_len_uv != key_len_uv)) 0 0 0
2870 0 SRL_RDR_ERROR(dec->pbuf, "Class name is too large for this platform");
2871 0 key_len = (U32)key_len_uv;
2872 0 flags = flags | SVf_UTF8;
2873 }
2874 else {
2875 0 SRL_RDR_ERROR_BAD_COPY(dec->pbuf, SRL_HDR_OBJECT);
2876 }
2877 0 ▶ if (expect_false(from > copytag_start
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(from > copytag_start 0 0 0
if (expect_false(from > copytag_start 0 0 0
if (expect_false(from > copytag_start 0 0 0
2878 || (UV)(copytag_start - from) < key_len_uv)) {
2879 0 SRL_RDR_ERRORf2(dec->pbuf,
2880 COPY_OFFSET_ERROR_FMT_PREFIX " while reading classname", (UV)ofs, key_len_uv);
2881 }
2882 0 ▶ if ((flags & SVf_UTF8) && key_len > 0 && !is_utf8_string(from, key_len))
Branch: 3 branches
BranchTrueFalseTotal
if ((flags & SVf_UTF8) && key_len > 0 && !is_utf8_string(from, key_len)) 0 0 0
if ((flags & SVf_UTF8) && key_len > 0 && !is_utf8_string(from, key_len)) 0 0 0
if ((flags & SVf_UTF8) && key_len > 0 && !is_utf8_string(from, key_len)) 0 0 0
2883 0 SRL_RDR_ERROR_PANIC(dec->pbuf, "utf8 flagged classname is not actually utf8");
2884 }
2885 } else {
2886 0 SRL_RDR_ERROR_UNEXPECTED(dec->pbuf, tag, "a class name");
2887 }
2888  
2889 /* At this point we may or may not have a class stash. If they used a Copy there
2890 * is a decent chance we do. */
2891 4 ▶ SRL_ASSERT_REF_PTR_TABLES(dec);
Branch: 1 branch
BranchTrueFalseTotal
SRL_ASSERT_REF_PTR_TABLES(dec); 2 2 4
2892 /* A COPY of this OBJECT tag re-reads an inline class name at the same
2893 * offset; reuse its registration so objects queued earlier against it
2894 * are still blessed. */
2895 4 ▶ if (!class_entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!class_entry) 4 0 4
2896 4 class_entry= (SV *) PTABLE_fetch(dec->ref_stashes, (void *)storepos);
2897 4 ▶ if (!class_entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!class_entry) { 4 0 4
2898 /* no class stash - so we need to look it up and then store it away
2899 * for future use. Only a frozen object needs the package now; for
2900 * the others it is created when they are blessed. */
2901 4 ▶ class_entry= (SV *) gv_stashpvn((char *)from, key_len,
Branch: 1 branch
BranchTrueFalseTotal
class_entry= (SV *) gv_stashpvn((char *)from, key_len, 4 0 4
2902 obj_tag == SRL_HDR_OBJECT_FREEZE ? flags : flags & ~GV_ADD);
2903 4 ▶ if (!class_entry)
Branch: 1 branch
BranchTrueFalseTotal
if (!class_entry) 2 2 4
2904 2 class_entry= sv_2mortal(newSVpvn_flags((char *)from, key_len, flags & SVf_UTF8));
2905 4 PTABLE_store(dec->ref_stashes, (void *)storepos, (void *)class_entry);
2906 /* Since this is the first time we have seen this stash then it is the first time
2907 * that we have stored an item in the ref_bless_av hash as well. So create a new one
2908 * and store it away. */
2909 4 av= newAV();
2910 4 sv_2mortal((SV*)av);
2911 4 PTABLE_store(dec->ref_bless_av, (void *)storepos, (void *)av);
2912 } else {
2913 /* we have a class stash so we should have a ref_bless_av as well. */
2914 0 av= (AV *)PTABLE_fetch(dec->ref_bless_av, (void *)storepos);
2915 0 ▶ if ( !av )
Branch: 1 branch
BranchTrueFalseTotal
if ( !av ) 0 0 0
2916 0 SRL_RDR_ERRORf1(dec->pbuf, "Panic, no ref_bless_av for %"UVuf, (UV)storepos);
2917 }
2918  
2919 #ifdef FOLLOW_REFERENCES_IF_NOT_STASHED
2920 /* at this point we have class name read and have coressponding records in
2921 * dec->dec->ref_stashes and dec->ref_bless_av. So, we can simply fetch
2922 * from hashes outside this function. The code */
2923 if (read_class_name_only) return;
2924 #else
2925 assert(into != NULL);
2926 assert(obj_tag != 0);
2927 #endif
2928  
2929 4 srl_track_class(aTHX_ dec, class_entry);
2930 4 SRL_DEC_SET_OPTION(dec, SRL_F_DECODER_NEEDS_FINALIZE);
2931 4 ▶ if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( obj_tag == SRL_HDR_OBJECT_FREEZE )) { 0 4 4
2932 0 srl_read_frozen_object(aTHX_ dec, srl_class_stash(aTHX_ dec, storepos, class_entry), into);
2933 } else {
2934  
2935 /* We now have a stash so we /could/ bless... except that
2936 * we don't actually want to do so right now. We want to defer blessing
2937 * until the full packet has been read. Yes it is more overhead, but
2938 * we really dont want to trigger DESTROY methods from a partial
2939 * deparse. So we insert the item into an array to be blessed later. */
2940 4 av_push(av, SvREFCNT_inc(into));
2941  
2942 /* now deparse the thing we are going to bless */
2943 4 srl_read_single_value(aTHX_ dec, into, NULL);
2944 4 ▶ if (expect_false(!SvROK(into)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!SvROK(into))) 0 4 4
2945 0 SRL_RDR_ERROR(dec->pbuf, "OBJECT(V) must be followed by a reference");
2946 4 ▶ if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) &&
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) && 0 4 4
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) && 0 0 0
if (expect_false(SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_REFUSE_OBJECTS) && 0 4 4
2947 !srl_is_canonical_regexp_object(aTHX_ into, class_entry)))
2948 0 SRL_RDR_ERROR_REFUSE_OBJECT(dec->pbuf);
2949 4 srl_refuse_blessing_alias(aTHX_ dec, SvRV(into));
2950  
2951 #if USE_588_WORKAROUND
2952 /* See 'define USE_588_WORKAROUND' above for a discussion of what this does. */
2953 if (!SRL_DEC_HAVE_OPTION(dec, SRL_F_DECODER_NO_BLESS_OBJECTS)) {
2954 HV *stash = srl_class_stash(aTHX_ dec, storepos, class_entry);
2955 if (!srl_class_allowed(aTHX_ dec->allow_hash, dec->allow_cb, stash))
2956 SRL_RDR_ERRORf1(dec->pbuf, "Object class '%s' is not in allow_classes", HvNAME(stash));
2957 sv_bless(into, stash);
2958 }
2959 #endif
2960 }
2961 4 }
2962  
2963  
2964 SRL_STATIC_INLINE void
2965 0 srl_read_frozen_object(pTHX_ srl_decoder_t *dec, HV *class_stash, SV *into)
2966 {
2967  
2968 0 ▶ if (!dec->thaw_av)
Branch: 1 branch
BranchTrueFalseTotal
if (!dec->thaw_av) 0 0 0
2969 0 ▶ SAFE_NEW_AV(dec->thaw_av);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_NEW_AV(dec->thaw_av); 0 0 0
2970  
2971 /* We do this BEFORE we call srl_read_single_value() so that
2972 thaw_av contains the items in order of us seeing them in the serialized
2973 dump. We will pop them off the list later on when we do the actual THAW.
2974 Note that at the time we do this we haven't "filled out" the 'into' var
2975 with the actual AV that is used for its frozen form. */
2976  
2977 0 AV_PUSH(dec->thaw_av, into);
2978  
2979 /* now fill out into with the AV that represents the object */
2980 0 srl_read_single_value(aTHX_ dec, into, NULL);
2981  
2982 /* validate that we actually deparsed an AV */
2983 0 ▶ if (expect_false(!SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false(!SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV)) 0 0 0
if (expect_false(!SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV)) 0 0 0
if (expect_false(!SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV)) 0 0 0
2984 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. OBJECT(V)_FREEZE must be followed by an array reference");
2985  
2986 0 ▶ if (!dec->ref_thawhash)
Branch: 1 branch
BranchTrueFalseTotal
if (!dec->ref_thawhash) 0 0 0
2987 0 ▶ SAFE_PTABLE_NEW(dec->ref_thawhash);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_PTABLE_NEW(dec->ref_thawhash); 0 0 0
2988  
2989 /* REFP may reuse a frozen object, but a second frozen declaration must
2990 * not overwrite the class/fixup record for the same argument array. */
2991 0 ▶ if (expect_false(PTABLE_find(dec->ref_thawhash, (void *)SvRV(into))))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(PTABLE_find(dec->ref_thawhash, (void *)SvRV(into)))) 0 0 0
2992 0 SRL_RDR_ERROR(dec->pbuf, "Frozen argument array is already assigned to an object");
2993  
2994 /* we need to do this *after* we have called srl_read_single_value() as
2995 we use the address of the AV that into references as the key to find the
2996 class stash. */
2997 0 PTABLE_store(dec->ref_thawhash, (void *)SvRV(into), (void *)class_stash);
2998 0 }
2999  
3000 /* Invoke a THAW callback on the given class. Pass in the next item in the
3001 * decoder stream. This is implementing the FREEZE/THAW part of
3002 * SRL_HDR_OBJECT_FREEZE and SRL_HDR_OBJECTV_FREEZE. */
3003 SRL_STATIC_INLINE SV *
3004 0 srl_thaw_object(pTHX_ srl_decoder_t *dec, HV *class_stash, SV *into)
3005 {
3006 0 GV *method = gv_fetchmethod_autoload(class_stash, "THAW", 0);
3007 0 ▶ char *classname = HvNAME(class_stash);
Branch: 6 branches
BranchTrueFalseTotal
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
char *classname = HvNAME(class_stash); 0 0 0
3008 0 SV *replacement = NULL;
3009 AV *arg_av;
3010  
3011 0 ▶ if (expect_false( method == NULL ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( method == NULL )) 0 0 0
3012 0 SRL_RDR_ERRORf1(dec->pbuf, "No THAW method defined for class '%s'", classname);
3013  
3014 /* Assert that we got a top level array ref as the spec requires.
3015 * Not throwing an exception here violates expectations down the line and
3016 * can lead to segfaults. */
3017 0 ▶ if (expect_false( !SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV ))
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( !SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV )) 0 0 0
if (expect_false( !SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV )) 0 0 0
if (expect_false( !SvROK(into) || SvTYPE(SvRV(into)) != SVt_PVAV )) 0 0 0
3018 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. OBJECT(V)_FREEZE used without "
3019 "being followed by an array reference");
3020  
3021 0 arg_av= (AV*)SvRV(into);
3022 {
3023 int count;
3024 0 SSize_t arg_av_len = av_len(arg_av);
3025 0 dSP;
3026  
3027 /* Leave room for the class and serializer arguments before either
3028 * adding to the array index or passing a count to EXTEND. */
3029 0 ▶ if (expect_false(arg_av_len > I32_MAX - 3))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(arg_av_len > I32_MAX - 3)) 0 0 0
3030 0 SRL_RDR_ERROR(dec->pbuf, "Too many THAW arguments for the Perl stack");
3031 0 ++arg_av_len;
3032  
3033 0 ENTER;
3034 0 SAVETMPS;
3035 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
3036  
3037 0 ▶ EXTEND(SP, 2+arg_av_len);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2+arg_av_len); 0 0 0
EXTEND(SP, 2+arg_av_len); 0 0 0
3038 /* TODO Consider more caching for some of this */
3039 0 PUSHs(sv_2mortal(newSVpvn(classname, strlen(classname))));
3040 /* FIXME do not recreate the following SV. That's dumb and wasteful! -
3041 * So long as it doesnt get modified! */
3042 0 PUSHs(sv_2mortal(newSVpvs("Sereal")));
3043 /* Push the args into the stack */
3044 0 ▶ for (count=0 ; count < arg_av_len; count++) {
Branch: 1 branch
BranchTrueFalseTotal
for (count=0 ; count < arg_av_len; count++) { 0 0 0
3045 /* An earlier THAW callback may have deleted elements through a
3046 * shared reference to this argument array. Perl passes holes as
3047 * undef when expanding an array into a function call. */
3048 0 SV **arg = av_fetch(arg_av, count, 0);
3049 0 ▶ PUSHs(arg ? *arg : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(arg ? *arg : &PL_sv_undef); 0 0 0
3050 }
3051  
3052 0 PUTBACK;
3053 0 count = call_sv((SV *)GvCV(method), G_SCALAR);
3054 /* TODO explore method lookup caching */
3055 0 SPAGAIN;
3056  
3057 0 ▶ if (expect_true( count == 1 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( count == 1 )) { 0 0 0
3058 0 replacement = POPs;
3059 0 SvREFCNT_inc(replacement);
3060 }
3061 /* If count is not 1, then it's 0. Then into is already undef. */
3062  
3063 0 PUTBACK;
3064 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
3065 0 LEAVE;
3066 }
3067 0 ▶ return sv_2mortal(replacement ? replacement : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
return sv_2mortal(replacement ? replacement : newSV(0)); 0 0 0
3068 }
3069  
3070  
3071 static SRL_NOINLINE SRL_UNUSED void
3072 0 srl_read_reserved(pTHX_ srl_decoder_t *dec, U8 tag, SV* into)
3073 {
3074 0 const UV len = srl_read_varint_uv_length(aTHX_ dec->pbuf, " while reading reserved");
3075 (void)tag; /* unused as of now */
3076  
3077 0 dec->buf.pos += len; /* discard */
3078 0 sv_setsv(into, &PL_sv_undef);
3079 0 }
3080  
3081  
3082 static SRL_NOINLINE void
3083 230 srl_read_regexp(pTHX_ srl_decoder_t *dec, SV* into)
3084 {
3085 230 SV *sv_pat= sv_2mortal(FRESH_SV());
3086 U8 pat_tag;
3087 /* The pattern must be a string. Given a reference to an existing
3088 * regexp, CALLREGCOMP returns that regexp itself. A COPY may only
3089 * point at a string, which is checked after reading it. */
3090 230 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading regexp pattern tag");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading regexp pattern tag"); 0 230 230
3091 230 pat_tag= *dec->buf.pos & ~SRL_HDR_TRACK_FLAG;
3092 230 ▶ if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY
Branch: 5 branches
BranchTrueFalseTotal
if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY 0 230 230
if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY 0 0 0
if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY 0 0 0
if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY 0 0 0
if (expect_false( !IS_SRL_HDR_SHORT_BINARY(pat_tag) && pat_tag != SRL_HDR_BINARY 0 230 230
3093 && pat_tag != SRL_HDR_STR_UTF8 && pat_tag != SRL_HDR_COPY ))
3094 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. REGEXP pattern must be a string");
3095 230 srl_read_single_value(aTHX_ dec, sv_pat, NULL);
3096 230 ▶ if (expect_false( SvROK(sv_pat) || SvTYPE(sv_pat) > SVt_PVMG || !SvPOK(sv_pat) ))
Branch: 4 branches
BranchTrueFalseTotal
if (expect_false( SvROK(sv_pat) || SvTYPE(sv_pat) > SVt_PVMG || !SvPOK(sv_pat) )) 230 0 230
if (expect_false( SvROK(sv_pat) || SvTYPE(sv_pat) > SVt_PVMG || !SvPOK(sv_pat) )) 230 0 230
if (expect_false( SvROK(sv_pat) || SvTYPE(sv_pat) > SVt_PVMG || !SvPOK(sv_pat) )) 0 230 230
if (expect_false( SvROK(sv_pat) || SvTYPE(sv_pat) > SVt_PVMG || !SvPOK(sv_pat) )) 0 230 230
3097 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. REGEXP pattern must be a string");
3098 230 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading regexp modifer tag");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading regexp modifer tag"); 0 230 230
3099 /* For now we will serialize the flags as ascii strings. Maybe we should use
3100 * something else but this is easy to debug and understand - since the modifiers
3101 * are tagged it doesn't matter much, we can add other tags later */
3102 230 ▶ if ( expect_true( IS_SRL_HDR_SHORT_BINARY(*dec->buf.pos) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( expect_true( IS_SRL_HDR_SHORT_BINARY(*dec->buf.pos) ) ) { 230 0 230
3103 230 U8 mod_len= SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(*dec->buf.pos++);
3104 230 U32 flags= 0;
3105 230 U8 seen_x = 0;
3106 230 U8 a_count = 0;
3107 230 U8 charset = 0;
3108 230 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, mod_len, " while reading regexp modifiers");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, mod_len, " while reading regexp modifiers"); 0 230 230
3109 458 ▶ while (mod_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (mod_len > 0) { 228 230 458
3110 228 mod_len--;
3111 228 switch (*dec->buf.pos++) {
3112 0 case 'm':
3113 0 flags= flags | PMf_MULTILINE;
3114 0 break;
3115 0 case 's':
3116 0 flags= flags | PMf_SINGLELINE;
3117 0 break;
3118 228 case 'i':
3119 228 flags= flags | PMf_FOLD;
3120 228 break;
3121 0 case 'x':
3122 0 ▶ if (seen_x) {
Branch: 1 branch
BranchTrueFalseTotal
if (seen_x) { 0 0 0
3123 /* The bit was reserved in 5.21, but /xx was only
3124 * implemented in 5.25.9 (stable 5.26). */
3125 #if PERL_VERSION > 25 || (PERL_VERSION == 25 && PERL_SUBVERSION >= 9)
3126 0 flags |= PMf_EXTENDED_MORE;
3127 #else
3128 SRL_RDR_ERROR(dec->pbuf, "Regexp modifier '/xx' requires Perl 5.26 or later");
3129 #endif
3130 }
3131 0 seen_x = 1;
3132 0 flags= flags | PMf_EXTENDED;
3133 0 break;
3134 0 case 'n':
3135 #ifdef PMf_NOCAPTURE
3136 0 flags |= PMf_NOCAPTURE;
3137 #else
3138 SRL_RDR_ERROR(dec->pbuf, "Regexp modifier '/n' requires Perl 5.22 or later");
3139 #endif
3140 0 break;
3141 0 case 'p':
3142 #ifdef REGEXP_HAS_P_MODIFIER
3143 0 flags = flags | PMf_KEEPCOPY;
3144 #else
3145 SRL_RDR_ERROR(dec->pbuf, "Regexp modifier '/p' requires Perl 5.10 or later");
3146 #endif
3147 0 break;
3148 0 case 'd':
3149 case 'l':
3150 case 'u':
3151 case 'a': {
3152 0 U8 modifier = dec->buf.pos[-1];
3153 0 ▶ if (charset && charset != modifier)
Branch: 2 branches
BranchTrueFalseTotal
if (charset && charset != modifier) 0 0 0
if (charset && charset != modifier) 0 0 0
3154 0 SRL_RDR_ERROR(dec->pbuf, "Conflicting regexp character set modifiers");
3155 0 ▶ if (charset == modifier && modifier != 'a')
Branch: 2 branches
BranchTrueFalseTotal
if (charset == modifier && modifier != 'a') 0 0 0
if (charset == modifier && modifier != 'a') 0 0 0
3156 0 SRL_RDR_ERRORf1(dec->pbuf, "Repeated regexp character set modifier '/%c'", modifier);
3157 0 charset = modifier;
3158 0 ▶ if (modifier == 'a' && ++a_count > 2)
Branch: 2 branches
BranchTrueFalseTotal
if (modifier == 'a' && ++a_count > 2) 0 0 0
if (modifier == 'a' && ++a_count > 2) 0 0 0
3159 0 SRL_RDR_ERROR(dec->pbuf, "Invalid regexp modifier '/aaa'");
3160 #if PERL_VERSION >= 14 || (PERL_VERSION == 13 && PERL_SUBVERSION >= 10)
3161 0 switch (modifier) {
3162 0 case 'd': set_regex_charset(&flags, REGEX_DEPENDS_CHARSET); break;
3163 0 case 'l': set_regex_charset(&flags, REGEX_LOCALE_CHARSET); break;
3164 0 case 'u': set_regex_charset(&flags, REGEX_UNICODE_CHARSET); break;
3165 0 case 'a':
3166 0 ▶ set_regex_charset(&flags, a_count == 2
Branch: 1 branch
BranchTrueFalseTotal
set_regex_charset(&flags, a_count == 2 0 0 0
3167 ? REGEX_ASCII_MORE_RESTRICTED_CHARSET
3168 : REGEX_ASCII_RESTRICTED_CHARSET);
3169 0 break;
3170 }
3171 #else
3172 SRL_RDR_ERRORf1(dec->pbuf, "Regexp modifier '/%c' requires Perl 5.14 or later", modifier);
3173 #endif
3174 0 break;
3175 }
3176 0 default:
3177 0 SRL_RDR_ERRORf1(dec->pbuf, "Unsupported regexp modifier byte 0x%02x", (unsigned int)dec->buf.pos[-1]);
3178 break;
3179 }
3180 }
3181 #ifdef MODERN_REGEXP
3182 {
3183 SV *referent;
3184  
3185 /* compile the regex */
3186 230 referent= (SV*)CALLREGCOMP(sv_pat, flags);
3187 230 ▶ if (!referent)
Branch: 1 branch
BranchTrueFalseTotal
if (!referent) 0 230 230
3188 0 SRL_RDR_ERROR(dec->pbuf, "bad regexp pattern");
3189  
3190 /* Store a copy, as qr// and ${qr//} do: perl only hands out
3191 * copies of a compiled regexp, and relies on that when one is
3192 * assigned to or freed. sv_setsv() makes the copy. */
3193 230 sv_setsv(into, referent);
3194 230 SvREFCNT_dec(referent);
3195 }
3196 #elif defined( TRANSITION_REGEXP )
3197 {
3198 REGEXP *referent = CALLREGCOMP(aTHX_ sv_pat, flags);
3199 sv_magic( into, (SV*)referent, PERL_MAGIC_qr, 0, 0);
3200 SvFLAGS( into ) |= SVs_SMG;
3201 }
3202 #else
3203 {
3204 PMOP pm; /* grr */
3205 STRLEN pat_len;
3206 REGEXP *re;
3207 char *pat= SvPV(sv_pat, pat_len);
3208  
3209 Zero(&pm,1,PMOP);
3210 pm.op_pmdynflags= SvUTF8(sv_pat) ? PMdf_CMP_UTF8 : 0;
3211 pm.op_pmflags= flags;
3212  
3213 re= CALLREGCOMP(aTHX_ pat, pat + pat_len, &pm);
3214 sv_magic( into, (SV*)re, PERL_MAGIC_qr, 0, 0);
3215 SvFLAGS( into ) |= SVs_SMG;
3216 }
3217 #endif
3218 }
3219 else {
3220 0 SRL_RDR_ERROR(dec->pbuf, "Expecting SRL_HDR_SHORT_BINARY for modifiers of regexp");
3221 }
3222 230 }
3223  
3224 /* Returns true for UNDEF_DEADLY so the caller can use the ordinary UNDEF
3225 * path, including use_undef aliasing and the original tag's tracking bit. */
3226 static SRL_NOINLINE int
3227 0 srl_read_extend(pTHX_ srl_decoder_t *dec, SV* into)
3228 {
3229 U8 ext_tag;
3230 0 ▶ if (expect_false(dec->proto_version < 6))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 6)) 0 0 0
3231 0 SRL_RDR_ERROR(dec->pbuf, "EXTEND tag requires protocol version 6 or later");
3232 0 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading extended tag ID");
Branch: 1 branch
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, 1, " while reading extended tag ID"); 0 0 0
3233 0 ext_tag = *dec->buf.pos++;
3234 0 ▶ if (ext_tag == SRL_EXT_DECLARE) {
Branch: 1 branch
BranchTrueFalseTotal
if (ext_tag == SRL_EXT_DECLARE) { 0 0 0
3235 0 ▶ if (dec->proto_version < 7)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->proto_version < 7) 0 0 0
3236 0 SRL_RDR_ERROR(dec->pbuf, "DECLARE tag requires protocol version 7 or later");
3237 0 SRL_RDR_ERROR(dec->pbuf, "Declaration outside document prefix");
3238 }
3239 0 ▶ if (ext_tag == SRL_EXT_UNDEF_DEADLY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ext_tag == SRL_EXT_UNDEF_DEADLY) { 0 0 0
3240 0 ▶ if (dec->undef_deadly_policy == SRL_UNDEF_DEADLY_DIE)
Branch: 1 branch
BranchTrueFalseTotal
if (dec->undef_deadly_policy == SRL_UNDEF_DEADLY_DIE) 0 0 0
3241 0 SRL_RDR_ERROR(dec->pbuf, "Encountered UNDEF_DEADLY extended tag");
3242 0 ▶ if (dec->undef_deadly_policy == SRL_UNDEF_DEADLY_WARN) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec->undef_deadly_policy == SRL_UNDEF_DEADLY_WARN) { 0 0 0
3243 0 warn("Encountered UNDEF_DEADLY extended tag");
3244 0 srl_assert_input_unchanged(aTHX_ dec);
3245 }
3246 0 return 1;
3247 }
3248 0 ▶ else if (ext_tag == SRL_EXT_VSTRING_BINARY)
Branch: 1 branch
BranchTrueFalseTotal
else if (ext_tag == SRL_EXT_VSTRING_BINARY) 0 0 0
3249 0 srl_read_vstring(aTHX_ dec, into, 0);
3250 0 ▶ else if (ext_tag == SRL_EXT_VSTRING_UTF8)
Branch: 1 branch
BranchTrueFalseTotal
else if (ext_tag == SRL_EXT_VSTRING_UTF8) 0 0 0
3251 0 srl_read_vstring(aTHX_ dec, into, 1);
3252 else
3253 0 SRL_RDR_ERRORf1(dec->pbuf, "Unknown extended tag ID %u", (unsigned int)ext_tag);
3254 0 return 0;
3255 }
3256  
3257 static SRL_NOINLINE void
3258 0 srl_read_copy(pTHX_ srl_decoder_t *dec, SV* into)
3259 {
3260 0 const U8 *copytag_start = dec->buf.pos - 1;
3261 0 UV ofs= srl_read_backref_offset(aTHX_ dec, " while reading COPY tag");
3262 0 const U8 *target_start = dec->buf.body_pos + ofs;
3263 0 const U8 *packet_end = dec->buf.end;
3264 0 const UV owed_bytes = dec->owed_bytes;
3265  
3266 0 ▶ if (expect_false( dec->save_pos )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( dec->save_pos )) { 0 0 0
3267 0 SRL_RDR_ERRORf2(dec->pbuf, "nested COPY at offset %d and %d",
3268 (int)(dec->save_pos - dec->buf.body_pos), (int)ofs );
3269 }
3270 0 ▶ if (expect_false( target_start >= copytag_start )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( target_start >= copytag_start )) { 0 0 0
3271 0 SRL_RDR_ERRORf1(dec->pbuf, "COPY target %d must precede its own tag", (int)ofs);
3272 }
3273 0 dec->save_pos= dec->buf.pos;
3274 0 dec->buf.pos= target_start;
3275 /* Bound the entire copied value, including lengths and nested values,
3276 * to bytes preceding COPY. Checking only its start is insufficient. */
3277 0 dec->buf.end= copytag_start;
3278 /* What enclosing containers owe lies after the COPY tag. */
3279 0 dec->owed_bytes= 0;
3280 0 srl_read_single_value(aTHX_ dec, into, NULL);
3281 0 dec->owed_bytes= owed_bytes;
3282 0 dec->buf.end= packet_end;
3283 0 dec->buf.pos= dec->save_pos;
3284 0 dec->save_pos= 0;
3285 0 }
3286  
3287  
3288  
3289 /****************************************************************************
3290 * MAIN DISPATCH SUB - ALL ROADS LEAD HERE *
3291 ****************************************************************************/
3292  
3293 void
3294 0 srl_decode_single_value(pTHX_ srl_decoder_t *dec, SV* into, SV** container)
3295 {
3296 0 srl_read_single_value(aTHX_ dec, into, container);
3297 0 }
3298  
3299 SRL_STATIC_INLINE void
3300 511 srl_read_single_value(pTHX_ srl_decoder_t *dec, SV* into, SV** container)
3301 {
3302 511 srl_read_single_value_ex(aTHX_ dec, into, container, 0);
3303 511 }
3304  
3305 /* is_refn_referent is set only by srl_read_refn(). An ARRAY or HASH tag
3306 * describes the aggregate itself, which in Perl can only exist behind a
3307 * reference; anywhere else it would turn the destination scalar into an
3308 * AV/HV body in place. */
3309 SRL_STATIC_INLINE void
3310 519 srl_read_single_value_ex(pTHX_ srl_decoder_t *dec, SV* into, SV** container, int is_refn_referent)
3311 {
3312 STRLEN len;
3313 U8 tag;
3314 519 int is_ref = 0;
3315 519 const U8 *track_it = NULL;
3316  
3317 /* Count every recursive value parse here. Counting only reference
3318 * container tags misses plain ARRAY/HASH, object payloads, WEAKEN, and
3319 * other tags that recurse through srl_read_single_value(). A crafted
3320 * packet could otherwise exhaust the C stack without reaching the
3321 * configured recursion limit. */
3322 519 ▶ DEPTH_INCREMENT(dec);
Branch: 1 branch
BranchTrueFalseTotal
DEPTH_INCREMENT(dec); 519 0 519
3323  
3324 519 read_again:
3325 519 ▶ if (expect_false( SRL_RDR_DONE(dec->pbuf) ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SRL_RDR_DONE(dec->pbuf) )) 0 519 519
3326 0 SRL_RDR_ERROR(dec->pbuf, "unexpected end of input stream while expecting a single value");
3327  
3328 519 tag= *dec->buf.pos++;
3329  
3330 0 read_tag:
3331 519 switch (tag) {
3332 14 CASE_SRL_HDR_POS:
3333 14 srl_setiv(aTHX_ dec, into, container, track_it, (IV)tag);
3334 14 break;
3335 0 CASE_SRL_HDR_NEG:
3336 0 srl_setiv(aTHX_ dec, into, container, track_it, (IV)(tag - 32));
3337 0 break;
3338 240 CASE_SRL_HDR_SHORT_BINARY:
3339 240 len= (STRLEN)SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag);
3340 240 ▶ SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 240 240
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 0 0
SRL_CHECK_MAX_STRING_LENGTH(dec, len, "string"); 0 240 240
3341 240 ▶ SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading ascii string");
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading ascii string"); 240 0 240
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading ascii string"); 0 240 240
SRL_RDR_ASSERT_SPACE(dec->pbuf, len, " while reading ascii string"); 0 240 240
3342 240 sv_setpvn(into,(char*)dec->buf.pos,len);
3343 240 dec->buf.pos += len;
3344 240 break;
3345 15 CASE_SRL_HDR_HASHREF: srl_read_hash(aTHX_ dec, into, tag); is_ref = 1; break;
3346 6 CASE_SRL_HDR_ARRAYREF: srl_read_array(aTHX_ dec, into, tag); is_ref = 1; break;
3347 0 case SRL_HDR_VARINT: srl_read_varint_into(aTHX_ dec, into, container, track_it); break;
3348 0 case SRL_HDR_ZIGZAG: srl_read_zigzag_into(aTHX_ dec, into, container, track_it); break;
3349 0 case SRL_HDR_NEG_VARINT:
3350 0 ▶ if (expect_false(dec->proto_version < 6))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 6)) 0 0 0
3351 0 SRL_RDR_ERROR(dec->pbuf, "NEG_VARINT tag requires protocol version 6");
3352 0 srl_read_neg_varint_into(aTHX_ dec, into, container, track_it);
3353 0 break;
3354  
3355 0 case SRL_HDR_FLOAT: srl_read_float(aTHX_ dec, into); break;
3356  
3357 0 case SRL_HDR_FLOAT_128:
3358 0 ▶ if (expect_false(dec->proto_version < 5))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 5)) 0 0 0
3359 0 SRL_RDR_ERROR(dec->pbuf, "FLOAT_128 tag requires protocol version 5 or later");
3360 0 srl_read_float_128(aTHX_ dec, into);
3361 0 break;
3362  
3363 0 case SRL_HDR_DOUBLE: srl_read_double(aTHX_ dec, into); break;
3364  
3365 0 case SRL_HDR_LONG_DOUBLE: srl_read_long_double(aTHX_ dec, into); break;
3366  
3367 0 case SRL_HDR_NO:
3368 0 ▶ if (expect_false(dec->proto_version < 5))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 5)) 0 0 0
3369 0 SRL_RDR_ERROR(dec->pbuf, "NO tag requires protocol version 5 or later");
3370 0 sv_setsv(into, &PL_sv_no);
3371 0 break;
3372 0 case SRL_HDR_FALSE: sv_setsv(into, &PL_sv_no); break;
3373  
3374 0 case SRL_HDR_YES:
3375 0 ▶ if (expect_false(dec->proto_version < 5))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 5)) 0 0 0
3376 0 SRL_RDR_ERROR(dec->pbuf, "YES tag requires protocol version 5 or later");
3377 0 sv_setsv(into, &PL_sv_yes);
3378 0 break;
3379 0 case SRL_HDR_TRUE: sv_setsv(into, &PL_sv_yes); break;
3380  
3381 0 case SRL_HDR_CANONICAL_UNDEF:
3382 0 ▶ if (expect_false(dec->proto_version < 3))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 3)) 0 0 0
3383 0 SRL_RDR_ERROR(dec->pbuf, "CANONICAL_UNDEF tag requires protocol version 3 or later");
3384 /* fall through */
3385 case SRL_HDR_UNDEF:
3386 {
3387 0 ▶ if (container && SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF)){
Branch: 2 branches
BranchTrueFalseTotal
if (container && SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF)){ 0 0 0
if (container && SRL_DEC_HAVE_OPTION(dec,SRL_F_DECODER_USE_UNDEF)){ 0 0 0
3388 0 SvREFCNT_dec(into);
3389 0 *container= &PL_sv_undef;
3390 0 ▶ if ( track_it )
Branch: 1 branch
BranchTrueFalseTotal
if ( track_it ) 0 0 0
3391 0 srl_track_sv(aTHX_ dec, track_it, *container);
3392 } else {
3393 0 sv_setsv(into, &PL_sv_undef);
3394 }
3395 }
3396 0 break;
3397  
3398 2 case SRL_HDR_BINARY: srl_read_string(aTHX_ dec, 0, into); break;
3399 0 case SRL_HDR_STR_UTF8: srl_read_string(aTHX_ dec, 1, into); break;
3400  
3401 0 case SRL_HDR_WEAKEN: srl_read_weaken(aTHX_ dec, into); is_ref=1; break;
3402 8 case SRL_HDR_REFN: srl_read_refn(aTHX_ dec, into); is_ref=1; break;
3403 0 case SRL_HDR_REFP: srl_read_refp(aTHX_ dec, into); is_ref=1; break;
3404 0 case SRL_HDR_OBJECT_FREEZE:
3405 0 ▶ if (expect_false(dec->proto_version < 2))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 2)) 0 0 0
3406 0 SRL_RDR_ERROR(dec->pbuf, "FREEZE object tags require protocol version 2 or later");
3407 0 srl_read_object(aTHX_ dec, into, tag, 0); is_ref=1; break;
3408 4 case SRL_HDR_OBJECT: srl_read_object(aTHX_ dec, into, tag, 0); is_ref=1; break;
3409 0 case SRL_HDR_OBJECTV_FREEZE:
3410 0 ▶ if (expect_false(dec->proto_version < 2))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(dec->proto_version < 2)) 0 0 0
3411 0 SRL_RDR_ERROR(dec->pbuf, "FREEZE object tags require protocol version 2 or later");
3412 0 srl_read_objectv(aTHX_ dec, into, tag); is_ref=1; break;
3413 0 case SRL_HDR_OBJECTV: srl_read_objectv(aTHX_ dec, into, tag); is_ref=1; break;
3414 0 case SRL_HDR_COPY: srl_read_copy(aTHX_ dec, into); is_ref=SvROK(into) ? 1 : 0; break;
3415 0 case SRL_HDR_EXTEND:
3416 0 ▶ if (srl_read_extend(aTHX_ dec, into)) {
Branch: 1 branch
BranchTrueFalseTotal
if (srl_read_extend(aTHX_ dec, into)) { 0 0 0
3417 0 tag = SRL_HDR_UNDEF;
3418 0 goto read_tag;
3419 }
3420 0 break;
3421 0 case SRL_HDR_HASH:
3422 0 ▶ if (expect_false(!is_refn_referent))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!is_refn_referent)) 0 0 0
3423 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. HASH tag is only valid as the referent of a REFN");
3424 0 srl_read_hash(aTHX_ dec, into, 0);
3425 0 break;
3426 0 case SRL_HDR_ARRAY:
3427 0 ▶ if (expect_false(!is_refn_referent))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(!is_refn_referent)) 0 0 0
3428 0 SRL_RDR_ERROR(dec->pbuf, "Corrupted packet. ARRAY tag is only valid as the referent of a REFN");
3429 0 srl_read_array(aTHX_ dec, into, 0);
3430 0 break;
3431 230 case SRL_HDR_REGEXP: srl_read_regexp(aTHX_ dec, into); break;
3432 0 case SRL_HDR_ALIAS:
3433 {
3434 UV offset;
3435 SV *alias;
3436 0 ▶ if (!container)
Branch: 1 branch
BranchTrueFalseTotal
if (!container) 0 0 0
3437 0 SRL_RDR_ERROR(dec->pbuf, "ALIAS tag not inside container, corrupt packet?");
3438 0 offset= srl_read_backref_offset(aTHX_ dec," while reading ALIAS tag");
3439 0 alias= srl_fetch_item(aTHX_ dec, offset, "ALIAS");
3440 #ifdef FOLLOW_REFERENCES_IF_NOT_STASHED
3441 if (!alias) alias= srl_follow_refp_alias_reference(aTHX_ dec, offset, 0);
3442 #endif
3443 /* A tracked ARRAY/HASH tag tracks the aggregate itself, which
3444 * cannot be stored as an element or value; encoders only
3445 * emit ALIAS for scalars. */
3446 0 ▶ if (expect_false( SvTYPE(alias) >= SVt_PVAV ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( SvTYPE(alias) >= SVt_PVAV )) 0 0 0
3447 0 SRL_RDR_ERRORf1(dec->pbuf, "Corrupted packet. ALIAS tag refers to a %s, not a scalar",
3448 sv_reftype(alias, 0));
3449 0 SvREFCNT_inc(alias);
3450 0 SvREFCNT_dec(into);
3451 0 *container= alias;
3452 0 ▶ if (track_it)
Branch: 1 branch
BranchTrueFalseTotal
if (track_it) 0 0 0
3453 0 srl_track_sv(aTHX_ dec, track_it, alias);
3454 0 DEPTH_DECREMENT(dec);
3455 0 return;
3456 }
3457 break;
3458 0 case SRL_HDR_PAD: /* no op */
3459 0 ▶ while (SRL_RDR_NOT_DONE(dec->pbuf) && *dec->buf.pos == SRL_HDR_PAD)
Branch: 2 branches
BranchTrueFalseTotal
while (SRL_RDR_NOT_DONE(dec->pbuf) && *dec->buf.pos == SRL_HDR_PAD) 0 0 0
while (SRL_RDR_NOT_DONE(dec->pbuf) && *dec->buf.pos == SRL_HDR_PAD) 0 0 0
3460 0 dec->buf.pos++;
3461 0 goto read_again;
3462 break;
3463 0 default:
3464 0 ▶ if (tag & SRL_HDR_TRACK_FLAG) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag & SRL_HDR_TRACK_FLAG) { 0 0 0
3465 0 tag= tag & ~SRL_HDR_TRACK_FLAG;
3466 0 ▶ if (!track_it)
Branch: 1 branch
BranchTrueFalseTotal
if (!track_it) 0 0 0
3467 0 track_it = dec->buf.pos-1;
3468 0 srl_track_sv(aTHX_ dec, dec->buf.pos-1, into);
3469 0 goto read_tag;
3470 } else {
3471 0 SRL_RDR_ERROR_UNEXPECTED(dec->pbuf, tag, " single value");
3472 }
3473 break;
3474 }
3475  
3476 /* UNDEF and integer aliasing may have replaced and freed the original
3477 * destination. Apply readonly flags to the value now in the container. */
3478 519 ▶ if (container)
Branch: 1 branch
BranchTrueFalseTotal
if (container) 25 494 519
3479 25 into = *container;
3480  
3481 /* they want us to set all SVs readonly, or only the non-ref */
3482 #define SUPPORT_READONLY 1
3483 #if SUPPORT_READONLY
3484 519 ▶ if ( expect_false(dec->flags_readonly) )
Branch: 1 branch
BranchTrueFalseTotal
if ( expect_false(dec->flags_readonly) ) 44 475 519
3485 {
3486 44 if (
3487 44 ▶ dec->flags_readonly == 1 || !is_ref
Branch: 2 branches
BranchTrueFalseTotal
dec->flags_readonly == 1 || !is_ref 22 22 44
dec->flags_readonly == 1 || !is_ref 16 6 22
3488 ) {
3489 #if PERL_VERSION < 16
3490 /* Perl 5.14 mistakes FAKE + READONLY regexp copies for shared
3491 * hash-key strings during cleanup. A readonly regexp cannot
3492 * be assigned to, so it does not need FAKE's coercion hook. */
3493 if (is_ref && SvTYPE(SvRV(into)) == SVt_REGEXP)
3494 SvFAKE_off(SvRV(into));
3495 if (SvTYPE(into) == SVt_REGEXP)
3496 SvFAKE_off(into);
3497 #endif
3498 38 ▶ if (is_ref && !SvREADONLY(SvRV(into)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ref && !SvREADONLY(SvRV(into)) ) { 6 32 38
if (is_ref && !SvREADONLY(SvRV(into)) ) { 4 2 6
3499 4 SvREADONLY_on(SvRV(into));
3500 }
3501 38 ▶ if (!SvREADONLY(into)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvREADONLY(into)) { 38 0 38
3502 38 SvREADONLY_on(into);
3503 }
3504 }
3505 }
3506 #endif
3507  
3508  
3509 519 DEPTH_DECREMENT(dec);
3510 519 return;
3511 }
« srl_checksum_policy.h ↑ Summary srl_common.h »