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 1sGU92-00Ddh1-3H for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 01:48:24 +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 1sGU8z-007BJ6-Sa for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jun 2024 01:48:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sGU8z-007BIx-BK for pgsql-hackers@lists.postgresql.org; Mon, 10 Jun 2024 01:48:22 +0000 Received: from mail-il1-x12a.google.com ([2607:f8b0:4864:20::12a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sGU8t-000YL4-5H for pgsql-hackers@lists.postgresql.org; Mon, 10 Jun 2024 01:48:20 +0000 Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-3758fa1cc8eso8362545ab.3 for ; Sun, 09 Jun 2024 18:48:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717984094; x=1718588894; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=gZ/Ypv6z2+q3LQFAqwuTHVa58rTAYyf9g/Kvv1WGzDw=; b=kHkylIMB58ylHVVj8/7ofhbRX2YW74XTzUvz7+MijR7yM8+RPRzEEy6/oJJDBL4rct w4UreiGdPkWkwQ6xTuU5vTV8YrCPbBWCG9rF4ObX5mCU1j8X7Ffpp6yH3MuwvwSsnThp JOzmPKjhBTyHFbH3h2TWZBEW1SG0R5orJ/EzJngU7XkdsVAOW2pQNpYYBm2CpqOpzCnl FDe8LbsORlU/+oV1Bp+w6FKttgiPskfIYCRL/g4MmGV4ZMMNtF58j5h1JJcnD++hPlVn 9OBeBbyZuF6KOvNTdwc40b9+Khhr/yAEI07gK1Pbz2F0+f27N6+13cYFg4P8KQwR5E1z ouPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717984094; x=1718588894; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gZ/Ypv6z2+q3LQFAqwuTHVa58rTAYyf9g/Kvv1WGzDw=; b=TSM98d1kU1R1XFplhOTDgtDTZfygY1YYDIm/Jvvnq1t3JuCp+Tq+5drPPTJw5BX2LI n4ap5idiQu2fZvb/Yl3NHhV9fOy0JiN7+H7RCff5u7znK06xeobXkfn6WXG5WBYqSeF/ YoNKMS29DanlMHWKlTFRJcUONzs5LidkYYIXabsOnQJrYteZBvTahRJvrD1EIZagASZj Ia1Icfkp5AwLFus5HeBoKLpa/qHWG/6O7MytdrcBA6d2t55LphtC87DgDJ4QbeJJwX0t r3gTM8ibEAHJh2PTuaNjke61zfnf8BkPYr19y/Q9pNqNqcaSUCLZ/8K6+TiLEWxNERs4 gwuQ== X-Gm-Message-State: AOJu0YwProNMcLNHFHKzVBzcFSlci30TRbnW4a8QrfivMsFD0Vksadg1 x8oitL/bscqWqEdl2+lWn1HC3UKzs1gfT1UIr69AZXhlz4wLH6gO X-Google-Smtp-Source: AGHT+IEkbbvmNmqQLBf9AncOA9rSYmjLLgD+SFGDnydBjykQMvY/+pu7mOqsoGtgQcea2wvuX6kmyA== X-Received: by 2002:a05:6e02:1c49:b0:374:ada1:295a with SMTP id e9e14a558f8ab-3758032dec5mr112886285ab.16.1717984094223; Sun, 09 Jun 2024 18:48:14 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-37594a02e33sm11106235ab.76.2024.06.09.18.48.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jun 2024 18:48:13 -0700 (PDT) Date: Sun, 9 Jun 2024 20:48:12 -0500 From: Nathan Bossart To: Joseph Koshakow Cc: PostgreSQL Hackers , Andres Freund Subject: Re: Remove dependence on integer wrapping Message-ID: References: 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: Archived-At: Precedence: bulk On Sun, Jun 09, 2024 at 04:59:22PM -0400, Joseph Koshakow wrote: > I originally added the helper functions to int.h thinking I'd find > many more instances of overflow due to integer negation, however I > didn't find that many. So let me know if you think we'd be better > off without the functions. I'd vote for the functions, even if it's just future-proofing at the moment. IMHO it helps with readability, too. > The following comment was in the code for parsing timestamps: > > /* check for just-barely overflow (okay except time-of-day wraps) */ > /* caution: we want to allow 1999-12-31 24:00:00 */ > > I wasn't able to fully understand it even after staring at it for > a while. Is the comment suggesting that it is ok for the months field, > for example, to wrap around? That doesn't sound right to me I tested > the supplied timestamp, 1999-12-31 24:00:00, and it behaves the same > before and after the patch. I haven't stared at this for a while like you, but I am likewise confused at first glance. This dates back to commit 84df54b, and it looks like this comment was present in the first version of the patch in the thread [0]. I CTRL+F'd for any discussion about this but couldn't immediately find anything. > /* check the negative equivalent will fit without overflowing */ > if (unlikely(tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1)) > goto out_of_range; > + > + /* > + * special case the minimum integer because its negation cannot be > + * represented > + */ > + if (tmp == ((uint16) PG_INT16_MAX) + 1) > + return PG_INT16_MIN; > return -((int16) tmp); My first impression is that there appears to be two overflow checks, one of which sends us to out_of_range, and another that just returns a special result. Why shouldn't we add a pg_neg_s16_overflow() and replace this whole chunk with something like this? if (unlikely(pg_neg_s16_overflow(tmp, &tmp))) goto out_of_range; else return tmp; > + return ((uint32) INT32_MAX) + 1; > + return ((uint64) INT64_MAX) + 1; nitpick: Any reason not to use PG_INT32_MAX/PG_INT64_MAX for these? [0] https://postgr.es/m/flat/CAFj8pRBwqALkzc%3D1WV%2Bh5e%2BDcALY2EizjHCvFi9vHbs%2Bz1OhjA%40mail.gmail.com -- nathan