Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gCRrX-0000Np-C9 for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Oct 2018 16:06:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gCRrT-0004Mx-T1 for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Oct 2018 16:06:23 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gCRrS-0004M8-4s for pgsql-hackers@lists.postgresql.org; Tue, 16 Oct 2018 16:06:23 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gCRrN-0006ut-Th for pgsql-hackers@postgresql.org; Tue, 16 Oct 2018 16:06:20 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id CDDB321EC3; Tue, 16 Oct 2018 12:06:15 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Tue, 16 Oct 2018 12:06:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm2; bh=VGHu4oBumXqnBc97jXDEf86d7dl mAvBRrQKNgjarGB8=; b=mB/ChYg36xGCIJCxuvgy1PwR6kYXTUrDIjhdaa3musw udBp/BvzC+gU/R/DT/HWnOQJft0IFpjaih3wEBZSmDakuRq1455Husu6sY1KWIXg v/ub+uVSr8Q+QFMydF/KtFKs/VOmZBkhiVrpYbNTtgt5Q+ei4o5s8vVQU/6nGqgZ mgMhIUcmy9IhPwBAL5RHiaDcxwJTfR2htMVklJuKCZ4jsoBFqFDkGmi6mcuw2Dru gMA04145w3d1Zi33vRnOllwN8OS//GU68v0Lvoo0PM+06NPdiVTQLV15vVOwaFwz mZK+cF7OCXWJc/9RbjVv3HifURyv0mkvCXcZZnugBEg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=VGHu4o BumXqnBc97jXDEf86d7dlmAvBRrQKNgjarGB8=; b=yO0n/lmMlrQwyAhruY2F9m 6rNLSZPBtdlAIAEn2lehEjilYx3YZ2d+lKwSqumrT7T8kyzYxIhZVdvrkxRPSNgz XgJiT5xOrtH2PSMUcrt5RK7ZL+fP1ooUPOjyrE4HSVTkONucnQEv5iI2aOpClYIF YDSsnNc01gIRSg9hfhZkeTR6VhKaMEtCnfgIryA7Ta211KgydHMBrbXyRe4x6nvJ ZuXtwdnmaBVytVfjdBvAxpboQCBg6lNt5HtDC7jlXz7xJT7M0FFdyzjC5Ku6ZAn3 QE90bRmXAAK6COPbRplOxL/+2Ghmo2qCsEfJY71CdOgNgjxaKQWHkE8tgRUHlxJw == X-ME-Sender: X-ME-Proxy: Received: from intern.anarazel.de (c-98-210-140-171.hsd1.ca.comcast.net [98.210.140.171]) by mail.messagingengine.com (Postfix) with ESMTPA id D6D11102F0; Tue, 16 Oct 2018 12:06:14 -0400 (EDT) Date: Tue, 16 Oct 2018 09:06:12 -0700 From: Andres Freund To: Robert Haas Cc: "pgsql-hackers@postgresql.org" , Thomas Munro Subject: Re: Large writable variables Message-ID: <20181016160612.eand666kosvv32cc@alap3.anarazel.de> References: <20181015200754.7y7zfuzsoux2c4ya@alap3.anarazel.de> <20181016045051.wr35ubb6h7h3ee2b@alap3.anarazel.de> <20181016050200.c65yjz2qgxulgouv@alap3.anarazel.de> <20181016063013.kigj6e5pqx6n75y3@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018-10-16 11:22:31 -0400, Robert Haas wrote: > On Tue, Oct 16, 2018 at 2:30 AM Andres Freund wrote: > > This just reminded me that a couple times I wanted a cast that casts > > away const, but otherwise makes sure the type stays the same. I don't > > think there's a way to do that in C, but we can write one that verifies > > the cast doesn't do something bad if gcc is used: > > > > #if defined(HAVE__BUILTIN_TYPES_COMPATIBLE_P) > > #define unconstify(cst, var) StaticAssertExpr(__builtin_types_compatible_p(__typeof(var), const cst), "wrong cast"), (cst) (var) > > #else > > #define unconstify(cst, var) ((cst) (var)) > > #endif > > > > Does anybody besides me see value in adding a cleaned up version of > > that? > > Under what circumstances would we consider this to be a legitimate thing to use? When the variable actually *will not* be modified, but language or API design reasons makes it unfeasiable to express that. Look e.g. DestReceiver * CreateDestReceiver(CommandDest dest); some of the returned receivers (e.g. donothingDR, printsimpleDR) are statically allocated and *any* modification would be a bug. But other return values will be modified, e.g. CreateIntoRelDestReceiver(). It's safe to cast constness away if the variable will not actually be modified after. Which is e.g. the case above. But making the static allocations const will a) save memory b) trigger sigbuses if you modify them. So the casting constness away here *increases* robustness. The problem is that just adding a cast like case DestNone: return (DestReceiver *) &donothingDR; also hides errors. If you e.g. changed the type of donothingDR you'd still not get an error. So I was wishing for a form of a cast that only casts the const away, but errors out if there's any other type difference. That's the above, I think. > I think if we add something this, we'd better accompany it with some > detailed and very clearly-written statements about when you're allowed > to use it. Otherwise, I predict that people will use it in cases > where it's not actually safe, and we'll end up with low-grade bugs. Well, right now people will (and have) just cast the const away like above. So I don't really see it being more likely to cause problems than we're doing now. But yea, it definitely should have a big red warning label. Greetings, Andres Freund