From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7AD9B3858D1E; Mon, 24 Apr 2023 15:21:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7AD9B3858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682349675; bh=Gg//+nHDo3fdJ95pK1QOaFLsfm47kBkcHX1fa+16Cr8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=MSOwp2sjKagAvsLnf+NgiKguyDXjT4zaqCRHk6HqR9chy5r34Op5u4dgr2f7onjlW LNV9EKLcU8xvanRkSxhjGV3BvknDc5ir8SY/+zokTnaVXt4BiQWbba2UbK5t0oof4T KN7BUKTuAQ231p8SkaQTM9GPbGmX21sKiWN6ghSI= From: "david at westcontrol dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/105523] Wrong warning array subscript [0] is outside array bounds Date: Mon, 24 Apr 2023 15:21:14 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: david at westcontrol dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: cc Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105523 David Brown changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |david at westcontrol dot c= om --- Comment #18 from David Brown --- This issue does not appear to be related to any particular backend. Changing the initial code to remove the volatile : void m(void) { uint8_t * ptr2 =3D ( uint8_t*)(0x0030); *ptr2 =3D 0xd8; } gives a warning in gcc 11 as well - "warning: writing 1 byte into a region = of size 0". (gcc 12 and 13 give the "array subscript [0] is outside array bou= nds" warning.) It is standard practice in embedded development to cast an integer value (o= ften a compile-time constant, but not always) to a pointer and use that to access memory-mapped registers or other data at fixed addresses. Typically the pointers are pointer-to-volatile. Sometimes the data type in question is a "uintN_t" type, sometimes it is a struct covering a range of registers. Th= ere are other ways to specify fixed addresses, such as using "extern" data that= is defined in a linker file or an assembly file - but I have not seen other methods used much in recent decades. The compiler knows nothing about the size of the region pointed to here. I= t is no more appropriate for it to guess the size is 0 than to guess it is 42. = It should either assume the size is one single object of the named type, or an array of unknown size of the named type. (I'd prefer the first, but the se= cond is also a reasonable choice.) As an embedded developer, I don't want to have to turn off useful warnings = to avoid false positives on common and essential constructs.=