shithub: riscv

Download patch

ref: e33798c7e0333e6b8c39be76c7b143834d81b81e
parent: e74a87d537dfd2ed817ba94314df7c517b51d65a
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Tue Dec 12 15:58:59 EST 2023

cc: don't rearrange constant scaling across unsigned casts

On 64-bit platforms, when the index being 32-bit unsigned,
any calculation there must be done modulo 2^32,
but rearranging the factors into the address calculation
is done in signed 64-bits yielding the wrong index.

--- a/sys/src/cmd/cc/scon.c
+++ b/sys/src/cmd/cc/scon.c
@@ -587,8 +587,16 @@
 	switch(n->op) {
 
 	case OCAST:
-		if(nilcast(n->left->type, n->type))
-			return 1;
+		if(nilcast(n->left->type, n->type)){
+			/*
+			 * must not cross casts from unsigned
+			 * when the modulus changes to handle
+			 * overflow correctly.
+			 */
+			if(!typeu[n->left->type->etype]
+			|| ewidth[n->left->type->etype] == ewidth[n->type->etype])
+				return 1;
+		}
 		break;
 
 	case ONEG: