Module Name:    src
Committed By:   rillig
Date:           Thu May 26 07:03:03 UTC 2022

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

Log Message:
tests/lint: demonstrate wrong 'may lose accuracy' warning

Reported in PR 36668, fixed in sys/sys/endian.h 1.26 from 2007-07-20,
unfixed in sys/sys/endian.h 1.29 from 2014-03-18.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/msg_132.c
cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/xlint/lint1/msg_132.exp

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.9 src/tests/usr.bin/xlint/lint1/msg_132.c:1.10
--- src/tests/usr.bin/xlint/lint1/msg_132.c:1.9	Thu Apr 21 19:48:18 2022
+++ src/tests/usr.bin/xlint/lint1/msg_132.c	Thu May 26 07:03:03 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_132.c,v 1.9 2022/04/21 19:48:18 rillig Exp $	*/
+/*	$NetBSD: msg_132.c,v 1.10 2022/05/26 07:03:03 rillig Exp $	*/
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
@@ -140,3 +140,37 @@ non_constant_expression(void)
 	/* expect+1: warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132] */
 	return not_a_constant * 8ULL;
 }
+
+typedef unsigned char u8_t;
+typedef unsigned short u16_t;
+typedef unsigned int u32_t;
+typedef unsigned long long u64_t;
+
+/*
+ * PR 36668 notices that lint wrongly complains about the possible loss.
+ * The expression 'uint8_t << 8' is guaranteed to fit into an 'unsigned short'.
+ * 'unsigned short | unsigned char' is guaranteed to fit into 'unsigned short'
+ */
+static inline u16_t
+be16dec(const void *buf)
+{
+	const u8_t *p = buf;
+
+	/* expect+1: warning: conversion from 'int' to 'unsigned short' may lose accuracy [132] */
+	return ((u16_t)p[0]) << 8 | p[1];
+}
+
+/*
+ * Since tree.c 1.434 from 2022-04-19, lint infers the possible values of
+ * expressions of the form 'integer & constant', see can_represent.
+ */
+static inline void
+be32enc(void *buf, u32_t u)
+{
+	u8_t *p = buf;
+
+	p[0] = u >> 24 & 0xff;
+	p[1] = u >> 16 & 0xff;
+	p[2] = u >> 8 & 0xff;
+	p[3] = u & 0xff;
+}

Index: src/tests/usr.bin/xlint/lint1/msg_132.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_132.exp:1.8 src/tests/usr.bin/xlint/lint1/msg_132.exp:1.9
--- src/tests/usr.bin/xlint/lint1/msg_132.exp:1.8	Thu Apr 21 19:48:18 2022
+++ src/tests/usr.bin/xlint/lint1/msg_132.exp	Thu May 26 07:03:03 2022
@@ -25,3 +25,4 @@ msg_132.c(99): warning: conversion from 
 msg_132.c(125): error: operands of '+' have incompatible types (pointer != double) [107]
 msg_132.c(125): warning: function 'cover_build_plus_minus' expects to return value [214]
 msg_132.c(141): warning: conversion from 'unsigned long long' to 'int' may lose accuracy [132]
+msg_132.c(160): warning: conversion from 'int' to 'unsigned short' may lose accuracy [132]

Reply via email to