From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id 1F2EC3858401 for ; Tue, 31 Aug 2021 08:38:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1F2EC3858401 Received: by mail-pj1-x102c.google.com with SMTP id j1so11292867pjv.3 for ; Tue, 31 Aug 2021 01:38:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=2ofWayATK7ECURdmq9sFrONoMc9pHO75Hw0B3jf2bDQ=; b=VJbPcVq+n6PtzA3xllwkbqZy3Ov+bHHOPMu59XdC/eID7E8ko5vbB9ZnzAqkRr4tr7 GDA9aQ77r5Dzpb6esXZx3ryvXcLXtMGhCaSqSRl6pWCwAl8r0hJberT24tSB48Vvzq0x Iy4mF8R80QIzGxetWHOPTq4u1JgCPSiy49S8TZ7miwx+PULACYUJDQ/n2aHLMlYfuCXy BPQIop0NwZOEybchttNhSekUoAy8fD7v2Q3t6pFRDBqZ7b/JA8hyPHeUkea7sBfkwRKS wFPrWA9nQCxDOXFI0EJqmd4QNc3635jl0YPG1ufR9Oih+lGUAA9TX/MG5O81WIjz2UDW vS2g== X-Gm-Message-State: AOAM530NDUt5qi4/rHYqXkagSheZHcPmv5ikz0FfcEZceBla5Kb6YkKG H6mWeHTIFi7uRaX9z31nOgs= X-Google-Smtp-Source: ABdhPJzAHdy8uhSQzK4OoZ8xPOrWKFCZp7eB8lXcIe6ciHIdVVSKQhM6uJCZMMXhP9T5T3RwXArq4A== X-Received: by 2002:a17:90a:aa14:: with SMTP id k20mr4156936pjq.88.1630399138112; Tue, 31 Aug 2021 01:38:58 -0700 (PDT) Received: from localhost ([45.251.50.171]) by smtp.gmail.com with ESMTPSA id a10sm16670084pfo.75.2021.08.31.01.38.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 01:38:57 -0700 (PDT) From: Utkarsh Singh To: Jonathan Wakely Cc: "gcc@gcc.gnu.org" , Aakarsh MJ Subject: Re: Problems in array access In-Reply-To: References: <87r1eac9v4.fsf@gmail.com> Date: Tue, 31 Aug 2021 14:09:37 +0530 Message-ID: <87lf4ic8ja.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=1.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, PDS_OTHER_BAD_TLD, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Level: * 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 08:39:09 -0000 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 Great! And thank you for a quick reply. -- Utkarsh Singh http://utkarshsingh.xyz