From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sonic302-19.consmr.mail.ir2.yahoo.com (sonic302-19.consmr.mail.ir2.yahoo.com [87.248.110.82]) by sourceware.org (Postfix) with ESMTPS id C357A385800F for ; Sat, 27 Mar 2021 02:26:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C357A385800F X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1616811962; bh=pafKJ9zPd76wdbNoyvKfjUEO30kPzNDVDY/64SVCrwg=; h=X-Sonic-MF:To:From:Subject:Date:From:Subject; b=GZYnY7wlEc6wGNYGew2c5/LeYRC9/CNcPEaAXPmpSWyMyIgIJK2o8xfhoiqY6ZdOqUYrLPQ16WPrmrwlhaQXmmRMjwUUYQQ6+VipvTlbMWNtK+DHR1Uj1ujhMHfdOb1l/RVfDhjnGbaHsxbtt78eosMo2pzsXNi/9FgZmWSLTFPwpSQD0KjMfaAT3lAQpCb7FDXlZ+HsGxeAxMJXMaQA490x/K/AZMZc35i6tswI4xxZ0cc6Z63HzWFuU0yR0oKCpJG3w6qL+Dq6UPtZxtnS85WWjngXqG/BqOnNCRdB6ByHWN7g8Cg1bQ1qrbazYZMHnCZWWTDuiVhTHUGTAQzQDw== X-YMail-OSG: U58hY6oVM1lAhjSQdK_23Xo32z3NMu3W1aRJDSMQNABFLyipb.zIVIC4Vgvapix SZc2wDHFna1Wm1qIFqVBvh4e9f8vc5pDMrDLGnn5CWua7z9xDzA.syGfwjG8Di6_I_iWqX4L1DOI hAYCT3SGzIT.F.UgdkFjsASl3PH8pJC9jdqUII3noURC50tUp9lHvfFGTCBwuD3jkWaSWyaAvdHZ W_XcgfFV_DS8sY2Y.pvEHTx9RUPPtia8b8xmRGsK80JHFKkS7c2xBJDLyc28sjE2mIVrSS6FFZux DxY9muBf6HrZ3Ohb8LOoUf8uQiGO7q4OeCxhWeGd7iQorZ3L44BLhk.OmhVAeUWv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ic-MF: Received: from sonic.gate.mail.ne1.yahoo.com by sonic302.consmr.mail.ir2.yahoo.com with HTTP; Sat, 27 Mar 2021 02:26:02 +0000 Received: by smtp440.mail.ir2.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID a94ab410f93f56373fac41458c6ef258; Sat, 27 Mar 2021 02:25:58 +0000 (UTC) To: libstdc++@gcc.gnu.org From: tika Subject: Questions about std::string::operator=(char) Message-ID: <7eeb24bf-58cc-5316-807e-64434ecf4b83@yahoo.fr> Date: Sat, 27 Mar 2021 03:25:56 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 Content-Language: en-US References: <7eeb24bf-58cc-5316-807e-64434ecf4b83.ref@yahoo.fr> X-Mailer: WebService/1.1.17936 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Apache-HttpAsyncClient/4.1.4 (Java/11.0.9.1) X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Mar 2021 02:26:07 -0000 Synopsys:?? assigning a char to a std::string makes a string 1 char long, but what happens if that char is zero??? And showing how easily can this UB pass undetected through unit testing... The story:?? I created a stupid bug, that my unit testing did not catch....?? And ended up spending more more time debugging the test than fixing my bug. The bug itself is kind of boring...?? Using the return value of int snd_get_name(int, char*) C function from the alsa library, and putting an int into a string. That's what I think could be a rather common mistake. Here's my piece of code, the various tests that could be applied (and most fail to detect the bug in my code).?? It turns out that assigning a null character to a std::string breaks a lot iof invariants we usually depend on.?? All of gcc 10.2.1 (with libstdc++-10.2.1-9), clang 11.0.1 and icc 2021.1.2 fail to warn of the mistake with -Wall, for gcc at least, that is both both on my system (fedora) and on godbolt.?? MSVC does a lot better, with both warnings and different outcome, with str = 0 (correctly ?) resulting in an empty string of length zero. I know it is UB, but I'm pretty sure the fact that this can go undetected so easily by the most common unit testing methods makes it an interseting one.?? Is it a feature? Is it something that is part of the standard's private UB collection? What does the standard say about expected invariants such as s == s.c_str() ? And about the contract of std::basic_string::operator(Char) ? So, here's some code that show the issue and how stcky it is... You can also find it here: https://godbolt.org/z/cTWGWohv3 #include #include #include // ------------------------------------------------------------------------- // simulating what is the C driver FYI, it's the alsa sound driver, // but that's not much relevant char*some_text =nullptr; voidinit_text() { ?????? some_text =strdup("some text"); } intget_card_name(int,char** x) { *x =some_text; return0; } // end 'driver' code // ------------------------------------------------------------------------- intmain() { ?????? init_text(); ?????? inti =0; char*name; ?????? std::string s; // my original bug... &"??! happens. ?????? s =get_card_name(i,&name); // Should have read: // if (!get_card_name(i, &name)) // s = name; // simulating EXPECT_FALSE(s.empty()); ?????? std::cout <<"s.empty() : "<