lint: fix integer overflow in integer overflow check

This commit is contained in:
rillig 2024-03-10 10:15:51 +00:00
parent 16f849b643
commit 34885a599d
2 changed files with 47 additions and 35 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: msg_141.c,v 1.11 2024/03/10 09:58:30 rillig Exp $ */ /* $NetBSD: msg_141.c,v 1.12 2024/03/10 10:15:52 rillig Exp $ */
# 3 "msg_141.c" # 3 "msg_141.c"
// Test for message: operator '%s' produces integer overflow [141] // Test for message: operator '%s' produces integer overflow [141]
@ -162,26 +162,26 @@ mult_u32(void)
void void
mult_s64(void) mult_s64(void)
{ {
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = -0x100000000LL * 0x100000000LL; // -0x010000000000000000 s64 = -0x100000000LL * 0x100000000LL; // -0x010000000000000000
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = -3LL * 0x2aaaaaaaaaaaaaabLL; // -0x8000000000000001 s64 = -3LL * 0x2aaaaaaaaaaaaaabLL; // -0x8000000000000001
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = 0x2aaaaaaaaaaaaaabLL * -3LL; // -0x8000000000000001 s64 = 0x2aaaaaaaaaaaaaabLL * -3LL; // -0x8000000000000001
s64 = -8LL * +0x1000000000000000LL; // -0x8000000000000000 s64 = -8LL * +0x1000000000000000LL; // -0x8000000000000000
s64 = +0x1000000000000000LL * -8LL; // -0x8000000000000000 s64 = +0x1000000000000000LL * -8LL; // -0x8000000000000000
s64 = +2LL * +0x3fffffffffffffffLL; // +0x7ffffffffffffffe s64 = +2LL * +0x3fffffffffffffffLL; // +0x7ffffffffffffffe
s64 = +0x3fffffffffffffffLL * +2LL; // +0x7ffffffffffffffe s64 = +0x3fffffffffffffffLL * +2LL; // +0x7ffffffffffffffe
s64 = +0x7fffffffffffffffLL * +1LL; // +0x7fffffffffffffff s64 = +0x7fffffffffffffffLL * +1LL; // +0x7fffffffffffffff
s64 = +1LL * +0x7fffffffffffffffLL; // +0x7fffffffffffffff s64 = +1LL * +0x7fffffffffffffffLL; // +0x7fffffffffffffff
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = +2LL * +0x4000000000000000LL; // +0x8000000000000000 s64 = +2LL * +0x4000000000000000LL; // +0x8000000000000000
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = +0x4000000000000000LL * +2LL; // +0x8000000000000000 s64 = +0x4000000000000000LL * +2LL; // +0x8000000000000000
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = +0xffffffffLL * +0x100000001LL; // +0xffffffffffffffff s64 = +0xffffffffLL * +0x100000001LL; // +0xffffffffffffffff
/* TODO: expect+1: warning: operator '*' produces integer overflow [141] */ /* expect+1: warning: operator '*' produces integer overflow [141] */
//s64 = +0x100000000LL * +0x100000000LL; // +0x010000000000000000 s64 = +0x100000000LL * +0x100000000LL; // +0x010000000000000000
} }
void void
@ -249,6 +249,7 @@ mod_s32(void)
{ {
s32 = -1 % (-0x7fffffff - 1); s32 = -1 % (-0x7fffffff - 1);
s32 = -1 % 0x7fffffff; s32 = -1 % 0x7fffffff;
/* expect+1: warning: operator '%' produces integer overflow [141] */
s32 = (-0x7fffffff - 1) % -1; s32 = (-0x7fffffff - 1) % -1;
s32 = 0x7fffffff % -1; s32 = 0x7fffffff % -1;
} }
@ -269,9 +270,8 @@ mod_s64(void)
{ {
s64 = -1LL % (-0x7fffffffffffffffLL - 1LL); s64 = -1LL % (-0x7fffffffffffffffLL - 1LL);
s64 = -1LL % 0x7fffffffffffffffLL; s64 = -1LL % 0x7fffffffffffffffLL;
// FIXME: endless loop on x86_64 in idivq instruction, /* expect+1: warning: operator '%' produces integer overflow [141] */
// probably due to SIGFPE handling. s64 = (-0x7fffffffffffffffLL - 1LL) % -1LL;
//s64 = (-0x7fffffffffffffffLL - 1LL) % -1LL;
s64 = 0x7fffffffffffffffLL % -1LL; s64 = 0x7fffffffffffffffLL % -1LL;
} }
@ -334,12 +334,12 @@ plus_u32(void)
void void
plus_s64(void) plus_s64(void)
{ {
/* TODO: expect+1: warning: operator '+' produces integer overflow [141] */ /* expect+1: warning: operator '+' produces integer overflow [141] */
// FIXME: s64 = -0x7fffffffffffffffLL + -2LL; s64 = -0x7fffffffffffffffLL + -2LL;
s64 = -0x7fffffffffffffffLL + -1LL; s64 = -0x7fffffffffffffffLL + -1LL;
s64 = 0x7ffffffffffffffeLL + 1LL; s64 = 0x7ffffffffffffffeLL + 1LL;
/* TODO: expect+1: warning: operator '+' produces integer overflow [141] */ /* expect+1: warning: operator '+' produces integer overflow [141] */
// FIXME: s64 = 0x7fffffffffffffffLL + 1LL; s64 = 0x7fffffffffffffffLL + 1LL;
} }
void void
@ -390,19 +390,19 @@ minus_u32(void)
void void
minus_s64(void) minus_s64(void)
{ {
/* TODO: expect+1: warning: operator '-' produces integer overflow [141] */ /* expect+1: warning: operator '-' produces integer overflow [141] */
// FIXME: s64 = -0x7fffffffffffffffLL - 0x7fffffffffffffffLL; s64 = -0x7fffffffffffffffLL - 0x7fffffffffffffffLL;
/* TODO: expect+1: warning: operator '-' produces integer overflow [141] */ /* expect+1: warning: operator '-' produces integer overflow [141] */
// FIXME: s64 = -0x7fffffffffffffffLL - 2LL; s64 = -0x7fffffffffffffffLL - 2LL;
s64 = -0x7fffffffffffffffLL - 1LL; s64 = -0x7fffffffffffffffLL - 1LL;
s64 = -0x7fffffffffffffffLL - 0LL; s64 = -0x7fffffffffffffffLL - 0LL;
s64 = -0x7fffffffffffffffLL - -1LL; s64 = -0x7fffffffffffffffLL - -1LL;
s64 = 0x7fffffffffffffffLL - 1LL; s64 = 0x7fffffffffffffffLL - 1LL;
s64 = 0x7fffffffffffffffLL - 0LL; s64 = 0x7fffffffffffffffLL - 0LL;
/* TODO: expect+1: warning: operator '-' produces integer overflow [141] */ /* expect+1: warning: operator '-' produces integer overflow [141] */
// FIXME: s64 = 0x7fffffffffffffffLL - -1LL; s64 = 0x7fffffffffffffffLL - -1LL;
/* TODO: expect+1: warning: operator '-' produces integer overflow [141] */ /* expect+1: warning: operator '-' produces integer overflow [141] */
// FIXME: s64 = 0x7fffffffffffffffLL - -0x7fffffffffffffffLL; s64 = 0x7fffffffffffffffLL - -0x7fffffffffffffffLL;
} }
void void

