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

Storable: use utf8_to_bytes_overwrite() if available #22956

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions dist/Storable/Storable.xs
Original file line number Diff line number Diff line change
Expand Up @@ -2960,6 +2960,19 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
keyval = SvPV(key, keylen_tmp);
keylen = keylen_tmp;
if (SvUTF8(key)) {

#ifdef utf8_to_bytes_overwrite

/* If we are able to downgrade here; that means that we have a
* key which only had chars 0-255, but was utf8 encoded. */
if (utf8_to_bytes_overwrite( (U8**) &keyval, &keylen_tmp)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit worried about this, since this modifies the PV belonging to a SV, but the SV is a copy of the key from the hash on this branch, so it's safe.

It does make a UTF-8 SV with a validly encoded PV into a UTF-8 SV with a probably invalidly encoded PV, but that SV is almost immediately discarded.

Will the new utf8_to_bytes_* functions be added to ppport.h?

keylen = keylen_tmp;
flags |= SHV_K_WASUTF8;
}
else {
flags |= SHV_K_UTF8;
}
#else
const char *keysave = keyval;
bool is_utf8 = TRUE;

Expand All @@ -2982,6 +2995,7 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
to assign back to keylen. */
flags |= SHV_K_UTF8;
}
#endif
}

if (flagged_hash) {
Expand All @@ -3000,8 +3014,12 @@ static int store_hash(pTHX_ stcxt_t *cxt, HV *hv)
WLEN(keylen);
if (keylen)
WRITE(keyval, keylen);

#ifndef utf8_to_bytes_overwrite

if (flags & SHV_K_WASUTF8)
Safefree (keyval);
#endif
}

/*
Expand Down
2 changes: 1 addition & 1 deletion dist/Storable/lib/Storable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ our @EXPORT_OK = qw(
our ($canonical, $forgive_me);

BEGIN {
our $VERSION = '3.35';
our $VERSION = '3.36';
}

our $recursion_limit;
Expand Down
Loading