Deutsch   English   Français   Italiano  
<20250210123802.00001ec3@yahoo.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: Two questions on arrays with size defined by variables
Date: Mon, 10 Feb 2025 12:38:02 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <20250210123802.00001ec3@yahoo.com>
References: <vo9mns$gsd7$1@dont-email.me>
	<vo9nn3$gtph$1@dont-email.me>
	<vo9u0u$i0n8$1@dont-email.me>
	<voae3a$2rke4$1@paganini.bofh.team>
	<voao29$o6uh$2@dont-email.me>
	<voaol9$o8ic$1@dont-email.me>
	<voapm4$oh21$1@dont-email.me>
	<20250209201902.000019e5@yahoo.com>
	<vobllr$tdqt$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 10 Feb 2025 11:38:03 +0100 (CET)
Injection-Info: dont-email.me; posting-host="7b7cf1bcc147fed05c30f988ad9ee242";
	logging-data="802523"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18iRIJ29AgGqrQ0GCBG8aPGeI7WqcV4nuQ="
Cancel-Lock: sha1:kUV4pQZFPAwCIl0jwiJGiAggQDM=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
Bytes: 2437

On Mon, 10 Feb 2025 02:44:26 +0100
Opus <ifonly@youknew.org> wrote:

> On 09/02/2025 19:19, Michael S wrote:
> > On Sun, 9 Feb 2025 18:46:44 +0100
> > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> >   
> >> wrong! - assignment to 'a[99]' produced also no compiler
> >> complaints,  
> > 
> > gcc produces warning in this case, but only at optimization level
> > of 2 or higher.  
> 
> Which version of gcc?

14.1

My test code is:

void bar(char*[]);
int foo(void)
{
  int n = 10;
  char *a[n];
  bar(a);
  a[99] = "42";
  return a[3][2];
}


> Tried with gcc 14.2 (x86-64) with -Wall -O3 (or -O2, same), it
> doesn't give any warning whatsoever. (And yes, same with clang.)
> 

May be, in your test arr[] is not used later, so compiler silently
optimizes away all accesses?

> Cppcheck does spot it properly though:
> 
> error: Array 'arr[5]' accessed at index 99, which is out of bounds. 
> [arrayIndexOutOfBounds]
>    arr[99] = "foobar";
> 
> I highly recommend using Cppcheck as a static analyzer (at the bare 
> minimum, there are better out there). Compilers are pretty basic in 
> terms of static analysis.
>