From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id 58E643858406 for ; Thu, 3 Feb 2022 20:48:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 58E643858406 Received: by mail-wr1-x42a.google.com with SMTP id v13so7327674wrv.10 for ; Thu, 03 Feb 2022 12:48:10 -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:cc; bh=VYICveA5ZUTWFv1dRBk+h9ROZ5cOo4QyYsiBFvl8NKw=; b=Wmy3LxKBXItcUGz9CtmzLmxBrjw7/btjLYN6ieEPVzVDHR72cN8Wc4xEQFAlHR6Dia rimtQY0ssnzX/2XYbuOqZpJliXGqnV0DxVYsNkvhU6qZq8+z5YfubUrzDRArZpX3ecyw Ulx7+6X8rZVQKvtRjnsBIV1NJagQGOgkyHBNd9sT1Wk/0n6H+GizbSDeFR/TznY2xADc 9ryOe6E5p3MHVAtEDlm6gZs//bSRqY3AXQhBA6az/uAK1EntNKzBJYH1p3+wWgXYRfs0 rM1ofGpwhxy4Q652UptZREUkpFRrecPMAAo9xXkMkspvZCCnI5/e6d/g/8L5fwPqV3VF xeZQ== X-Gm-Message-State: AOAM532t3/KOKX71XM5i+Lc5W959Gz/v8DtgHlZXYtuWRIKyurzJ7W4e EkRSzgjPuNEKRm8ayh7P/4PiabKSseYK42Sh3MM= X-Google-Smtp-Source: ABdhPJxMNLmj0sBKUUoh8epZyHx5xl5fhxVk7XRxhf+cKJhzTnynYjzjLYxXrymsBztZem4lgBV1REgDtKsnP4YMEck= X-Received: by 2002:a05:6000:3c3:: with SMTP id b3mr30893407wrg.102.1643921288603; Thu, 03 Feb 2022 12:48:08 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Thu, 3 Feb 2022 20:47:57 +0000 Message-ID: Subject: Re: Doubts regarding the issue (bug 62181) To: Krishna Narayanan Cc: gcc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org 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: Thu, 03 Feb 2022 20:48:11 -0000 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.