Skip to content

Commit 4539fb8

Browse files
committed
builtin::trim: ensure TARG is properly reset
refactor a lot of custom "set SV to a string code" away to sv_setpvn(), this: - fixed the original problem reported for #22784, where TARG wasn't being reset properly and contained a cached numeric version of the result from the previous call. - removed some never executed code, since builtin::trim is only XS and is not an OP with the TARGLEX optimization - fixes a possible problem if the result of the first call to trim() is COWed. This does slightly change the taint behaviour, rather than making TARG tainted iff source is tainted, it changes to the behaviour of the rest of perl, making TARG tainted if any tainted input is seen in the current expression. See thr PR #22788 for some discussion on how we got here. Fixes #22784
1 parent 342b57d commit 4539fb8

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

builtin.c

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ XS(XS_builtin_trim)
225225
SV *source = TOPs;
226226
STRLEN len;
227227
const U8 *start;
228-
SV *dest;
229228

230229
SvGETMAGIC(source);
231230

@@ -273,33 +272,14 @@ XS(XS_builtin_trim)
273272
}
274273
}
275274

276-
dest = TARG;
275+
sv_setpvn(TARG, (const char *)start, len);
277276

278-
if (SvPOK(dest) && (dest == source)) {
279-
sv_chop(dest, (const char *)start);
280-
SvCUR_set(dest, len);
281-
}
282-
else {
283-
SvUPGRADE(dest, SVt_PV);
284-
SvGROW(dest, len + 1);
285-
286-
Copy(start, SvPVX(dest), len, U8);
287-
SvPVX(dest)[len] = '\0';
288-
SvPOK_on(dest);
289-
SvCUR_set(dest, len);
290-
291-
if (DO_UTF8(source))
292-
SvUTF8_on(dest);
293-
else
294-
SvUTF8_off(dest);
295-
296-
if (SvTAINTED(source))
297-
SvTAINT(dest);
298-
}
299-
300-
SvSETMAGIC(dest);
277+
if (DO_UTF8(source))
278+
SvUTF8_on(TARG);
279+
else
280+
SvUTF8_off(TARG);
301281

302-
SETs(dest);
282+
SETTARG;
303283

304284
XSRETURN(1);
305285
}

lib/builtin.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package builtin 0.016;
1+
package builtin 0.017;
22

33
use v5.40;
44

lib/builtin.t

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,14 @@ EOS
678678
}
679679
}
680680

681+
# github #22784
682+
{
683+
use builtin qw( trim );
684+
sub f { 0+trim($_[0]) }
685+
is(f(4), 4, "populate TARG.iv");
686+
is(f(123), 123, "check TARG.IOK is reset properly");
687+
}
688+
681689
# vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4
682690

683691
done_testing();

0 commit comments

Comments
 (0)