From 2b5c0f6a77a6a9f6062fa24ee0efe42c83c275c5 Mon Sep 17 00:00:00 2001 From: rillig Date: Sat, 9 Mar 2024 17:34:01 +0000 Subject: [PATCH] tests/lint: demonstrate integer overflow on lp64 platforms --- .../usr.bin/xlint/lint1/platform_ilp32_int.c | 67 ++++++++++++++++--- .../usr.bin/xlint/lint1/platform_ilp32_long.c | 67 ++++++++++++++++--- tests/usr.bin/xlint/lint1/platform_lp64.c | 53 ++++++++++++--- 3 files changed, 163 insertions(+), 24 deletions(-) diff --git a/tests/usr.bin/xlint/lint1/platform_ilp32_int.c b/tests/usr.bin/xlint/lint1/platform_ilp32_int.c index 7f2b1ea5521c..bd3f568f2654 100644 --- a/tests/usr.bin/xlint/lint1/platform_ilp32_int.c +++ b/tests/usr.bin/xlint/lint1/platform_ilp32_int.c @@ -1,4 +1,4 @@ -/* $NetBSD: platform_ilp32_int.c,v 1.3 2024/03/09 16:47:09 rillig Exp $ */ +/* $NetBSD: platform_ilp32_int.c,v 1.4 2024/03/09 17:34:01 rillig Exp $ */ # 3 "platform_ilp32_int.c" /* @@ -28,24 +28,75 @@ convert_between_int_and_long(void) ul32 = u32; } -char ch; +unsigned char u8; +unsigned long long u64; +unsigned char u8_buf[20]; +unsigned long long u64_buf[20]; void array_index(void) { - static char buf[20]; + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u8 += u8_buf[0x00ffffff]; /* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */ - ch += buf[2147483647]; + u8 += u8_buf[0x7fffffff]; /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ - ch += buf[2147483648]; + u8 += u8_buf[2147483648]; /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ - ch += buf[0x80000000]; + u8 += u8_buf[0x80000000]; /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -1 [167] */ - ch += buf[0xffffffff]; + u8 += u8_buf[0xffffffff]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ + u8 += u8_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -1 [167] */ - ch += buf[0xffffffffffffffff]; + u8 += u8_buf[0xffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u8 += u8_buf[0x00ffffffffffffff]; + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u8 += u8_buf[0xffffffffffffffff]; + + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u64 += u64_buf[0x00ffffff]; + /* expect+2: warning: operator '*' produces integer overflow [141] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x7fffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[2147483648]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffff]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x00ffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x0fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x1fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x3fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'int' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x7fffffffffffffff]; + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffffffffffff]; } diff --git a/tests/usr.bin/xlint/lint1/platform_ilp32_long.c b/tests/usr.bin/xlint/lint1/platform_ilp32_long.c index 923bd466a7c3..2b39e230633b 100644 --- a/tests/usr.bin/xlint/lint1/platform_ilp32_long.c +++ b/tests/usr.bin/xlint/lint1/platform_ilp32_long.c @@ -1,4 +1,4 @@ -/* $NetBSD: platform_ilp32_long.c,v 1.5 2024/03/09 16:47:09 rillig Exp $ */ +/* $NetBSD: platform_ilp32_long.c,v 1.6 2024/03/09 17:34:01 rillig Exp $ */ # 3 "platform_ilp32_long.c" /* @@ -37,24 +37,75 @@ convert_between_int_and_long(void) ul32 = u32; } -char ch; +unsigned char u8; +unsigned long long u64; +unsigned char u8_buf[20]; +unsigned long long u64_buf[20]; void array_index(void) { - static char buf[20]; + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u8 += u8_buf[0x00ffffff]; /* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */ - ch += buf[2147483647]; + u8 += u8_buf[0x7fffffff]; /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ - ch += buf[2147483648]; + u8 += u8_buf[2147483648]; /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ - ch += buf[0x80000000]; + u8 += u8_buf[0x80000000]; /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -1 [167] */ - ch += buf[0xffffffff]; + u8 += u8_buf[0xffffffff]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */ + u8 += u8_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ /* expect+1: warning: array subscript cannot be negative: -1 [167] */ - ch += buf[0xffffffffffffffff]; + u8 += u8_buf[0xffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u8 += u8_buf[0x00ffffffffffffff]; + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u8 += u8_buf[0xffffffffffffffff]; + + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u64 += u64_buf[0x00ffffff]; + /* expect+2: warning: operator '*' produces integer overflow [141] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x7fffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[2147483648]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffff]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ + /* expect+1: warning: operator '*' produces integer overflow [141] */ + u64 += u64_buf[0x80000000]; + /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x00ffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x0fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x1fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x3fffffffffffffff]; + /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */ + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0x7fffffffffffffff]; + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffffffffffff]; } diff --git a/tests/usr.bin/xlint/lint1/platform_lp64.c b/tests/usr.bin/xlint/lint1/platform_lp64.c index 760f6514f869..26f3c7177323 100644 --- a/tests/usr.bin/xlint/lint1/platform_lp64.c +++ b/tests/usr.bin/xlint/lint1/platform_lp64.c @@ -1,4 +1,4 @@ -/* $NetBSD: platform_lp64.c,v 1.8 2024/03/09 16:47:09 rillig Exp $ */ +/* $NetBSD: platform_lp64.c,v 1.9 2024/03/09 17:34:01 rillig Exp $ */ # 3 "platform_lp64.c" /* @@ -45,21 +45,58 @@ convert_128(void) u32 = u128; } -char ch; +unsigned char u8; +unsigned long long u64; +unsigned char u8_buf[20]; +unsigned long long u64_buf[20]; void array_index(void) { - static char buf[20]; + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u8 += u8_buf[0x00ffffff]; /* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */ - ch += buf[2147483647]; + u8 += u8_buf[0x7fffffff]; /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ - ch += buf[2147483648]; + u8 += u8_buf[2147483648]; /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ - ch += buf[0x80000000]; + u8 += u8_buf[0x80000000]; /* expect+1: warning: array subscript cannot be > 19: 4294967295 [168] */ - ch += buf[0xffffffff]; + u8 += u8_buf[0xffffffff]; + /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ + u8 += u8_buf[0x80000000]; + /* expect+1: warning: array subscript cannot be > 19: 4294967295 [168] */ + u8 += u8_buf[0xffffffff]; + /* expect+1: warning: array subscript cannot be > 19: 72057594037927935 [168] */ + u8 += u8_buf[0x00ffffffffffffff]; /* expect+1: warning: array subscript cannot be negative: -1 [167] */ - ch += buf[0xffffffffffffffff]; + u8 += u8_buf[0xffffffffffffffff]; + + /* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */ + u64 += u64_buf[0x00ffffff]; + /* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */ + u64 += u64_buf[0x7fffffff]; + /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ + u64 += u64_buf[2147483648]; + /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ + u64 += u64_buf[0x80000000]; + /* expect+1: warning: array subscript cannot be > 19: 4294967295 [168] */ + u64 += u64_buf[0xffffffff]; + /* expect+1: warning: array subscript cannot be > 19: 2147483648 [168] */ + u64 += u64_buf[0x80000000]; + /* expect+1: warning: array subscript cannot be > 19: 4294967295 [168] */ + u64 += u64_buf[0xffffffff]; + /* expect+1: warning: array subscript cannot be > 19: 72057594037927935 [168] */ + u64 += u64_buf[0x00ffffffffffffff]; + /* expect+1: warning: array subscript cannot be > 19: 1152921504606846975 [168] */ + u64 += u64_buf[0x0fffffffffffffff]; + // FIXME: integer overflow + //u64 += u64_buf[0x1fffffffffffffff]; + // FIXME: integer overflow + //u64 += u64_buf[0x3fffffffffffffff]; + // FIXME: integer overflow + //u64 += u64_buf[0x7fffffffffffffff]; + /* expect+1: warning: array subscript cannot be negative: -1 [167] */ + u64 += u64_buf[0xffffffffffffffff]; }