From f1c715c4559f9033352e51d3b75358463ac9c864 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 7 Feb 2021 11:35:36 +0800 Subject: [PATCH] Fixed a bug that pointer subtraction results from two absolute addresses are calculated as unsigned long. --- src/cc65/expr.c | 2 +- test/val/bug1310.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 4f8a09bec..30eda8707 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -3353,7 +3353,7 @@ static void parsesub (ExprDesc* Expr) Error ("Incompatible pointer types"); } else { Expr->IVal = (Expr->IVal - Expr2.IVal) / - CheckedPSizeOf (lhst); + (long)CheckedPSizeOf (lhst); } /* Operate on pointers, result type is an integer */ Expr->Type = type_int; diff --git a/test/val/bug1310.c b/test/val/bug1310.c index dc9e47b5c..306c91fb6 100644 --- a/test/val/bug1310.c +++ b/test/val/bug1310.c @@ -76,6 +76,12 @@ int main (void) fprintf (stderr, "Expected ~32767U == 32768U, got: %ld\n", (long)~32767U); failures++; } + + if ((long*)0x1000 - (long*)0x2000 >= 0) { + fprintf (stderr, "Expected (long*)0x1000 - (long*)0x2000 < 0, got: %ld\n", (long*)0x1000 - (long*)0x2000); + failures++; + } printf ("failures: %u\n", failures); + return failures; }