683572
From 3a019afd6f6291c3249c254b5c01e244e4ec83ab Mon Sep 17 00:00:00 2001
683572
From: Karl Williamson <khw@cpan.org>
683572
Date: Sun, 28 Apr 2019 17:42:44 -0600
683572
Subject: [PATCH 1/3] Create fcn for lossless conversion of NV to IV
683572
MIME-Version: 1.0
683572
Content-Type: text/plain; charset=UTF-8
683572
Content-Transfer-Encoding: 8bit
683572
683572
Essentially the same code was being used in three places, and had
683572
undefined C behavior for some inputs.
683572
683572
This consolidates the code into one inline function, and rewrites it to
683572
avoid undefined behavior.
683572
683572
Signed-off-by: Petr Písař <ppisar@redhat.com>
683572
---
683572
 embed.fnc |  1 +
683572
 embed.h   |  3 +++
683572
 inline.h  | 34 ++++++++++++++++++++++++++++++++++
683572
 pp.c      | 20 ++++----------------
683572
 pp_hot.c  | 10 ++--------
683572
 proto.h   |  7 +++++++
683572
 6 files changed, 51 insertions(+), 24 deletions(-)
683572
683572
diff --git a/embed.fnc b/embed.fnc
683572
index 45597f67b6..259affded0 100644
683572
--- a/embed.fnc
683572
+++ b/embed.fnc
683572
@@ -2272,6 +2272,7 @@ sR	|SV*	|refto		|NN SV* sv
683572
 : Used in pp_hot.c
683572
 pRxo	|GV*	|softref2xv	|NN SV *const sv|NN const char *const what \
683572
 				|const svtype type|NN SV ***spp
683572
+inR	|bool	|lossless_NV_to_IV|const NV nv|NN IV * ivp
683572
 #endif
683572
 
683572
 #if defined(PERL_IN_PP_PACK_C)
683572
diff --git a/embed.h b/embed.h
683572
index 75c91f77f4..9178c51e92 100644
683572
--- a/embed.h
683572
+++ b/embed.h
683572
@@ -1924,6 +1924,9 @@
683572
 #define do_delete_local()	S_do_delete_local(aTHX)
683572
 #define refto(a)		S_refto(aTHX_ a)
683572
 #  endif
683572
+#  if defined(PERL_IN_PP_C) || defined(PERL_IN_PP_HOT_C)
683572
+#define lossless_NV_to_IV	S_lossless_NV_to_IV
683572
+#  endif
683572
 #  if defined(PERL_IN_PP_CTL_C)
683572
 #define check_type_and_open(a)	S_check_type_and_open(aTHX_ a)
683572
 #define destroy_matcher(a)	S_destroy_matcher(aTHX_ a)
683572
diff --git a/inline.h b/inline.h
683572
index 654f801b75..de1e33e8ce 100644
683572
--- a/inline.h
683572
+++ b/inline.h
683572
@@ -1913,6 +1913,40 @@ S_should_warn_nl(const char *pv) {
683572
 
683572
 #endif
683572
 
683572
+#if defined(PERL_IN_PP_C) || defined(PERL_IN_PP_HOT_C)
683572
+
683572
+PERL_STATIC_INLINE bool
683572
+S_lossless_NV_to_IV(const NV nv, IV *ivp)
683572
+{
683572
+    /* This function determines if the input NV 'nv' may be converted without
683572
+     * loss of data to an IV.  If not, it returns FALSE taking no other action.
683572
+     * But if it is possible, it does the conversion, returning TRUE, and
683572
+     * storing the converted result in '*ivp' */
683572
+
683572
+    PERL_ARGS_ASSERT_LOSSLESS_NV_TO_IV;
683572
+
683572
+#  if  defined(Perl_isnan)
683572
+
683572
+    if (UNLIKELY(Perl_isnan(nv))) {
683572
+        return FALSE;
683572
+    }
683572
+
683572
+#  endif
683572
+
683572
+    if (UNLIKELY(nv < IV_MIN) || UNLIKELY(nv > IV_MAX)) {
683572
+        return FALSE;
683572
+    }
683572
+
683572
+    if ((IV) nv != nv) {
683572
+        return FALSE;
683572
+    }
683572
+
683572
+    *ivp = (IV) nv;
683572
+    return TRUE;
683572
+}
683572
+
683572
+#endif
683572
+
683572
 /* ------------------ pp.c, regcomp.c, toke.c, universal.c ------------ */
683572
 
683572
 #define MAX_CHARSET_NAME_LENGTH 2
683572
diff --git a/pp.c b/pp.c
683572
index c89cb7198c..0956121b27 100644
683572
--- a/pp.c
683572
+++ b/pp.c
683572
@@ -1268,16 +1268,10 @@ PP(pp_multiply)
683572
             NV nr = SvNVX(svr);
683572
             NV result;
683572
 
683572
-            if (
683572
-#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
683572
-                !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
683572
-                && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
683572
-#else
683572
-                nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
683572
-#endif
683572
-                )
683572
+            if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
683572
                 /* nothing was lost by converting to IVs */
683572
                 goto do_iv;
683572
+            }
683572
             SP--;
