Module Name:    src
Committed By:   rillig
Date:           Thu Jan  2 18:36:52 UTC 2025

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_132.c
        src/usr.bin/xlint/lint1: tree.c

Log Message:
lint: fix assertion failure in pointer subtraction


To generate a diff of this commit:
cvs rdiff -u -r1.50 -r1.51 src/tests/usr.bin/xlint/lint1/msg_132.c
cvs rdiff -u -r1.667 -r1.668 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/msg_132.c
diff -u src/tests/usr.bin/xlint/lint1/msg_132.c:1.50 src/tests/usr.bin/xlint/lint1/msg_132.c:1.51
--- src/tests/usr.bin/xlint/lint1/msg_132.c:1.50	Thu Jan  2 17:35:02 2025
+++ src/tests/usr.bin/xlint/lint1/msg_132.c	Thu Jan  2 18:36:52 2025
@@ -1,10 +1,8 @@
-/*	$NetBSD: msg_132.c,v 1.50 2025/01/02 17:35:02 rillig Exp $	*/
+/*	$NetBSD: msg_132.c,v 1.51 2025/01/02 18:36:52 rillig Exp $	*/
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
 
-/* lint1-extra-flags: -X 351 */
-
 /*
  * NetBSD's default lint flags only include a single -a, which only flags
  * narrowing conversions from long.  To get warnings for all narrowing
@@ -13,7 +11,7 @@
  * https://gnats.netbsd.org/14531
  */
 
-/* lint1-extra-flags: -aa */
+/* lint1-extra-flags: -aa -X 351 */
 
 typedef unsigned char u8_t;
 typedef unsigned short u16_t;
@@ -37,6 +35,8 @@ s16_t s16;
 s32_t s32;
 s64_t s64;
 
+const char *ptr;
+
 struct bit_fields {
 	unsigned u1:1;
 	unsigned u2:2;
@@ -376,6 +376,8 @@ test_ic_plus(void)
 	u16 = u32 % 0x00010000 + 0xffff0000 + 0x00010000;
 
 	s8 = '0' + s64 % 10;
+
+	ptr = ptr + 3;
 }
 
 void
@@ -400,6 +402,11 @@ test_ic_minus(void)
 	u16 = s16 - -0x8000;
 	u32 = s32 - -0x80000000;
 	u64 = s64 - -0x8000000000000000;
+
+	ptr = ptr - 3;
+	s64 = ptr + 3 - ptr;
+	/* expect+1: warning: conversion from 'long' to 'unsigned int' may lose accuracy [132] */
+	u32 = ptr + 3 - ptr;
 }
 
 void

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.667 src/usr.bin/xlint/lint1/tree.c:1.668
--- src/usr.bin/xlint/lint1/tree.c:1.667	Thu Jan  2 17:35:01 2025
+++ src/usr.bin/xlint/lint1/tree.c	Thu Jan  2 18:36:51 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.667 2025/01/02 17:35:01 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.668 2025/01/02 18:36:51 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.667 2025/01/02 17:35:01 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.668 2025/01/02 18:36:51 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -439,16 +439,16 @@ ic_con(const type_t *tp, const val_t *v)
 static integer_constraints
 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
 {
-	unsigned nw = width_in_bits(ntp);
-	unsigned ow = width_in_bits(otp);
-	bool nu = is_uinteger(ntp->t_tspec);
-	bool ou = is_uinteger(otp->t_tspec);
+	unsigned new_width = width_in_bits(ntp);
+	unsigned old_width = width_in_bits(otp);
+	bool new_unsigned = is_uinteger(ntp->t_tspec);
+	bool old_unsigned = is_uinteger(otp->t_tspec);
 
-	if (nw >= ow && nu == ou)
+	if (new_width >= old_width && new_unsigned == old_unsigned)
 		return a;
-	if (nw > ow && ou)
+	if (new_width > old_width && old_unsigned)
 		return a;
-	if (nu && (~value_bits(nw) & ~a.bclr) == 0)
+	if (new_unsigned && (~value_bits(new_width) & ~a.bclr) == 0)
 		return a;
 	return ic_any(ntp);
 }
@@ -478,6 +478,8 @@ ic_expr(const tnode_t *tn)
 		rc = ic_expr(before_conversion(tn->u.ops.right));
 		return ic_plus(tn->tn_type, lc, rc);
 	case MINUS:
+		if (tn->u.ops.left->tn_type->t_tspec == PTR)
+			return ic_any(tn->tn_type);
 		lc = ic_expr(before_conversion(tn->u.ops.left));
 		rc = ic_expr(before_conversion(tn->u.ops.right));
 		return ic_minus(tn->tn_type, lc, rc);

Reply via email to