From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) by sourceware.org (Postfix) with ESMTP id BA7D9385841C for ; Fri, 26 Jan 2024 20:08:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA7D9385841C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gentoo.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gentoo.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BA7D9385841C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:ea4a:1:5054:ff:fec7:86e4 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706299690; cv=none; b=Y7U3uYuNzBdzkMCeYNpat7mKSOPHNMyVO+rt3Q9zi9rTa2hcPS9MlyrnNoATxvjzmPu7JMqiYZz1VjM8KX+CiOha8yIT96Z6EP40Zddx4BakVhKyXwd9xscSOb8nc1bExpnbIe8KcrXSHSziAxyqX5C+HzqGJnrbQ+avq3d/Vbc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706299690; c=relaxed/simple; bh=jtZqnrS71WdvZvPYcnpNYT+u3Kednyf7qQhdYQbJkqQ=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=bSlf1t6drBgMSZpQttW4+wcYZAszC6E1kJFf7BLGvvAjQWgBGkm2GUZ34FHFZXesqkOnL07yKob9HpNTlFsjiekRFurvpkL5hqfTTwIGvpYkVWaS3mjRmUdWMyeZFydSOAyUVecJnUE5uqbvNes0tZ7bc0vrT0jdK3vRgideA0g= ARC-Authentication-Results: i=1; server2.sourceware.org References: <87fryk5fyy.fsf@gentoo.org> <87fryk2ehc.fsf@gentoo.org> User-agent: mu4e 1.10.8; emacs 30.0.50 From: Arsen =?utf-8?Q?Arsenovi=C4=87?= To: Xi Ruoyao Cc: Alejandro Colomar , libc-alpha@sourceware.org Subject: Re: free(3) const void * Date: Fri, 26 Jan 2024 21:07:08 +0100 Organization: Gentoo In-reply-to: Message-ID: <87cytn2705.fsf@gentoo.org> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_PASS,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Xi Ruoyao writes: > On Fri, 2024-01-26 at 18:22 +0100, Arsen Arsenovi=C4=87 wrote: >> >> Alejandro Colomar writes: >> >> > [[PGP Signed Part:No public key for 9E8C1AFBBEFFDB32 created at 2024-0= 1-26T16:35:04+0100 using RSA]] >> > Hi Arsen, >> > >> > On Fri, Jan 26, 2024 at 03:24:29PM +0100, Arsen Arsenovi=C4=87 wrote: >> > > But, free() modifies the object passed to it (even if not its bit >> > > representation) by freeing it.=C2=A0 Freeing const-passed objects wo= uld also >> > > violate the constness promise, so I disagree that free should take c= onst >> > > void*. >> > >> > This is an interesting interpretation.=C2=A0 Is expiring the lifetime = of an >> > object a modification of the object?=C2=A0 Possibly. >> > >> > But, the standard says: >> > >> > If an attempt is made to modify an object defined with a >> > const-qualified type through use of an lvalue with >> > non-const-qualified type, the behavior is undefined. >> > >> > Even if you consider expiring the lifetime of the object a modification >> > of the object, the part that says "through use of an lvalue with >> > non-const-qualified type" is not fulfilled, IMO.=C2=A0 That would reqi= re >> > dereferencing the pointer, to actually get the lvalue, which free(3) >> > never does. >> >> What 'free' precisely does is outside the bounds of the standard, >> though.=C2=A0 We can assume it is permitted to so since nothing says >> otherwise. >> >> But, besides that, what I mean by 'constness promise' is that an object >> must be usable following a const usage of it as if that usage never >> happened.=C2=A0 This would certainly not be true of 'free', whether it >> dereferences or not.=C2=A0 I am not sure if this is a formalism of the >> language definition, but it is something people (and AFAIK compilers) >> rely on significantly. > > In C we (not sure about the people, but at least the compiler) cannot > rely on it at all. It's perfectly legal to write something like > > void > stupid (const char *c) > { > strcpy ((char *)c, "some bullshit"); > } > > int > main (void) > { > char buf[100]; > stupid (buf); > puts (buf); > } > > Yes it's as stupid as the name of the function. But it does *not* > invoke any undefined behavior, and so the compiler is not allowed to do > any optimization assuming "stupid" won't change the content in buf. > > That's why GCC has invented __attribute__ ((access (read_only, ...))). > The documentation of this attribute even says we cannot rely on the > const qualifier: > > The read_only access mode specifies that the pointer to which it > applies is used to read the referenced object but not write to it. > Unless the argument specifying the size of the access denoted by > size-index is zero, the referenced object must be initialized. The > mode implies a stronger guarantee than the const qualifier which, > when cast away from a pointer, does not prevent the pointed-to object > from being modified. Examples of the use of the read_only access mode > is the argument to the puts function, or the second and third > arguments to the memcpy function. Ah, another regrettable bit of C to remember. Thanks for sharing - I'll keep this in mind. Have a lovely night! =2D- Arsen Arsenovi=C4=87 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iIcEARYKAC8WIQT+4rPRE/wAoxYtYGFSwpQwHqLEkwUCZbQRChEcYXJzZW5AZ2Vu dG9vLm9yZwAKCRBSwpQwHqLEk71lAP47ic77VGvthuo8yH/QgW5CukgGB6MzO9Xd o3erW3WWbgEAy22+pgM7fCYY83qUgRYKZJ++uZXX+k4zlSmRBxH1rwA= =qNfH -----END PGP SIGNATURE----- --=-=-=--