about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2009-08-24 12:06:55 -0700
committerPetr Baudis <pasky@suse.cz>2009-09-18 17:48:09 +0200
commita4a88a24a91dcfdc5c8816404cfdb68572f5bf95 (patch)
treedf8ba153e43762905d7dd8e8a834fffd1dda68f2
parent68941973fa567cd79aedcdc90367d217107dd8a7 (diff)
downloadglibc-a4a88a24a91dcfdc5c8816404cfdb68572f5bf95.tar.gz
glibc-a4a88a24a91dcfdc5c8816404cfdb68572f5bf95.tar.xz
glibc-a4a88a24a91dcfdc5c8816404cfdb68572f5bf95.zip
Fix overflow handling in fdim.
(cherry picked from commit f0c281e072fd324261a51558284c04e230c0178d)
-rw-r--r--ChangeLog6
-rw-r--r--math/s_fdim.c12
-rw-r--r--math/s_fdimf.c12
-rw-r--r--math/s_fdiml.c16
4 files changed, 38 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index e1743e9b58..f5c24d4a7c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-08-24  Ulrich Drepper  <drepper@redhat.com>
+
+	* math/s_fdim.c: In case of overflows set errno.
+	* math/s_fdimf.c: Likewise.
+	* math/s_fdiml.c: Likewise.
+
 2009-08-23  Ulrich Drepper  <drepper@redhat.com>
 
 	* posix/regcomp.c (parse_dup_op): Verify the expression is correctly
diff --git a/math/s_fdim.c b/math/s_fdim.c
index 5804e631c3..677fdcde1a 100644
--- a/math/s_fdim.c
+++ b/math/s_fdim.c
@@ -1,5 +1,5 @@
 /* Return positive difference between arguments.
-   Copyright (C) 1997, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1997, 2004, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
@@ -18,6 +18,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <errno.h>
 #include <math.h>
 
 double
@@ -31,7 +32,14 @@ __fdim (double x, double y)
     /* Raise invalid flag.  */
     return x - y;
 
-  return x <= y ? 0 : x - y;
+  if (x <= y)
+    return 0.0;
+
+  double r = x - y;
+  if (fpclassify (r) == FP_INFINITE)
+    __set_errno (ERANGE);
+
+  return r;
 }
 weak_alias (__fdim, fdim)
 #ifdef NO_LONG_DOUBLE
diff --git a/math/s_fdimf.c b/math/s_fdimf.c
index 2f3ce303ae..737413a5f4 100644
--- a/math/s_fdimf.c
+++ b/math/s_fdimf.c
@@ -1,5 +1,5 @@
 /* Return positive difference between arguments.
-   Copyright (C) 1997, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1997, 2004, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
@@ -18,6 +18,7 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <errno.h>
 #include <math.h>
 
 float
@@ -31,6 +32,13 @@ __fdimf (float x, float y)
     /* Raise invalid flag.  */
     return x - y;
 
-  return x <= y ? 0 : x - y;
+  if (x <= y)
+    return 0.0f;
+
+  float r = x - y;
+  if (fpclassify (r) == FP_INFINITE)
+    __set_errno (ERANGE);
+
+  return r;
 }
 weak_alias (__fdimf, fdimf)
diff --git a/math/s_fdiml.c b/math/s_fdiml.c
index 70246bafbd..e1ff11b307 100644
--- a/math/s_fdiml.c
+++ b/math/s_fdiml.c
@@ -1,5 +1,5 @@
 /* Return positive difference between arguments.
-   Copyright (C) 1997, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1997, 2004, 2009 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
 
@@ -18,19 +18,27 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <errno.h>
 #include <math.h>
 
 long double
 __fdiml (long double x, long double y)
 {
-  int clsx = fpclassify (x);
-  int clsy = fpclassify (y);
+  int clsx = fpclassifyl (x);
+  int clsy = fpclassifyl (y);
 
   if (clsx == FP_NAN || clsy == FP_NAN
       || (y < 0 && clsx == FP_INFINITE && clsy == FP_INFINITE))
     /* Raise invalid flag.  */
     return x - y;
 
-  return x <= y ? 0 : x - y;
+  if (x <= y)
+    return 0.0f;
+
+  long double r = x - y;
+  if (fpclassify (r) == FP_INFINITE)
+    __set_errno (ERANGE);
+
+  return r;
 }
 weak_alias (__fdiml, fdiml)