Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iv3ht-0002Oq-6p for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jan 2020 18:29:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iv3hr-0003uk-Ug for pgsql-hackers@arkaria.postgresql.org; Fri, 24 Jan 2020 18:29:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iv3hr-0003ud-M6 for pgsql-hackers@lists.postgresql.org; Fri, 24 Jan 2020 18:29:23 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iv3ho-00087G-S7 for pgsql-hackers@postgresql.org; Fri, 24 Jan 2020 18:29:23 +0000 Received: by mail-wr1-x443.google.com with SMTP id y11so3163830wrt.6 for ; Fri, 24 Jan 2020 10:29:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=OpMo98a+8Wq4171039DOMIlK4RZQa8mLgAEEmh49fkI=; b=a8jWs5mqsaEkME+wVI6EMIF8DEBELge4ok6Ez5IFazVGxHX6M5LeBJ7RIhESIqZiFM k6L1uZ7q+PGOV8bbNDSvdeTDniGLF5WmdGhqi6/V7tHQZN+rKmx4788FCPRpjr7B8vj9 XjZe5nJcaZu4iVAPyhmLMUrUDJqfUNd2R3RMz0EV5A8GkLEJ36DT+bkpyiLTAs2zbLXp 5k0Tspi/oqjTihNDkOVIcuILS23HzywEJZPLmPrGNEtWjpi9YvfO9d7hkaH0CDsD2K1q /ustwY9xyAybjdiHRha08SYgZAbBCOi5tr6Q+fcYHxyK1ZXuEyVlUSxP8Xk5bCAz1ybW nQGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=OpMo98a+8Wq4171039DOMIlK4RZQa8mLgAEEmh49fkI=; b=eN0EQgis9r1PyIkSLXREnbisWj4Jeo4o71+eIvz8p8YpGBnLyZbt7o6+cjO3UmOymM 1IgZmtzLBmINGO0awN3JAXFMUCJn+FzJsWo0N7wRnwUNdZ2mPMkwfXPSDxgRGdJLFPpn m5neQmVW39jWvimHA9XDwYBWBjzxSRtaC9xk1V0TI2OWuD1H2lFKh0TdZiuun4Ry1iuV CQZZYSqDSRiRV5GUS+/Kt5r5MgVvIOjIbm1bnYSzVEfFm3P5UNaQebTViIZt8FHuxCFq cAKE6Re+TwuD3Z5HN443yp5EkMsBQi6Rx1EJ3lMGoPjyqDXeFCQcrMkE3UmeEQdks2V5 74zg== X-Gm-Message-State: APjAAAV9IRKJXsnQdGJ0oL0YlccMFpXDKRifIhkG4rZAoIAVHuo4e09z 4Njpmq+RDxF8eY73qT7ftwPl/Q== X-Google-Smtp-Source: APXvYqxJU/meZeKz+V/Un7qNfRwCLQYIP1YtvbMe4BVEquf/wXgKze7PkDayLPnC1I+dhNXc81TKOw== X-Received: by 2002:a5d:56ca:: with SMTP id m10mr5936285wrw.313.1579890558809; Fri, 24 Jan 2020 10:29:18 -0800 (PST) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id d10sm8530695wrw.64.2020.01.24.10.29.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 10:29:17 -0800 (PST) Date: Fri, 24 Jan 2020 19:29:15 +0100 From: Tomas Vondra To: Alexander Korotkov Cc: Nikita Glukhov , Oleg Bartunov , Thomas Munro , Pgsql Hackers , Tom Lane , Filip =?utf-8?Q?Rembia=C5=82kowski?= , Teodor Sigaev Subject: Re: fix for BUG #3720: wrong results at using ltree Message-ID: <20200124182915.zz7hzdkijzsb4ic4@development> References: <28639.1554565562@sss.pgh.pa.us> <5eedfd9e-2f14-a2df-3bd9-4c92eb0f08a5@postgrespro.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Nikita, This patch seems inactive / stuck in "waiting on author" since November. It's marked as bugfix, so it'd be good to get it committed instead of just punting it to the next CF. I did a quick review, and I came mostly with the same two complaints as Alexander ... On Wed, Jul 17, 2019 at 09:33:46PM +0300, Alexander Korotkov wrote: >Hi Nikita, > >On Tue, Jul 16, 2019 at 6:52 PM Nikita Glukhov wrote: >> I looked at "ltree syntax improvement" patch and found two more very >> old bugs in ltree/lquery (fixes are attached): > >Thank you for the fixes. I've couple notes on them. > >0001-Fix-max-size-checking-for-ltree-and-lquery.patch > >+#define LTREE_MAX_LEVELS Min(PG_UINT16_MAX, MaxAllocSize / sizeof(nodeitem)) >+#define LQUERY_MAX_LEVELS Min(PG_UINT16_MAX, MaxAllocSize / ITEMSIZE) > >Looks over caution. PG_UINT16_MAX is not even close to MaxAllocSize / >sizeof(nodeitem) or MaxAllocSize / ITEMSIZE. > Yeah, I'm also puzzled by the usage of PG_UINT16_MAX here. It's so much lower than the other values we could jut use the constant directly, but let's say the structs could grow from the ~16B to chnge this. The main question is why we need PG_UINT16_MAX at all? It kinda implies we need to squish the value into a 2B counter or something, but is that actually true? I don't see anything like that in ltree_io.c. So it seems more like an arbitrary value considered "sane" - which is fine, but then a comment saying so would be nice, and we could pick a value that is "nicer" for humans. Or just use value computed from the MaxAllocSize limit, without the Min(). >0002-Fix-successive-lquery-ops.patch > >diff --git a/contrib/ltree/lquery_op.c b/contrib/ltree/lquery_op.c >index 62172d5..d4f4941 100644 >--- a/contrib/ltree/lquery_op.c >+++ b/contrib/ltree/lquery_op.c >@@ -255,8 +255,8 @@ checkCond(lquery_level *curq, int query_numlevel, >ltree_level *curt, int tree_nu > } > else > { >- low_pos = cur_tpos + curq->low; >- high_pos = cur_tpos + curq->high; >+ low_pos = Min(low_pos + curq->low, PG_UINT16_MAX); >+ high_pos = Min(high_pos + curq->high, PG_UINT16_MAX); > if (ptr && ptr->q) > { > ptr->nq++; >@@ -282,8 +282,8 @@ checkCond(lquery_level *curq, int query_numlevel, >ltree_level *curt, int tree_nu > } > else > { >- low_pos = cur_tpos + curq->low; >- high_pos = cur_tpos + curq->high; >+ low_pos = Min(low_pos + curq->low, PG_UINT16_MAX); >+ high_pos = Min(high_pos + curq->high, PG_UINT16_MAX); > } > > curq = LQL_NEXT(curq); > >I'm not sure what do these checks do. Code around is uncommented and >puzzled. But could we guarantee the same invariant on the stage of >ltree/lquery parsing? > Unfortunately, the current code is somewhat undercommented :-( Anyway, I don't quite understand why we need these caps. It kinda seems like a band-aid for potential overflow. Why should it be OK for the values to even get past the maximum, with sane input data? And isn't there a better upper limit (e.g. based on how much space we actually allocated)? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services