summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTAKAI Kousuke <62541129+t-a-k@users.noreply.github.com>2022-06-15 01:55:36 +0900
committerTony Cook <tony@develop-help.com>2022-07-01 08:26:55 +1000
commitb52cb8576e7432bf7e962089e9e54451381968c8 (patch)
tree5bb0ac3ae070d861f51935ccb32ca5c06dc05afa
parent878d0f6b930a6f3a1e01768796d2f7a1b71f9da0 (diff)
downloadperl-b52cb8576e7432bf7e962089e9e54451381968c8.tar.gz
sv.c: Replace a runtime check with compile-time static assertion
The original code used to check U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX in runtime, but possibility of failing this check will imply that such perl configuration is unusable anyway. To make this check never fail, the first non-preserved integer, which is (1 << NV_PRESERVES_UV_BITS), is required to be less than (or equal to) IV_MAX, which can be checked at compile time without runtime overhead.
-rw-r--r--sv.c11
-rw-r--r--t/porting/diag.t1
2 files changed, 6 insertions, 6 deletions
diff --git a/sv.c b/sv.c
index 64ca25096b..7276030bf5 100644
--- a/sv.c
+++ b/sv.c
@@ -2187,11 +2187,12 @@ S_sv_2iuv_common(pTHX_ SV *const sv)
SvIV_set(sv, I_V(SvNVX(sv)));
if ((NV)(SvIVX(sv)) == SvNVX(sv))
SvIOK_on(sv);
- /* Assumption: first non-preserved integer is < IV_MAX,
- this NV is in the preserved range, therefore: */
- if (!(U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX)) {
- Perl_croak(aTHX_ "sv_2iv assumed (U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%" NVgf " U_V is 0x%" UVxf ", IV_MAX is 0x%" UVxf "\n", SvNVX(sv), U_V(Perl_fabs(SvNVX(sv))), (UV)IV_MAX);
- }
+ /* There had been runtime checking for
+ "U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX" here to ensure
+ that this NV is in the preserved range, but this should
+ be always true if the following assertion is true: */
+ STATIC_ASSERT_STMT(((UV)1 << NV_PRESERVES_UV_BITS) <=
+ (UV)IV_MAX);
} else {
/* IN_UV NOT_INT
0 0 already failed to read UV.
diff --git a/t/porting/diag.t b/t/porting/diag.t
index 80f7458562..7645a0f0cb 100644
--- a/t/porting/diag.t
+++ b/t/porting/diag.t
@@ -670,7 +670,6 @@ socketpair not implemented!
%s: %s
Starting Full Screen process with flag=%d, mytype=%d
Starting PM process with flag=%d, mytype=%d
-sv_2iv assumed (U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%f U_V is 0x%x, IV_MAX is 0x%x
switching effective gid is not implemented
switching effective uid is not implemented
System V IPC is not implemented on this machine