public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Problems in array access
@ 2021-08-31  8:10 Utkarsh Singh
  2021-08-31  8:28 ` Jonathan Wakely
  0 siblings, 1 reply; 5+ messages in thread
From: Utkarsh Singh @ 2021-08-31  8:10 UTC (permalink / raw)
  To: gcc; +Cc: Aakarsh MJ

Hello GCC mailing list,

In one of my friend's C programming class, they asked him a question on
the topic of array bounds based on the follwing code snippet:

#include <stdio.h>

int main(void)
{
	char str[] = {'G' , 'C' , 'C' };
	str[3] = '\0' ; /* Isn't this invalid? */
	printf("%s\n", str);
}

In an ideal case, str[3] should be a case of out-of-bound array access.
But when compiling the above with -Wall option flag GCC shows no
warning.  So, am I missing something?

Thank you,
Utkarsh Singh

-- 
Utkarsh Singh
http://utkarshsingh.xyz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems in array access
  2021-08-31  8:10 Problems in array access Utkarsh Singh
@ 2021-08-31  8:28 ` Jonathan Wakely
  2021-08-31  8:39   ` Utkarsh Singh
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2021-08-31  8:28 UTC (permalink / raw)
  To: Utkarsh Singh; +Cc: gcc, Aakarsh MJ

On Tue, 31 Aug 2021 at 09:11, Utkarsh Singh wrote:
>
> Hello GCC mailing list,
>
> In one of my friend's C programming class, they asked him a question on
> the topic of array bounds based on the follwing code snippet:
>
> #include <stdio.h>
>
> int main(void)
> {
>         char str[] = {'G' , 'C' , 'C' };
>         str[3] = '\0' ; /* Isn't this invalid? */
>         printf("%s\n", str);
> }
>
> In an ideal case, str[3] should be a case of out-of-bound array access.
> But when compiling the above with -Wall option flag GCC shows no
> warning.  So, am I missing something?

This question belongs on the gcc-help mailing list, not here.

The code has undefined behaviour.

Some GCC warnings depend on checks done during optimization. GCC will
warn about this code if you use -Wall -O2 and you will get a runtime
error if you compile with -fsanitize=undefined

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems in array access
  2021-08-31  8:28 ` Jonathan Wakely
@ 2021-08-31  8:39   ` Utkarsh Singh
  2021-08-31 15:08     ` Martin Sebor
  0 siblings, 1 reply; 5+ messages in thread
From: Utkarsh Singh @ 2021-08-31  8:39 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc, Aakarsh MJ

On 2021-08-31, 09:28 +0100, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:

> On Tue, 31 Aug 2021 at 09:11, Utkarsh Singh wrote:
>>
>> Hello GCC mailing list,
>>
>> In one of my friend's C programming class, they asked him a question on
>> the topic of array bounds based on the follwing code snippet:
>>
>> #include <stdio.h>
>>
>> int main(void)
>> {
>>         char str[] = {'G' , 'C' , 'C' };
>>         str[3] = '\0' ; /* Isn't this invalid? */
>>         printf("%s\n", str);
>> }
>>
>> In an ideal case, str[3] should be a case of out-of-bound array access.
>> But when compiling the above with -Wall option flag GCC shows no
>> warning.  So, am I missing something?
>
> This question belongs on the gcc-help mailing list, not here.

Sorry! I will keep this in mind.

> The code has undefined behaviour.
>
> Some GCC warnings depend on checks done during optimization. GCC will
> warn about this code if you use -Wall -O2 and you will get a runtime
> error if you compile with -fsanitize=undefined

Great! And thank you for a quick reply.

-- 
Utkarsh Singh
http://utkarshsingh.xyz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems in array access
  2021-08-31  8:39   ` Utkarsh Singh
@ 2021-08-31 15:08     ` Martin Sebor
  2021-09-01  2:31       ` Utkarsh Singh
  0 siblings, 1 reply; 5+ messages in thread
From: Martin Sebor @ 2021-08-31 15:08 UTC (permalink / raw)
  To: Utkarsh Singh, Jonathan Wakely; +Cc: gcc, Aakarsh MJ

On 8/31/21 2:39 AM, Utkarsh Singh via Gcc wrote:
> On 2021-08-31, 09:28 +0100, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> 
>> On Tue, 31 Aug 2021 at 09:11, Utkarsh Singh wrote:
>>>
>>> Hello GCC mailing list,
>>>
>>> In one of my friend's C programming class, they asked him a question on
>>> the topic of array bounds based on the follwing code snippet:
>>>
>>> #include <stdio.h>
>>>
>>> int main(void)
>>> {
>>>          char str[] = {'G' , 'C' , 'C' };
>>>          str[3] = '\0' ; /* Isn't this invalid? */
>>>          printf("%s\n", str);
>>> }
>>>
>>> In an ideal case, str[3] should be a case of out-of-bound array access.
>>> But when compiling the above with -Wall option flag GCC shows no
>>> warning.  So, am I missing something?
>>
>> This question belongs on the gcc-help mailing list, not here.
> 
> Sorry! I will keep this in mind.
> 
>> The code has undefined behaviour.
>>
>> Some GCC warnings depend on checks done during optimization. GCC will
>> warn about this code if you use -Wall -O2 and you will get a runtime
>> error if you compile with -fsanitize=undefined

To refine Jonathan's answer: In cases where the index is constant
like this one the warning could be issued even with no optimization.
That it isn't is the result of the choice to depend on optimizations
unconditionally.  It's worth revisiting this choice in the future.
If you would like to see such a change for -Warray-bounds (or any
other warning) please open requests in Bugzilla.

Martin

> 
> Great! And thank you for a quick reply.
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problems in array access
  2021-08-31 15:08     ` Martin Sebor
@ 2021-09-01  2:31       ` Utkarsh Singh
  0 siblings, 0 replies; 5+ messages in thread
From: Utkarsh Singh @ 2021-09-01  2:31 UTC (permalink / raw)
  To: Martin Sebor, Jonathan Wakely; +Cc: gcc, Aakarsh MJ

On 2021-08-31, 09:08 -0600, Martin Sebor <msebor@gmail.com> wrote:

> To refine Jonathan's answer: In cases where the index is constant
> like this one the warning could be issued even with no optimization.
> That it isn't is the result of the choice to depend on optimizations
> unconditionally.  It's worth revisiting this choice in the future.
> If you would like to see such a change for -Warray-bounds (or any
> other warning) please open requests in Bugzilla.

Sure! Currently user account registration is restricted at Bugzilla, I
will request it ASAP.

-- 
Utkarsh Singh
http://utkarshsingh.xyz

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-09-01  2:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-31  8:10 Problems in array access Utkarsh Singh
2021-08-31  8:28 ` Jonathan Wakely
2021-08-31  8:39   ` Utkarsh Singh
2021-08-31 15:08     ` Martin Sebor
2021-09-01  2:31       ` Utkarsh Singh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).