aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2021-10-03 13:29:58 -0700
committerMichael Forney <mforney@mforney.org>2021-10-03 13:29:58 -0700
commit043879980822bb15d4d580a2ce36b99f0c73fa83 (patch)
tree561d8c3b672aff72ee461ab6b7706c9385a3b983
parent6764a38d863103f6ca66bb05fbbe2be2225f973e (diff)
downloadcproc-043879980822bb15d4d580a2ce36b99f0c73fa83.tar.xz
expr: Use end pointer to detect string-to-number conversion failures
We explicitly ignore ERANGE for strtod, and in any other error case, the end pointer is set to the beginning of the string.
-rw-r--r--expr.c11
-rw-r--r--qbe.c1
2 files changed, 4 insertions, 8 deletions
diff --git a/expr.c b/expr.c
index 57ef471..19dd012 100644
--- a/expr.c
+++ b/expr.c
@@ -1,6 +1,5 @@
#include <assert.h>
#include <ctype.h>
-#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
@@ -511,10 +510,9 @@ primaryexpr(struct scope *s)
base = tok.lit[0] != '0' ? 10 : tolower(tok.lit[1]) == 'x' ? 16 : 8;
if (strpbrk(tok.lit, base == 16 ? ".pP" : ".eE")) {
/* floating constant */
- errno = 0;
e->constant.f = strtod(tok.lit, &end);
- if (errno && errno != ERANGE)
- error(&tok.loc, "invalid floating constant '%s': %s", tok.lit, strerror(errno));
+ if (end == tok.lit)
+ error(&tok.loc, "invalid floating constant '%s'", tok.lit);
if (!end[0])
e->type = &typedouble;
else if (tolower(end[0]) == 'f' && !end[1])
@@ -525,10 +523,9 @@ primaryexpr(struct scope *s)
error(&tok.loc, "invalid floating constant suffix '%s'", end);
} else {
/* integer constant */
- errno = 0;
e->constant.u = strtoull(tok.lit, &end, 0);
- if (errno)
- error(&tok.loc, "invalid integer constant '%s': %s", tok.lit, strerror(errno));
+ if (end == tok.lit)
+ error(&tok.loc, "invalid integer constant '%s'", tok.lit);
e->type = inttype(e->constant.u, base == 10, end);
}
next();
diff --git a/qbe.c b/qbe.c
index a1f2773..50eb5eb 100644
--- a/qbe.c
+++ b/qbe.c
@@ -1,6 +1,5 @@
#include <assert.h>
#include <ctype.h>
-#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>