summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-03-22 10:18:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2012-03-22 10:18:33 -0700
commit6254e4fa987437b1d785cae66122d707c886e144 (patch)
tree45ecf360d0748a1f33a18f93d6dde199cde4b497
parentd4a331511ffa45f41a0a619649e366905e406037 (diff)
downloadtxr-6254e4fa987437b1d785cae66122d707c886e144.tar.gz
txr-6254e4fa987437b1d785cae66122d707c886e144.tar.bz2
txr-6254e4fa987437b1d785cae66122d707c886e144.zip
* arith.c (int_flo): If sprintf produces something
that doesn't begin with a digit, it's most likely NaN or Inf. We can turn that into an exception. * stream.c (vformat): If sprintf produces a non-number, turn it into the printed representation #<bad-float>.
-rw-r--r--ChangeLog9
-rw-r--r--arith.c6
-rw-r--r--stream.c14
3 files changed, 29 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 9ca30cf7..7da801c8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2012-03-22 Kaz Kylheku <kaz@kylheku.com>
+ * arith.c (int_flo): If sprintf produces something
+ that doesn't begin with a digit, it's most likely NaN or Inf.
+ We can turn that into an exception.
+
+ * stream.c (vformat): If sprintf produces a non-number,
+ turn it into the printed representation #<bad-float>.
+
+2012-03-22 Kaz Kylheku <kaz@kylheku.com>
+
* arith.c (to_float): New static function.
(divi): Uses to_float.
(zerop, gt, lt, ge, le, expt): Floating support.
diff --git a/arith.c b/arith.c
index c98d0531..6bb82efd 100644
--- a/arith.c
+++ b/arith.c
@@ -39,6 +39,7 @@
#include <wchar.h>
#include <limits.h>
#include <math.h>
+#include <ctype.h>
#include "config.h"
#include "lib.h"
#include "unwind.h"
@@ -1315,6 +1316,11 @@ val int_flo(val f)
sprintf(text, "%.64g", d);
+ if (!isdigit(text[0]))
+ uw_throwf(error_s,
+ lit("int-flo: cannot convert #<bad-float> to integer"),
+ nao);
+
have_exp = (strchr(text, 'e') != 0);
have_point = (strchr(text, '.') != 0);
diff --git a/stream.c b/stream.c
index 6110e1d6..6761afd8 100644
--- a/stream.c
+++ b/stream.c
@@ -32,6 +32,7 @@
#include <assert.h>
#include <setjmp.h>
#include <errno.h>
+#include <ctype.h>
#include <wchar.h>
#include <unistd.h>
#include "config.h"
@@ -1147,6 +1148,12 @@ val vformat(val stream, val fmtstr, va_list vl)
sprintf(num_buf, "%.*e", precision, n);
else
sprintf(num_buf, "%.*f", precision, n);
+ if (!isdigit(num_buf[0])) {
+ if (!vformat_str(stream, lit("#<bad-float>"),
+ width, left, precision))
+ return nil;
+ continue;
+ }
precision = 0;
goto output_num;
}
@@ -1170,6 +1177,13 @@ val vformat(val stream, val fmtstr, va_list vl)
case FLNUM:
sprintf(num_buf, "%g", obj->fl.n);
+ if (!isdigit(num_buf[0])) {
+ if (!vformat_str(stream, lit("#<bad-float>"),
+ width, left, precision))
+ return nil;
+ continue;
+ }
+
if (!precision) {
if (!strpbrk(num_buf, "e."))
strcat(num_buf, ".0");