Skip to content

Commit 3ed3768

Browse files
committed
Fixes to utf8-to_bytes derivative functions
This commit turns them into inline functions instead of macros and changes the type of a parameter to void*, which is a more accurate type for it.
1 parent 9de1a49 commit 3ed3768

File tree

8 files changed

+75
-46
lines changed

8 files changed

+75
-46
lines changed

embed.fnc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3711,16 +3711,16 @@ Adpx |U8 * |utf8_to_bytes |NN U8 *s \
37113711
|NN STRLEN *lenp
37123712
Cp |bool |utf8_to_bytes_ |NN U8 **s_ptr \
37133713
|NN STRLEN *lenp \
3714-
|NN U8 **free_me \
3714+
|NN void **free_me \
37153715
|Perl_utf8_to_bytes_arg result_as
3716-
Admp |bool |utf8_to_bytes_new_pv \
3716+
Adip |bool |utf8_to_bytes_new_pv \
37173717
|NN U8 const **s_ptr \
37183718
|NN STRLEN *lenp \
3719-
|NN U8 *free_me
3720-
Admp |bool |utf8_to_bytes_overwrite \
3719+
|NN void **free_me
3720+
Adip |bool |utf8_to_bytes_overwrite \
37213721
|NN U8 **s_ptr \
37223722
|NN STRLEN *lenp
3723-
Admp |bool |utf8_to_bytes_temp_pv \
3723+
Adip |bool |utf8_to_bytes_temp_pv \
37243724
|NN U8 const **s_ptr \
37253725
|NN STRLEN *lenp
37263726
EMXp |U8 * |utf16_to_utf8 |NN U8 *p \

embed.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,9 @@
863863
# define utf8_length(a,b) Perl_utf8_length(aTHX_ a,b)
864864
# define utf8_to_bytes(a,b) Perl_utf8_to_bytes(aTHX_ a,b)
865865
# define utf8_to_bytes_(a,b,c,d) Perl_utf8_to_bytes_(aTHX_ a,b,c,d)
866-
# define utf8_to_bytes_new_pv(a,b,c) Perl_utf8_to_bytes_new_pv(aTHX,a,b,c)
867-
# define utf8_to_bytes_overwrite(a,b) Perl_utf8_to_bytes_overwrite(aTHX,a,b)
868-
# define utf8_to_bytes_temp_pv(a,b) Perl_utf8_to_bytes_temp_pv(aTHX,a,b)
866+
# define utf8_to_bytes_new_pv(a,b,c) Perl_utf8_to_bytes_new_pv(aTHX_ a,b,c)
867+
# define utf8_to_bytes_overwrite(a,b) Perl_utf8_to_bytes_overwrite(aTHX_ a,b)
868+
# define utf8_to_bytes_temp_pv(a,b) Perl_utf8_to_bytes_temp_pv(aTHX_ a,b)
869869
# define utf8_to_uv Perl_utf8_to_uv
870870
# define utf8_to_uv_errors Perl_utf8_to_uv_errors
871871
# define utf8_to_uv_flags Perl_utf8_to_uv_flags

hv.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,9 +1338,9 @@ S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
13381338

