Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1saFQK-007QtV-4s for pgsql-hackers@arkaria.postgresql.org; Sat, 03 Aug 2024 14:07:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1saFQG-005iGP-OW for pgsql-hackers@arkaria.postgresql.org; Sat, 03 Aug 2024 14:07:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1saFQG-005iGG-A8 for pgsql-hackers@lists.postgresql.org; Sat, 03 Aug 2024 14:07:52 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1saFQD-002wRD-Ck for pgsql-hackers@postgresql.org; Sat, 03 Aug 2024 14:07:51 +0000 Received: from [192.168.1.110] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4Wbl026ZF3z49Pv5; Sat, 3 Aug 2024 17:07:46 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1722694067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RCgUPQBt/Y8Hol//djvs3rjUUFfo1nzRGC7ZApE8/pA=; b=Js9Mlr7udAhxdN/jqLIBMflNOkidChQa4Web62GBEv1AqydFTMawGk8zbRSwHLbrALTxlg ywsf7y6B4gN7+oG1tyvZohyl/Y8ZUB4tMbJ5PcDeAJZruDxQQn3iNeQqpM+rGSkA9rOuFk ujuci3hrMZ0nQZiJ2ZuLV2vcfAM1QWeRbv3DBwhyy+by7jTGTlVsVsXgLhrpwVylJF6Ag5 jSNLukMmmln33GjWCIuXBrEMRJ3zesi/AUsd9kPkNShuEr2gI+c5Wp1N7K0T0zrjydCH9b bVknrHTldeXxvj0nvBrgcvq+IqLRPeQYMb2xMDOgmcZPdofUGA9ppdeHahUz0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1722694067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RCgUPQBt/Y8Hol//djvs3rjUUFfo1nzRGC7ZApE8/pA=; b=ZJoZjfPXBr7Gs2ym/AydGWYWi3UxvOXs6NTajw6342xHPIWupx38rXj7U4/iQ2VCyK9g/F Hz5FH0j/Q14oSwiEgnMulCnYzXNTCuc79649Ae8lmfuk6gGrBMA90jZO4fjZ0fGGPne4gi hFZJw/337v2dsdzBcNAUIjTa7JOAPFzxAFE7GsGG5c8WEms2YhUR+vCSbIYvsGXlgZAAwZ +2GbTPgrKJFZlliHR4viz57pjTvd+mQ4gYmiTV3VkMWxDJOgifwt3k3CeGWIhIU6NoZEo4 +HNP+uOKk22Mz9VS7RwDbZH/dXoeap5iBvaa5iilxiaBk3FGO7PWahs2+zvfFA== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1722694067; a=rsa-sha256; cv=none; b=g86HG7/RJgfVj+mTGweQn+5W5UtcyXXQTuG3VfXeX/HawIwM1Yh2NX816ps61EcVyhinW6 avOwBwbpjQxDlBIqMO4IINTKXvWmytGxEovzekDlBGv6g3ckEuJ+cTOi8Kea8YpBSiLcG1 FAi/8/w7a+mCK/WYS/3CYu1BwKyOeEZHv16ss/10ZG7n9pbsHqwDu4n5ka6OfWHv1xkaev je6i/eR9mST6SyF6LgmMFa4GYDR0dcIJzZ/n++m7IBLeuxiPuWmFK598zPY6n3MasHrVTq Zxvr/lVlHgY1c1RgU+0ZXHqzjMjVKsXkdI65vbKtvdxblf09uARZqayl5+ghuA== Message-ID: <0d3b4dfa-afa2-4f1c-9b30-e2acbdeb2e64@iki.fi> Date: Sat, 3 Aug 2024 17:07:46 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fix inappropriate uses of atol() To: Peter Eisentraut , pgsql-hackers References: Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 03/08/2024 14:04, Peter Eisentraut wrote: > I noticed (during [0]) to some uses of the function atol() seem > inappropriate.  Either they assume that sizeof(long)==8 and so might > truncate data if not, or they are gratuitous because the surrounding > code does not use the long type.  This patch fixes these, by using > atoll() or atoi() instead.  (There are still some atol() calls left > after this, which seemed ok to me.) > > In the past, Windows didn't have atoll(), but the online documentation > appears to indicate that this now works in VS 2015 and later, which is > what we support at the moment.  The Cirrus CI passes. +1 except for this one: > diff --git a/src/interfaces/ecpg/preproc/ecpg.trailer b/src/interfaces/ecpg/preproc/ecpg.trailer > index b2aa44f36dd..8ac1c5c9eda 100644 > --- a/src/interfaces/ecpg/preproc/ecpg.trailer > +++ b/src/interfaces/ecpg/preproc/ecpg.trailer > @@ -217,7 +217,7 @@ char_variable: cvariable > enum ECPGttype type = p->type->type; > > /* If we have just one character this is not a string */ > - if (atol(p->type->size) == 1) > + if (atoi(p->type->size) == 1) > mmerror(PARSE_ERROR, ET_ERROR, "invalid data type"); > else > { > In principle you can have an array larger than INT_MAX. However, this is a pretty weak test anyway. I think this is what the error is meant for: EXEC SQL BEGIN DECLARE SECTION; char connstr; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO :connstr; This also produces the error, which seems fine: EXEC SQL BEGIN DECLARE SECTION; char connstr[1]; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO :connstr; This also produces the error, which does not seem good (if you replace 1 with 2 here, it works): EXEC SQL BEGIN DECLARE SECTION; char connstr[1+100]; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO :connstr; You can work around that with: #define LEN (1 + 100) EXEC SQL BEGIN DECLARE SECTION; char connstr[LEN]; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO :connstr; The grammar currently balks on arrays larger than INT_MAX, giving a "syntax error", which I don't think is correct, because at least my C compiler accepts it in a non-ecpg context: EXEC SQL BEGIN DECLARE SECTION; char connstr[2147483648]; EXEC SQL END DECLARE SECTION; EXEC SQL CONNECT TO :connstr; Overall I think we should just leave this as it is. If we want to do something here, would be good to address those cases that are currently bogus, but it's probably not worth the effort. -- Heikki Linnakangas Neon (https://neon.tech)