From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by sourceware.org (Postfix) with ESMTPS id 2CAC93858D20 for ; Fri, 4 Feb 2022 11:35:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2CAC93858D20 Received: by mail-pg1-x531.google.com with SMTP id 77so1963897pgc.0 for ; Fri, 04 Feb 2022 03:35:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=CZGYqQYtRKT8GRiGx3DSnZsbEQD4/z6ChX6sqo7SoLA=; b=MD5eNaWXYYeOGEYT8exJwkTAPCv6Kjbha84IDj6AMmgZ+HMHMwqFbdU0JTQBEw6x+t gQuc4tLufemqfV5LPhoRvQlKEHPCGq9YuK0JHATWbaV3L8vwA+lI4juDkg2Er4AsuPht 0hF7MTPK8VNwwu3O/O+1YA/8CsMes7PxEk6xHLCpcsQ//yPXrKJRMlkGlNfrjPRoRl4F B4E7SsJ323fdi1Azy+ZWZY/r2j35A+GZ4xHQi9z6Ho5qrl18Jar68+uHfpxge/dF8xwC am+cyNzbiW0x7TiLjogrfTz2RLHImYC9v2y6oIgIoRzPZQvJEwZLzfFVSXpaIHJOiz7w jJsw== X-Gm-Message-State: AOAM5304f+NA/dTLmP02UeOnKdaJuwqsLzMumyTHlNTGLhuzMSVlfHAC omS+IeiYdjqx3++KryNzyGz0TGOTgbNgyIzHGdFr3Gszx7A= X-Google-Smtp-Source: ABdhPJw+zfJdIOAx3cpYC3VJFLX2DMrjul7GTLV12zIEJ/Sz+XoPChJZ3/8mCtQrUzaeI0Sns7fwTn4Ox0P8G79qAOI= X-Received: by 2002:a63:e84f:: with SMTP id a15mr1976460pgk.191.1643974541811; Fri, 04 Feb 2022 03:35:41 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Krishna Narayanan Date: Fri, 4 Feb 2022 17:05:30 +0530 Message-ID: Subject: Re: Doubts regarding the issue (bug 62181) To: gcc-help X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Feb 2022 11:35:45 -0000 Respected sir, I went through the compiler flags and mistake was on my side using the 'w' suppressor flag to remove the warnings without knowing it.Thanks for the docs and reply. By unicode I meant there is a question mark inside a diagonal.(unicode error symbol) Sir this is perspective with the third print that is "cc"+foobar() where I get zR for gcc (9.3.0) and unicode for g++-10.1. I got your point regarding the garbage value and to throw a warning is better than to get such an unwanted output. I thought there would be a specific reason why it had come because in const char *a ="aa"+'operator/number' i.e when I add some character with some change it gives blank space for numbers and operators, where as for addition of 'a' in *a= "aa"+'a' it give 4 times the unicode symbol but for *c="cc" +'c' and *b="bb"+'b' gives a space as output.Yes it has been quite unpredictable and undefined behaviour. So has the request of warning been granted in the upcoming gcc version!? Thanks and regards, Krishna Narayanan. On Fri, Feb 4, 2022 at 2:18 AM Jonathan Wakely wrote: > On Thu, 3 Feb 2022 at 18:46, Krishna Narayanan via Gcc-help > wrote: > > > > Respected Sir/Madam, > > I have been working on an issue (bug 62181) [C/C++] Expected new warning: > > "adding 'char' to a string does not append to the string" > > [-Wstring-plus-int].This is asking for a warning being added to gcc for > > such a case.Not using concat and indexing which were mentioned earlier > as a > > solution to the warning I tried the example given in > https://godbolt.org/ > > and was trying different permutations of addresses and pointers in clang > > and gcc (trunk), I am not sure but I tried '-w' in compiler flags and it > > worked fine for both clang and gcc ,it did not show any warning.I am not > > sure what exactly did the flag do but the warning arises in other > > optimizations. > > I had a doubt regarding the output of a slightly modified code in which I > > have taken a character instead of int , > > #include > > char ch ='o'; > > char bar() { > > return 1; > > } > > int foobar() { > > return ch; } > > int main() > > { const char* a = "aa"; > > const char *b = "bb" + bar(); > > const char *c = "cc" + foobar(); > > printf("%s, %s, %s", a, b, c); > > return 0 ; > > } > > In the output I get zR when I did it with gcc and get unicode when done > > with g++10.1 .I did not understand how does this output arise.Can you > > please help me out with this? > > What do you mean you get "unicode"? > > "bb" + bar() is equivalent to "bb" + (int)bar() > which is equivalent to "bb" + 1 > > "cc" + foobar() is equivalent to "cc" + (int)'o' > which has undefined behaviour, because it produces a pointer that is > outside the array of three chars "cc\0". > It will print whatever happens to be in memory approximately 100 bytes > after the "cc" string literal. That is random garbage of some kind. > > The precise behaviour you get from this undefined behaviour is not > important, the point is that it's undefined behaviour, and that's why > a warning would be useful. >