13391339
if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) {
13401340
const char * const keysave = key;
1341-
U8 * free_me = NULL;
1341+
void * free_me = NULL;
13421342

1343-
if (! utf8_to_bytes_new_pv(&key, &klen, &free_me)) {
1343+
if (! utf8_to_bytes_new_pv((const U8 **) &key, &klen, &free_me)) {
13441344
k_flags |= HVhek_UTF8;
13451345
}
13461346
else {
@@ -3270,8 +3270,8 @@ S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
32703270
} else if (len < 0) {
32713271
STRLEN tmplen = -len;
32723272
/* See the note in hv_fetch(). --jhi */
3273-
U8 * free_str = NULL;
3274-
if (! utf8_to_bytes_new_pv(&str, &tmplen, &free_str)) {
3273+
void * free_str = NULL;
3274+
if (! utf8_to_bytes_new_pv((const U8 **) &str, &tmplen, &free_str)) {
32753275
k_flags = HVhek_UTF8;
32763276
}
32773277
else {
@@ -3687,7 +3687,7 @@ Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
36873687
PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN;
36883688

36893689
U8 utf8_flag;
3690-
U8 * free_me = NULL;
3690+
void * free_me = NULL;
36913691

36923692
if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS))
36933693
Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %" UVxf,
@@ -3696,7 +3696,7 @@ Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
36963696
goto ret;
36973697
/* For searching purposes, canonicalise to Latin-1 where possible. */
36983698
if ( flags & REFCOUNTED_HE_KEY_UTF8
3699-
&& utf8_to_bytes_new_pv(&keypv, &keylen, &free_me))
3699+
&& utf8_to_bytes_new_pv((const U8 **) &keypv, &keylen, &free_me))
37003700
{
37013701
flags &= ~REFCOUNTED_HE_KEY_UTF8;
37023702
}
@@ -3821,7 +3821,7 @@ Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
38213821
char hekflags;
38223822
STRLEN key_offset = 1;
38233823
struct refcounted_he *he;
3824-
U8 * free_me = NULL;
3824+
void * free_me = NULL;
38253825

38263826
if (!value || value == &PL_sv_placeholder) {
38273827
value_type = HVrhek_delete;
@@ -3847,7 +3847,7 @@ Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
38473847

38483848
/* Canonicalise to Latin-1 where possible. */
38493849
if ( (flags & REFCOUNTED_HE_KEY_UTF8)
3850-
&& utf8_to_bytes_new_pv(&keypv, &keylen, &free_me))
3850+
&& utf8_to_bytes_new_pv((const U8 **) &keypv, &keylen, &free_me))
38513851
{
38523852
flags &= ~REFCOUNTED_HE_KEY_UTF8;
38533853
}

inline.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* inline.h
1+
/*> inline.h
22
*
33
* Copyright (C) 2012 by Larry Wall and others
44
*
@@ -1236,6 +1236,40 @@ Perl_bytes_to_utf8(pTHX_ const U8 *s, STRLEN *lenp)
12361236
return bytes_to_utf8_free_me(s, lenp, NULL);
12371237
}
12381238

1239+
PERL_STATIC_INLINE bool
1240+
Perl_utf8_to_bytes_new_pv(pTHX_ U8 const **s_ptr, STRLEN *lenp, void ** free_me)
1241+
{
1242+
/* utf8_to_bytes_() is declared to take a non-const s_ptr because it may
1243+
* change it, but NOT when called with PL_utf8_to_bytes_new_memory, so it
1244+
* is ok to cast away const */
1245+
return utf8_to_bytes_((U8 **) s_ptr, lenp, free_me,
1246+
PL_utf8_to_bytes_new_memory);
1247+
}
1248+
1249+
PERL_STATIC_INLINE bool
1250+
Perl_utf8_to_bytes_temp_pv(pTHX_ U8 const **s_ptr, STRLEN *lenp)
1251+
{
1252+
/* utf8_to_bytes_() requires a non-NULL pointer, but doesn't use it when
1253+
* called with PL_utf8_to_bytes_use_temporary */
1254+
void* dummy = NULL;
1255+
1256+
/* utf8_to_bytes_() is declared to take a non-const s_ptr because it may
1257+
* change it, but NOT when called with PL_utf8_to_bytes_use_temporary, so
1258+
* it is ok to cast away const */
1259+
return utf8_to_bytes_((U8 **) s_ptr, lenp, &dummy,
1260+
PL_utf8_to_bytes_use_temporary);
1261+
}
1262+
1263+
PERL_STATIC_INLINE bool
1264+
Perl_utf8_to_bytes_overwrite(pTHX_ U8 **s_ptr, STRLEN *lenp)
1265+
{
1266+
/* utf8_to_bytes_() requires a non-NULL pointer, but doesn't use it when
1267+
* called with PL_utf8_to_bytes_overwrite */
1268+
void* dummy = NULL;
1269+
1270+
return utf8_to_bytes_(s_ptr, lenp, &dummy, PL_utf8_to_bytes_overwrite);
1271+
}
1272+
12391273
/*
12401274
=for apidoc valid_utf8_to_uvchr
12411275
Like C<L<perlapi/utf8_to_uvchr_buf>>, but should only be called when it is

pp.c

Lines changed: 6 additions & 4 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-
U8 *temp_buffer = NULL;
796+
void *temp_buffer = NULL;
797797
s += --len;
798798
if (RsPARA(PL_rs)) {
799799
if (*s != '\n')
@@ -817,7 +817,7 @@ S_do_chomp(pTHX_ SV *retval, SV *sv, bool chomping)
817817
/* Assumption is that rs is shorter than the scalar. */
818818
if (SvUTF8(PL_rs)) {
819819
/* RS is utf8, scalar is 8 bit. */
820-
if (! utf8_to_bytes_new_pv(&rsptr, &rslen,
820+
if (! utf8_to_bytes_new_pv((const U8 **) &rsptr, &rslen,
821821
&temp_buffer))
822822
{
823823
/* Cannot downgrade, therefore cannot possibly
@@ -3911,8 +3911,10 @@ PP(pp_index)
39113911
if (little_utf8) {
39123912
/* Well, maybe instead we might be able to downgrade the small
39133913
string? */
3914-
U8 * free_little_p = NULL;
3915-
if (utf8_to_bytes_new_pv(&little_p, &llen, &free_little_p)) {
3914+
void * free_little_p = NULL;
3915+
if (utf8_to_bytes_new_pv((const U8 **) &little_p, &llen,
3916+
&free_little_p))
3917+
{
39163918
little_utf8 = false;
39173919

39183920
/* Here 'little_p' is in byte form, and 'free_little_p' is

proto.h

Lines changed: 16 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utf8.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2940,7 +2940,7 @@ New code should use the first three functions listed above.
29402940
*/
29412941

29422942
bool
2943-
Perl_utf8_to_bytes_(pTHX_ U8 **s_ptr, STRLEN *lenp, U8 ** free_me,
2943+
Perl_utf8_to_bytes_(pTHX_ U8 **s_ptr, STRLEN *lenp, void ** free_me,
29442944
Perl_utf8_to_bytes_arg result_as)
29452945
{
29462946
PERL_ARGS_ASSERT_UTF8_TO_BYTES_;
@@ -3219,7 +3219,7 @@ Perl_bytes_from_utf8(pTHX_ const U8 *s, STRLEN *lenp, bool *is_utf8p)
32193219
PERL_ARGS_ASSERT_BYTES_FROM_UTF8;
32203220

32213221
if (*is_utf8p) {
3222-
U8 * new_memory = NULL;
3222+
void * new_memory = NULL;
32233223
if (utf8_to_bytes_new_pv(&s, lenp, &new_memory)) {
32243224
*is_utf8p = false;
32253225

utf8.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,19 +1336,6 @@ typedef enum {
13361336
PL_utf8_to_bytes_use_temporary,
13371337
} Perl_utf8_to_bytes_arg;
13381338

1339-
/* INT2PTR() is because this parameter should not be used in this case, but
1340-
* there is a NN assertion for it. It causes that to pass but to still
1341-
* segfault if wrongly gets used */
1342-
#define Perl_utf8_to_bytes_overwrite(mTHX, s, l) \
1343-
Perl_utf8_to_bytes_(aTHX_ s, l, INT2PTR(U8 **, 1), \
1344-
PL_utf8_to_bytes_overwrite)
1345-
#define Perl_utf8_to_bytes_new_pv(mTHX, s, l, f) \
1346-
Perl_utf8_to_bytes_(aTHX_ (U8 **) s, l, f, \
1347-
PL_utf8_to_bytes_new_memory)
1348-
#define Perl_utf8_to_bytes_temp_pv(mTHX, s, l) \
1349-
Perl_utf8_to_bytes_(aTHX_ (U8 **) s, l, INT2PTR(U8 **, 1), \
1350-
PL_utf8_to_bytes_use_temporary)
1351-
13521339
/* Do not use; should be deprecated. Use isUTF8_CHAR() instead; this is
13531340
* retained solely for backwards compatibility */
13541341
#define IS_UTF8_CHAR(p, n) (isUTF8_CHAR(p, (p) + (n)) == n)

0 commit comments

Comments
 (0)