Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do_chomp: Use bytes_to_utf8_free_me #22906

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
s = SvPV(sv, len);
if (chomping) {
if (s && len) {
void *temp_buffer = NULL;
void *free_me = NULL;
s += --len;
if (RsPARA(PL_rs)) {
if (*s != '\n')
Expand All @@ -818,7 +818,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
if (SvUTF8(PL_rs)) {
/* RS is utf8, scalar is 8 bit. */
if (! utf8_to_bytes_new_pv((const U8 **) &rsptr, &rslen,
&temp_buffer))
&free_me))
{
/* Cannot downgrade, therefore cannot possibly
* match. */
Expand All @@ -827,8 +827,9 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
}
else {
/* RS is 8 bit, scalar is utf8. */
temp_buffer = bytes_to_utf8((U8*)rsptr, &rslen);
rsptr = (char *) temp_buffer;
rsptr = (char *) bytes_to_utf8_free_me((U8*) rsptr,
&rslen,
&free_me);
}
}
if (rslen == 1) {
Expand All @@ -853,7 +854,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
SvSETMAGIC(sv);

nope_free_all:
Safefree(temp_buffer);
Safefree(free_me);
nope_free_nothing: ;
}
} else {
Expand Down
23 changes: 20 additions & 3 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -2865,9 +2865,24 @@ automatically freed, via a call to C<L</SAVEFREEPV>>.

For C<utf8_to_bytes_new_pv>, C<*free_me> has been set to C<*s_ptr>, and it is
the caller's responsibility to free the new memory when done using it.
The results of this parameter can simply be passed to C<L</Safefree>> when
done, as that handles a C<NULL> parameter, and/or it can be used as a boolean
(non-NULL meaning C<true>) to indicate that the input was indeed changed.
The following paradigm is convenient to use for this:

void * free_me;
if (utf8_to_bytes_new_pv(&s, &len, &free_me) {
...
}
else {
...
}

...

Safefree(free_me);

C<free_me> can be used as a boolean (non-NULL meaning C<true>) to indicate that
the input was indeed changed if you need to revisit that later in the code.
Your design is likely flawed if you find yourself using C<free_me> for any
other purpose.

=back

Expand Down Expand Up @@ -3279,6 +3294,8 @@ allows the following convenient paradigm:
You don't have to know if memory was allocated or not. Just call C<Safefree>
unconditionally. C<free_me> will contain a suitable value to pass to
C<Safefree> for it to do the right thing, regardless.
Your design is likely flawed if you find yourself using C<free_me> for anything
other than passing to C<Safefree>.

Upon return, the number of variants in the string can be computed by having
saved the value of C<*lenp> before the call, and subtracting the after-call
Expand Down
Loading