Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ks8H2-0002n7-MD for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Dec 2020 17:50:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ks8H1-0005zM-Jp for pgsql-bugs@arkaria.postgresql.org; Wed, 23 Dec 2020 17:50:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ks8H1-0005zF-Bi for pgsql-bugs@lists.postgresql.org; Wed, 23 Dec 2020 17:50:07 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ks8Gy-0000Yy-Ko for pgsql-bugs@lists.postgresql.org; Wed, 23 Dec 2020 17:50:06 +0000 Received: by mail-lf1-x136.google.com with SMTP id s26so41898941lfc.8 for ; Wed, 23 Dec 2020 09:50:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=FUoG8Bnwp3ZjBXi6c7j5QGfcIO2+z6zg7hUy6urwZdY=; b=WaVWEkXt34+A62zXG1tc8REY+9biWmO+XeJLHyIoRBSTGYut6CAgdxBKdttSB7o8gd WeqPUmDULdktlbu7PNzP3KEtURnl/CG8knhPNkRui794ukFTNczh3GJTtrYoxW2836Be +9YNYm/8VFNtRQE1/3X39WZ9sSklxFNCBcE64qXdPM2Po4EX3rPv9VWQ7GUs/HapBJPN 2xJKF6PeeHbnHeCpeiNo2C9lwIZB3TWygK1FlD6L5maCHOtE9HyuAGjLiXTmg8HRVUPZ PamvJ85p2+Bn0/XJ5NFSHfOD8KzkwSyCrgeB26sNiLec+MRiMAV/uryjjeXtoNVFz/oV izTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=FUoG8Bnwp3ZjBXi6c7j5QGfcIO2+z6zg7hUy6urwZdY=; b=EY2Nu3yt9IR+DEkUlg0mJNt/88WVVvFBpv5MZfVTxHutoqUMIJcr5PN5r/h0u1SSWo 0qLBnQoqTeNkTK0ms+wLXRsc0CGUhu+RXFu1cxbIQjxuuGfRNctGJ5bST12r0G66uMH2 RqgSgpM1QupyoLaGZNNJMxcJjKgOBngs//IQ4ma5DsADt61R3Ij1Xlsq7eIluU83LTgS KEmSzQ6jKmPU05ni5/u6EYLhtnZM30jJNw9c2wxtL6hXkmLpOci/AN5iRvlWWx87RfRL q8Ix7Vq9XqA9gvPLSg5vFykWd9Ne71uNTXn+hemEyA14aEkVQDfRy0kOp6JjkSMKUM4N c/Dg== X-Gm-Message-State: AOAM531PoWoYMg7mGFyuXaxw2cSsQXTnwAOfbbePBX1ELQC2h1Vzfkqx 2A1bkfYiXYRlcFqmcYdVJ9K43609NsBwWw== X-Google-Smtp-Source: ABdhPJyC84q5gfxeED3v/zmDrwDBT9RoAqxCNq7HudB4EZj3y0ly9gkcDT29yHhKqM97B+Jprl4/Yg== X-Received: by 2002:ac2:5dd1:: with SMTP id x17mr10405988lfq.252.1608745802752; Wed, 23 Dec 2020 09:50:02 -0800 (PST) Received: from [1.0.0.7] ([178.155.6.51]) by smtp.gmail.com with ESMTPSA id c10sm3283819lfh.258.2020.12.23.09.50.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Dec 2020 09:50:02 -0800 (PST) Subject: Re: BUG #16790: Integer overflow not detected with << To: Tom Lane Cc: pgsql-bugs@lists.postgresql.org References: <16790-c2a81c92a8776c5b@postgresql.org> <710063.1608737396@sss.pgh.pa.us> From: Alexander Lakhin Message-ID: <17340682-ad3e-9145-543a-27a62518357e@gmail.com> Date: Wed, 23 Dec 2020 20:50:00 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <710063.1608737396@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 23.12.2020 18:29, Tom Lane wrote: > PG Bug reporting form writes: >> It seems that the bitwise shift left operator ignores overflow: >> Is this an expected behavior (a kind of UB)? It's not like an arithmetic >> shift. (The semantic of '1::int4<<-1' is questionable too.) > It's defined to do whatever the C << operator does. That will certainly > not involve throwing an error. Unfortunately, the behavior of the C << operator varies from defined to implementation-defined and undefined. In fact I found this issue when executing that query in an instance built with "-fsanitize=undefined". Also I found an interesting reading about the shift operator: https://devblogs.microsoft.com/cppblog/hello-arm-exploring-undefined-unspecified-and-implementation-defined-behavior-in-c/ So it's not that simple to describe the expected result of the operator for all cases/platforms/architectures. It seems that a user is left with "just try and see" or "avoid illegal values" approach. On the other hand, there are pg_{mul,add,sub}_s{16,32,64}_overflow() functions. Aren't they purposed to provide a well-defined behaviour for related arithmetic operators? Maybe the bitwise shift can be thought as something different from arithmetic shift, and i<<1 is not the same as i*2, but in my opinion it's not that could be expected at the database level. Best regards, Alexander