Skip to content

Commit 3fce3a2

Browse files
committed
do_chomp: Use bytes_to_utf8_free_me
This won't allocate new memory unless it is needed.
1 parent 4c62bc0 commit 3fce3a2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pp.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
793793
s = SvPV(sv, len);
794794
if (chomping) {
795795
if (s && len) {
796-
void *temp_buffer = NULL;
796+
void *free_me = NULL;
797797
s += --len;
798798
if (RsPARA(PL_rs)) {
799799
if (*s != '\n')
@@ -818,7 +818,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
818818
if (SvUTF8(PL_rs)) {
819819
/* RS is utf8, scalar is 8 bit. */
820820
if (! utf8_to_bytes_new_pv((const U8 **) &rsptr, &rslen,
821-
&temp_buffer))
821+
&free_me))
822822
{
823823
/* Cannot downgrade, therefore cannot possibly
824824
* match. */
@@ -827,8 +827,9 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
827827
}
828828
else {
829829
/* RS is 8 bit, scalar is utf8. */
830-
temp_buffer = bytes_to_utf8((U8*)rsptr, &rslen);
831-
rsptr = (char *) temp_buffer;
830+
rsptr = (char *) bytes_to_utf8_free_me((U8*) rsptr,
831+
&rslen,
832+
&free_me);
832833
}
833834
}
834835
if (rslen == 1) {
@@ -853,7 +854,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
853854
SvSETMAGIC(sv);
854855

855856
nope_free_all:
856-
Safefree(temp_buffer);
857+
Safefree(free_me);
857858
nope_free_nothing: ;
858859
}
859860
} else {

utf8.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,6 +3279,8 @@ allows the following convenient paradigm:
32793279
You don't have to know if memory was allocated or not. Just call C<Safefree>
32803280
unconditionally. C<free_me> will contain a suitable value to pass to
32813281
C<Safefree> for it to do the right thing, regardless.
3282+
Your design is likely flawed if you find yourself using C<free_me> for anything
3283+
other than passing to C<Safefree>.
32823284
32833285
Upon return, the number of variants in the string can be computed by having
32843286
saved the value of C<*lenp> before the call, and subtracting the after-call

0 commit comments

Comments
 (0)