View File

@ -1,4 +1,4 @@
/* $NetBSD: tree.c,v 1.615 2024/03/10 09:24:54 rillig Exp $ */ /* $NetBSD: tree.c,v 1.616 2024/03/10 10:15:51 rillig Exp $ */
/* /*
* Copyright (c) 1994, 1995 Jochen Pohl * Copyright (c) 1994, 1995 Jochen Pohl
@ -37,7 +37,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__RCSID) #if defined(__RCSID)
__RCSID("$NetBSD: tree.c,v 1.615 2024/03/10 09:24:54 rillig Exp $"); __RCSID("$NetBSD: tree.c,v 1.616 2024/03/10 10:15:51 rillig Exp $");
#endif #endif
#include <float.h> #include <float.h>
@ -834,6 +834,15 @@ fold_constant_integer(tnode_t *tn)
else if (ul != 0 && si / ul != ur) else if (ul != 0 && si / ul != ur)
ovfl = true; ovfl = true;
} else { } else {
uint64_t al = sl >= 0 ? ul : -ul;
uint64_t ar = sr >= 0 ? ur : -ur;
bool neg = (sl >= 0) != (sr >= 0);
uint64_t max_prod = (uint64_t)max_value
+ (neg ? 1 : 0);
if (al > 0 && ar > max_prod / al) {
si = (int64_t)(al * ar);
ovfl = true;
} else
si = sl * sr; si = sl * sr;
if (msb(si, t) != (msb(sl, t) ^ msb(sr, t))) if (msb(si, t) != (msb(sl, t) ^ msb(sr, t)))
ovfl = true; ovfl = true;
@ -855,18 +864,21 @@ fold_constant_integer(tnode_t *tn)
/* modulus by 0 */ /* modulus by 0 */
error(140); error(140);
si = 0; si = 0;
} else if (!utyp && sl == min_value && sr == -1) {
ovfl = true;
si = 0;
} else } else
si = utyp ? (int64_t)(ul % ur) : sl % sr; si = utyp ? (int64_t)(ul % ur) : sl % sr;
break; break;
case PLUS: case PLUS:
si = utyp ? (int64_t)(ul + ur) : sl + sr; si = (int64_t)(ul + ur);
if (msb(sl, t) && msb(sr, t) && !msb(si, t)) if (msb(sl, t) && msb(sr, t) && !msb(si, t))
ovfl = true; ovfl = true;
if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(si, t)) if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(si, t))
ovfl = true; ovfl = true;
break; break;
case MINUS: case MINUS:
si = utyp ? (int64_t)(ul - ur) : sl - sr; si = (int64_t)(ul - ur);
if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(si, t)) if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(si, t))
ovfl = true; ovfl = true;
if (!msb(sl, t) && msb(sr, t) && msb(si, t)) if (!msb(sl, t) && msb(sr, t) && msb(si, t))