| 1 |
|
| /* vi: set ft=c : */ |
| 2 |
|
| |
| 3 |
|
| |
| 4 |
481 |
| static void walk_ops_find_labels(pTHX_ OP *o, HV *gotolabels) |
| 5 |
|
| { |
| 6 |
481 |
▶ | switch(o->op_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(o->op_type) { |
53 |
428 |
481 |
|
| 7 |
53 |
| case OP_NEXTSTATE: |
| 8 |
|
| case OP_DBSTATE: |
| 9 |
|
| { |
| 10 |
|
| STRLEN label_len; |
| 11 |
|
| U32 label_flags; |
| 12 |
53 |
| const char *label_pv = CopLABEL_len_flags((COP *)o, &label_len, &label_flags); |
| 13 |
53 |
▶ | if(!label_pv) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!label_pv) |
53 |
0 |
53 |
|
| 14 |
53 |
| break; |
| 15 |
|
| |
| 16 |
0 |
| SV *labelsv = newSVpvn_flags(label_pv, label_len, label_flags); |
| 17 |
0 |
| SAVEFREESV(labelsv); |
| 18 |
|
| |
| 19 |
0 |
| sv_inc(HeVAL(hv_fetch_ent(gotolabels, labelsv, TRUE, 0))); |
| 20 |
0 |
| break; |
| 21 |
|
| } |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
481 |
▶ | if(!(o->op_flags & OPf_KIDS)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(o->op_flags & OPf_KIDS)) |
272 |
209 |
481 |
|
| 25 |
272 |
| return; |
| 26 |
|
| |
| 27 |
209 |
| OP *kid = cUNOPo->op_first; |
| 28 |
660 |
▶ | while(kid) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(kid) { |
451 |
209 |
660 |
|
| 29 |
451 |
| walk_ops_find_labels(aTHX_ kid, gotolabels); |
| 30 |
451 |
▶ | kid = OpSIBLING(kid); |
Branch: 1 branch
| Branch | True | False | Total |
| kid = OpSIBLING(kid); |
242 |
209 |
451 |
|
| 31 |
|
| } |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
|
| enum { |
| 35 |
|
| FORBID_LOOPEX_DEFAULT = (1<<0), |
| 36 |
|
| }; |
| 37 |
|
| |
| 38 |
480 |
| static OPCODE walk_ops_forbid(pTHX_ OP *o, U32 flags, HV *permittedloops, HV *permittedgotos) |
| 39 |
|
| { |
| 40 |
480 |
| bool is_loop = FALSE; |
| 41 |
480 |
| SV *labelsv = NULL; |
| 42 |
|
| |
| 43 |
480 |
| switch(o->op_type) { |
| 44 |
53 |
| case OP_NEXTSTATE: |
| 45 |
|
| case OP_DBSTATE: |
| 46 |
53 |
| PL_curcop = (COP *)o; |
| 47 |
53 |
| return 0; |
| 48 |
|
| |
| 49 |
1 |
| case OP_RETURN: |
| 50 |
1 |
| goto forbid; |
| 51 |
|
| |
| 52 |
0 |
| case OP_GOTO: |
| 53 |
|
| { |
| 54 |
|
| /* OPf_STACKED means either dynamically computed label or `goto &sub` */ |
| 55 |
0 |
▶ | if(o->op_flags & OPf_STACKED) |
Branch: 1 branch
| Branch | True | False | Total |
| if(o->op_flags & OPf_STACKED) |
0 |
0 |
0 |
|
| 56 |
0 |
| goto forbid; |
| 57 |
|
| |
| 58 |
0 |
| SV *target = newSVpv(cPVOPo->op_pv, strlen(cPVOPo->op_pv)); |
| 59 |
0 |
▶ | if(cPVOPo->op_private & OPpPV_IS_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if(cPVOPo->op_private & OPpPV_IS_UTF8) |
0 |
0 |
0 |
|
| 60 |
0 |
| SvUTF8_on(target); |
| 61 |
0 |
| SAVEFREESV(target); |
| 62 |
|
| |
| 63 |
0 |
▶ | if(hv_fetch_ent(permittedgotos, target, FALSE, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(hv_fetch_ent(permittedgotos, target, FALSE, 0)) |
0 |
0 |
0 |
|
| 64 |
0 |
| break; |
| 65 |
|
| |
| 66 |
0 |
| goto forbid; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
0 |
| case OP_NEXT: |
| 70 |
|
| case OP_LAST: |
| 71 |
|
| case OP_REDO: |
| 72 |
|
| { |
| 73 |
|
| /* OPf_SPECIAL means this is a default loopex */ |
| 74 |
0 |
▶ | if(o->op_flags & OPf_SPECIAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(o->op_flags & OPf_SPECIAL) { |
0 |
0 |
0 |
|
| 75 |
0 |
▶ | if(flags & FORBID_LOOPEX_DEFAULT) |
Branch: 1 branch
| Branch | True | False | Total |
| if(flags & FORBID_LOOPEX_DEFAULT) |
0 |
0 |
0 |
|
| 76 |
0 |
| goto forbid; |
| 77 |
|
| |
| 78 |
0 |
| break; |
| 79 |
|
| } |
| 80 |
|
| /* OPf_STACKED means it's a dynamically computed label */ |
| 81 |
0 |
▶ | if(o->op_flags & OPf_STACKED) |
Branch: 1 branch
| Branch | True | False | Total |
| if(o->op_flags & OPf_STACKED) |
0 |
0 |
0 |
|
| 82 |
0 |
| goto forbid; |
| 83 |
|
| |
| 84 |
0 |
| SV *target = newSVpv(cPVOPo->op_pv, strlen(cPVOPo->op_pv)); |
| 85 |
0 |
▶ | if(cPVOPo->op_private & OPpPV_IS_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if(cPVOPo->op_private & OPpPV_IS_UTF8) |
0 |
0 |
0 |
|
| 86 |
0 |
| SvUTF8_on(target); |
| 87 |
0 |
| SAVEFREESV(target); |
| 88 |
|
| |
| 89 |
0 |
▶ | if(hv_fetch_ent(permittedloops, target, FALSE, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(hv_fetch_ent(permittedloops, target, FALSE, 0)) |
0 |
0 |
0 |
|
| 90 |
0 |
| break; |
| 91 |
|
| |
| 92 |
0 |
| goto forbid; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
0 |
| case OP_LEAVELOOP: |
| 96 |
|
| { |
| 97 |
|
| STRLEN label_len; |
| 98 |
|
| U32 label_flags; |
| 99 |
0 |
| const char *label_pv = CopLABEL_len_flags(PL_curcop, &label_len, &label_flags); |
| 100 |
|
| |
| 101 |
0 |
▶ | if(label_pv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(label_pv) { |
0 |
0 |
0 |
|
| 102 |
0 |
| labelsv = newSVpvn_flags(label_pv, label_len, label_flags); |
| 103 |
0 |
| SAVEFREESV(labelsv); |
| 104 |
|
| |
| 105 |
0 |
| sv_inc(HeVAL(hv_fetch_ent(permittedloops, labelsv, TRUE, 0))); |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
0 |
| is_loop = TRUE; |
| 109 |
0 |
| break; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
1 |
| forbid: |
| 113 |
1 |
| return o->op_type; |
| 114 |
|
| |
| 115 |
426 |
| default: |
| 116 |
426 |
| break; |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
426 |
▶ | if(!(o->op_flags & OPf_KIDS)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(o->op_flags & OPf_KIDS)) |
218 |
208 |
426 |
|
| 120 |
218 |
| return 0; |
| 121 |
|
| |
| 122 |
208 |
| OP *kid = cUNOPo->op_first; |
| 123 |
656 |
▶ | while(kid) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(kid) { |
450 |
206 |
656 |
|
| 124 |
450 |
| OPCODE ret = walk_ops_forbid(aTHX_ kid, flags, permittedloops, permittedgotos); |
| 125 |
450 |
▶ | if(ret) |
Branch: 1 branch
| Branch | True | False | Total |
| if(ret) |
2 |
448 |
450 |
|
| 126 |
2 |
| return ret; |
| 127 |
|
| |
| 128 |
448 |
▶ | kid = OpSIBLING(kid); |
Branch: 1 branch
| Branch | True | False | Total |
| kid = OpSIBLING(kid); |
242 |
206 |
448 |
|
| 129 |
|
| |
| 130 |
448 |
▶ | if(is_loop) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(is_loop) { |
0 |
448 |
448 |
|
| 131 |
|
| /* Now in the body of the loop; we can permit loopex default */ |
| 132 |
0 |
| flags &= ~FORBID_LOOPEX_DEFAULT; |
| 133 |
|
| } |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
206 |
▶ | if(is_loop && labelsv) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(is_loop && labelsv) { |
0 |
206 |
206 |
| if(is_loop && labelsv) { |
0 |
0 |
0 |
|
| 137 |
0 |
| HE *he = hv_fetch_ent(permittedloops, labelsv, FALSE, 0); |
| 138 |
0 |
▶ | if(SvIV(HeVAL(he)) > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvIV(HeVAL(he)) > 1) |
0 |
0 |
0 |
|
| 139 |
0 |
| sv_dec(HeVAL(he)); |
| 140 |
|
| else |
| 141 |
0 |
| hv_delete_ent(permittedloops, labelsv, 0, 0); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
206 |
| return 0; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| #ifndef forbid_outofblock_ops |
| 148 |
|
| # define forbid_outofblock_ops(o, blockname) S_forbid_outofblock_ops(aTHX_ o, blockname) |
| 149 |
|
| static void S_forbid_outofblock_ops(pTHX_ OP *o, const char *blockname) |
| 150 |
|
| { |
| 151 |
|
| ENTER; |
| 152 |
|
| SAVEVPTR(PL_curcop); |
| 153 |
|
| |
| 154 |
|
| HV *looplabels = newHV(); |
| 155 |
|
| SAVEFREESV((SV *)looplabels); |
| 156 |
|
| |
| 157 |
|
| HV *gotolabels = newHV(); |
| 158 |
|
| SAVEFREESV((SV *)gotolabels); |
| 159 |
|
| |
| 160 |
|
| walk_ops_find_labels(aTHX_ o, gotolabels); |
| 161 |
|
| |
| 162 |
|
| OPCODE forbidden = walk_ops_forbid(aTHX_ o, FORBID_LOOPEX_DEFAULT, looplabels, gotolabels); |
| 163 |
|
| if(forbidden) |
| 164 |
|
| croak("Can't \"%s\" out of %s", PL_op_name[forbidden], blockname); |
| 165 |
|
| |
| 166 |
|
| LEAVE; |
| 167 |
|
| } |
| 168 |
|
| #endif |
| 169 |
|
| |
| 170 |
|
| #ifndef warn_outofblock_ops |
| 171 |
|
| # define warn_outofblock_ops(o, fmt) S_warn_outofblock_ops(aTHX_ o, fmt) |
| 172 |
30 |
| static void S_warn_outofblock_ops(pTHX_ OP *o, const char *fmt) |
| 173 |
|
| { |
| 174 |
30 |
| ENTER; |
| 175 |
30 |
| SAVEVPTR(PL_curcop); |
| 176 |
|
| |
| 177 |
30 |
| HV *looplabels = newHV(); |
| 178 |
30 |
| SAVEFREESV((SV *)looplabels); |
| 179 |
|
| |
| 180 |
30 |
| HV *gotolabels = newHV(); |
| 181 |
30 |
| SAVEFREESV((SV *)gotolabels); |
| 182 |
|
| |
| 183 |
30 |
| walk_ops_find_labels(aTHX_ o, gotolabels); |
| 184 |
|
| |
| 185 |
30 |
| OPCODE forbidden = walk_ops_forbid(aTHX_ o, FORBID_LOOPEX_DEFAULT, looplabels, gotolabels); |
| 186 |
30 |
▶ | if(forbidden) |
Branch: 1 branch
| Branch | True | False | Total |
| if(forbidden) |
1 |
29 |
30 |
|
| 187 |
1 |
| warn(fmt, PL_op_name[forbidden]); |
| 188 |
|
| |
| 189 |
30 |
| LEAVE; |
| 190 |
30 |
| } |
| 191 |
|
| #endif |