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