srl_decoder.c

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