ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.xs
(Generate patch)

Comparing CBOR-XS/XS.xs (file contents):
Revision 1.76 by root, Thu Sep 7 23:57:58 2023 UTC vs.
Revision 1.77 by root, Fri Sep 8 05:47:10 2023 UTC

1196 if (ecb_expect_false (!dec->shareable)) 1196 if (ecb_expect_false (!dec->shareable))
1197 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ()); 1197 dec->shareable = (AV *)sv_2mortal ((SV *)newAV ());
1198 1198
1199 if (dec->cbor.flags & F_ALLOW_CYCLES) 1199 if (dec->cbor.flags & F_ALLOW_CYCLES)
1200 { 1200 {
1201 // if cycles are allowed, then we store an AV as value
1202 // while it is being decoded, and gather unresolved
1203 // references in it, to be re4solved after decoding.
1204 int idx, i;
1201 sv = newSV (0); 1205 AV *av = newAV ();
1202 av_push (dec->shareable, SvREFCNT_inc_NN (sv)); 1206 av_push (dec->shareable, SvREFCNT_inc_NN ((SV *)av));
1207 idx = AvFILLp (dec->shareable);
1203 1208
1204 SV *osv = decode_sv (dec); 1209 sv = decode_sv (dec);
1205 sv_setsv (sv, osv); 1210
1211 // the AV now contains \undef for all unresolved references,
1212 // so we fix them up here.
1213 for (i = 0; i <= AvFILLp (av); ++i)
1214 SvRV_set (AvARRAY (av)[i], SvREFCNT_inc_NN (SvRV (sv)));
1215
1216 // now replace the AV by a reference to the completed value
1206 SvREFCNT_dec_NN (osv); 1217 SvREFCNT_dec_NN (av);
1218 AvARRAY (dec->shareable)[idx] = SvREFCNT_inc_NN (sv);
1207 } 1219 }
1208 else 1220 else
1209 { 1221 {
1210 av_push (dec->shareable, &PL_sv_undef); 1222 av_push (dec->shareable, &PL_sv_undef);
1211 int idx = AvFILLp (dec->shareable); 1223 int idx = AvFILLp (dec->shareable);
1223 UV idx = decode_uint (dec); 1235 UV idx = decode_uint (dec);
1224 1236
1225 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable))) 1237 if (!dec->shareable || idx >= (UV)(1 + AvFILLp (dec->shareable)))
1226 ERR ("corrupted CBOR data (sharedref index out of bounds)"); 1238 ERR ("corrupted CBOR data (sharedref index out of bounds)");
1227 1239
1228 sv = newSVsv (AvARRAY (dec->shareable)[idx]); 1240 sv = AvARRAY (dec->shareable)[idx];
1229 1241
1230 if (sv == &PL_sv_undef) 1242 // reference to cycle, we create a new \undef and use that, and also
1243 // registerr it in the AV for later fixing
1244 if (SvTYPE (sv) == SVt_PVAV)
1245 {
1246 AV *av = (AV *)sv;
1247 sv = newRV_noinc (&PL_sv_undef);
1248 av_push (av, sv);
1249 }
1250 else if (sv == &PL_sv_undef) // not yet decoded, but cycles not allowed
1231 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled"); 1251 ERR ("cyclic CBOR data structure found, but allow_cycles is not enabled");
1252 else // we decoded the object earlier, no cycle
1253 sv = newSVsv (sv);
1232 } 1254 }
1233 break; 1255 break;
1234 1256
1235 case CBOR_TAG_PERL_OBJECT: 1257 case CBOR_TAG_PERL_OBJECT:
1236 { 1258 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines