Skip to content

Commit

Permalink
builtin::trim: ensure TARG is properly reset
Browse files Browse the repository at this point in the history
In the reported test case, trim() sets TARG (used for the return
value), and that is then evaluated in numeric context, setting the IV
slot and pIOK and possible IOK.

These aren't reset on the second call to trim from the same entersub
OP, and since TARG is re-used it still has the IV slot and pIOK/IOK
set, and so uses the cached numeric value when evaluating TARG in
numeric context again.

Clear all but the string related flags.

Fixes Perl#22784
  • Loading branch information
tonycoz committed Nov 26, 2024
1 parent 44641fd commit 9c2a10f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ XS(XS_builtin_trim)

Copy(start, SvPVX(dest), len, U8);
SvPVX(dest)[len] = '\0';
SvPOK_on(dest);
SvPOK_only_UTF8(dest);
SvCUR_set(dest, len);

if (DO_UTF8(source))
Expand Down
2 changes: 1 addition & 1 deletion lib/builtin.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin 0.016;
package builtin 0.017;

use v5.40;

Expand Down
8 changes: 8 additions & 0 deletions lib/builtin.t
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,14 @@ EOS
}
}

# github #22784
{
use builtin qw( trim );
sub f { 0+trim($_[0]) }
is(f(4), 4, "populate TARG.iv");
is(f(123), 123, "check TARG.IOK is reset properly");
}

# vim: tabstop=4 shiftwidth=4 expandtab autoindent softtabstop=4

done_testing();

0 comments on commit 9c2a10f

Please sign in to comment.