lint: fix integer overflow on seeing -(uint64_t)INT64_MIN

Seen in external/cddl/osnet/dist/lib/libuutil/common/uu_strtoint.c,
which is protected by a NOLINT in ../Makefile.inc.
This commit is contained in:
rillig 2022-04-19 23:16:14 +00:00
parent 3e14ad04ec
commit 598b5fa387
3 changed files with 18 additions and 4 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: expr_fold.c,v 1.6 2022/04/15 21:50:07 rillig Exp $ */
/* $NetBSD: expr_fold.c,v 1.7 2022/04/19 23:16:14 rillig Exp $ */
# 3 "expr_fold.c"
/*
@ -287,3 +287,16 @@ struct ctassert5_struct {
? 1
: -1;
};
/*
* Since Makefile.inc 1.21 from 2022-04-08 (which added -ftrapv) and before
* tree.c 1.436 from 2022-04-20, lint crashed with an integer overflow when
* calculating '-(uint64_t)INT64_MIN' in val_t.v_quad.
*/
void
unary_minus_overflow(unsigned long long val)
{
/* expect+1: warning: integer overflow detected, op '-' [141] */
if (val > -(unsigned long long)(-0x7fffffffffffffffL - 1))
return;
}

View File

@ -32,3 +32,4 @@ expr_fold.c(183): warning: integer overflow detected, op '<<' [141]
expr_fold.c(186): warning: integer overflow detected, op '<<' [141]
expr_fold.c(190): warning: shift amount 104 is greater than bit-size 32 of 'unsigned int' [122]
expr_fold.c(202): warning: shift amount 104 is greater than bit-size 32 of 'int' [122]
expr_fold.c(300): warning: integer overflow detected, op '-' [141]

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.435 2022/04/19 22:14:30 rillig Exp $ */
/* $NetBSD: tree.c,v 1.436 2022/04/19 23:16:14 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(lint)
__RCSID("$NetBSD: tree.c,v 1.435 2022/04/19 22:14:30 rillig Exp $");
__RCSID("$NetBSD: tree.c,v 1.436 2022/04/19 23:16:14 rillig Exp $");
#endif
#include <float.h>
@ -3210,7 +3210,7 @@ fold(tnode_t *tn)
q = sl;
break;
case UMINUS:
q = -sl;
q = sl == INT64_MIN ? sl : -sl;
if (sl != 0 && msb(q, t) == msb(sl, t))
ovfl = true;
break;