From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by sourceware.org (Postfix) with ESMTPS id 0B93D385840B for ; Tue, 31 Aug 2021 15:08:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0B93D385840B Received: by mail-oi1-x230.google.com with SMTP id w19so24776752oik.10 for ; Tue, 31 Aug 2021 08:08:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=3KVvTXbzF0uyevYGX7NLZZdkKFpVodUA8aAh1vxEA68=; b=lM6DVYbwSfvVl9cBX5Bk9Bq91wa5ra0QVHhlpLmUSsUJj0elXUfJgTn2yLSsdhbDM8 Wyh5g+0Pd+mPvVjK88k/+uYkJ/vK9H09ESFD1P73DikBm3rEl12rZXyhTNT8NCQIOtxn RlnAsBrqFe6bHu7mJLPXSkjOCKvBI76e8gKgyAo0fzy7ODvk4WXow60W0ABY/degv+xt s+q6pvGASMIWuSub4a92gZDo7v9XYyzSOQ7W7sXXoxmKByqCJaAdIVYptkQk/LBaXTal 3zN0dVS9pjjBLAabzjniKRnirr83QCp0Qe9guJbKqQFNUxCAUFpLv0Q1riJZhK0tFd05 4ZIA== X-Gm-Message-State: AOAM531m5kOrrTso1BQHxX9VyOhjmqRgx9yOZQlVBOvoMDp/H6gVsH+Z 57QqspxZyLgor5KQDSaDwms= X-Google-Smtp-Source: ABdhPJw4IkD5d4ZxZsdc23E03algMOImBwYOC8pBTS17si4a+UoEMttEyw5UsHHHtoYR5YogGSI1vg== X-Received: by 2002:aca:5889:: with SMTP id m131mr3480581oib.140.1630422514410; Tue, 31 Aug 2021 08:08:34 -0700 (PDT) Received: from [192.168.0.41] (97-118-104-61.hlrn.qwest.net. [97.118.104.61]) by smtp.gmail.com with ESMTPSA id r13sm3940063oti.80.2021.08.31.08.08.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 31 Aug 2021 08:08:34 -0700 (PDT) Subject: Re: Problems in array access To: Utkarsh Singh , Jonathan Wakely Cc: "gcc@gcc.gnu.org" , Aakarsh MJ References: <87r1eac9v4.fsf@gmail.com> <87lf4ic8ja.fsf@gmail.com> From: Martin Sebor Message-ID: <385d7e15-a583-a973-9443-5a62e3b914d5@gmail.com> Date: Tue, 31 Aug 2021 09:08:32 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <87lf4ic8ja.fsf@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Aug 2021 15:08:36 -0000 On 8/31/21 2:39 AM, Utkarsh Singh via Gcc wrote: > On 2021-08-31, 09:28 +0100, Jonathan Wakely 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 >>> >>> 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. >