~mcf/cproc

Deny casts from floating types to pointers. v1 PROPOSED

Isaiah Poston
Isaiah Poston: 1
 Deny casts from floating types to pointers.

 1 files changed, 21 insertions(+), 2 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~mcf/cproc/patches/14737/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Deny casts from floating types to pointers. Export this patch

Isaiah Poston
In section 6.5.4, the standard says, "A pointer type shall not be
converted to any floating type. A floating type shall not be converted
to any pointer type." This patch denies these casts.
---
 expr.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/expr.c b/expr.c
index 8b10bf4..566fe33 100644
--- a/expr.c
+++ b/expr.c
@@ -813,6 +813,21 @@ postfixexpr(struct scope *s, struct expr *r)
	}
}

/* 6.5.4 Cast constraints */
static void
castcheck(struct expr *e1, struct type *t)
{
	/* Pointer types may not be converted to floating types. */
	if ((e1->type->kind == TYPEPOINTER && (t->kind == TYPEFLOAT
			|| t->kind == TYPEDOUBLE
			|| t->kind == TYPELDOUBLE))
		|| (t->kind == TYPEPOINTER && (e1->type->kind == TYPEFLOAT
			|| e1->type->kind == TYPEDOUBLE
			|| e1->type->kind == TYPELDOUBLE))) {
		error(&tok.loc, "invalid cast from pointer to floating type");
	}
}

static struct expr *castexpr(struct scope *);

static struct expr *
@@ -911,7 +926,7 @@ unaryexpr(struct scope *s)
static struct expr *
castexpr(struct scope *s)
{
	struct type *t;
	struct type *t = NULL;
	enum typequal tq;
	struct expr *r, *e, **end;

@@ -936,12 +951,16 @@ castexpr(struct scope *s)
			return r;
		}
		e = mkexpr(EXPRCAST, t);
		// XXX check types 6.5.4
		*end = e;
		end = &e->base;
	}
	*end = unaryexpr(s);

	if (t && *end && (*end)->type) {
		/* printf("*end type: (%d) %d", t->kind, (*end)->type->kind); */
		castcheck((*end), t);
	}

	return r;
}

-- 
2.29.2