683572
             result = nl * nr;
683572
 #  if defined(__sgi) && defined(USE_LONG_DOUBLE) && LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE && NVSIZE == 16
683572
@@ -1849,16 +1843,10 @@ PP(pp_subtract)
683572
             NV nl = SvNVX(svl);
683572
             NV nr = SvNVX(svr);
683572
 
683572
-            if (
683572
-#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
683572
-                !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
683572
-                && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
683572
-#else
683572
-                nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
683572
-#endif
683572
-                )
683572
+            if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
683572
                 /* nothing was lost by converting to IVs */
683572
                 goto do_iv;
683572
+            }
683572
             SP--;
683572
             TARGn(nl - nr, 0); /* args not GMG, so can't be tainted */
683572
             SETs(TARG);
683572
diff --git a/pp_hot.c b/pp_hot.c
683572
index 7d5ffc02fd..2df5df8303 100644
683572
--- a/pp_hot.c
683572
+++ b/pp_hot.c
683572
@@ -1435,16 +1435,10 @@ PP(pp_add)
683572
             NV nl = SvNVX(svl);
683572
             NV nr = SvNVX(svr);
683572
 
683572
-            if (
683572
-#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
683572
-                !Perl_isnan(nl) && nl == (NV)(il = (IV)nl)
683572
-                && !Perl_isnan(nr) && nr == (NV)(ir = (IV)nr)
683572
-#else
683572
-                nl == (NV)(il = (IV)nl) && nr == (NV)(ir = (IV)nr)
683572
-#endif
683572
-                )
683572
+            if (lossless_NV_to_IV(nl, &il) && lossless_NV_to_IV(nr, &ir)) {
683572
                 /* nothing was lost by converting to IVs */
683572
                 goto do_iv;
683572
+            }
683572
             SP--;
683572
             TARGn(nl + nr, 0); /* args not GMG, so can't be tainted */
683572
             SETs(TARG);
683572
diff --git a/proto.h b/proto.h
683572
index 0f8feed187..74a8e46ab7 100644
683572
--- a/proto.h
683572
+++ b/proto.h
683572
@@ -5224,6 +5224,13 @@ STATIC SV*	S_refto(pTHX_ SV* sv)
683572
 
683572
 #endif
683572
 #if defined(PERL_IN_PP_C) || defined(PERL_IN_PP_HOT_C)
683572
+#ifndef PERL_NO_INLINE_FUNCTIONS
683572
+PERL_STATIC_INLINE bool	S_lossless_NV_to_IV(const NV nv, IV * ivp)
683572
+			__attribute__warn_unused_result__;
683572
+#define PERL_ARGS_ASSERT_LOSSLESS_NV_TO_IV	\
683572
+	assert(ivp)
683572
+#endif
683572
+
683572
 PERL_CALLCONV GV*	Perl_softref2xv(pTHX_ SV *const sv, const char *const what, const svtype type, SV ***spp)
683572
 			__attribute__warn_unused_result__;
683572
 #define PERL_ARGS_ASSERT_SOFTREF2XV	\
683572
-- 
683572
2.20.1
683572