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.96) (envelope-from ) id 1wZNQk-000zUk-1U for pgsql-bugs@arkaria.postgresql.org; Tue, 16 Jun 2026 06:37:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wZNQi-00Flyc-2w for pgsql-bugs@arkaria.postgresql.org; Tue, 16 Jun 2026 06:37:48 +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.96) (envelope-from ) id 1wZNQi-00FlyH-1s for pgsql-bugs@lists.postgresql.org; Tue, 16 Jun 2026 06:37:48 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wZNQf-00000000g02-2i2J for pgsql-bugs@postgresql.org; Tue, 16 Jun 2026 06:37:48 +0000 Received: from [10.0.2.15] (unknown [130.41.208.2]) (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 4gfcjz23tlz49Q32; Tue, 16 Jun 2026 09:37:43 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1781591864; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eIpHUaAjLE9FKuNfIZmPU+zysm318eEXDI0T9nSF5p0=; b=YAiba00floLHT8x2hvOoEjvg86B/lGgDNKawA1GvPulXv03sH7iyQPUB5Fm8tn5RHdtKWs MhqYPVDkhgl45Refch20R/9xboKigN6lICAEaxyMbH95XfhlpiobMejTUs575aQinXIcS0 4d0h3Q5OqBnJE3TMXjpqUVfP8vDEpIGIo8VdO5QkTVQPMdX+Nf5Mca9eSarE+Atn/ZBWIh D2vHU5NOA8o0VMp7SW5GOl21d1wwWQzZ24xNu4uOG9IY9mdd+IB30AtYTTmB1PSME+6HBX hTiKyvsYVwd+sy5H+SKESecytOAY2XmtYmsy3iS+r+kgYVZKjhlsyC9HIj6VGA== ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1781591864; b=tyeaA1xW6PAwg6cmpZL7D/HXcOEzz2KISrasJn34Mt46o8PJkU8J6e2nfVP2MeQErc/Whk YO+M3ht9vOF04VcAGicdbZ/EKEhBzgtK4OSoC55WGuMk/PoPjFGssSL8GHy5dHEmbIHqdn E6L5Pb2uFC06MxXWzonSgOu2IKWLIq2w6odf8DZ/R+BEGLPkRIKmiERrjbJ9f6IGjqd7Nv FoLcMfJfuctGfvxyeXpFP2ozJC84CdEQwawvVeb28OHtjBEjXj7MEJ743JoTrJqt4wQJcZ NambaNfAVl+oNco7LkEhrj3RwYcYAOW5Z8P9ugmOu9clXCWK+Tx9cL35S1KbAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1781591864; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eIpHUaAjLE9FKuNfIZmPU+zysm318eEXDI0T9nSF5p0=; b=tTE6idJNRLVId0PyiKxA4otmqduK0Fp+JC7lvEiLk98z0pF0gb2D/p17Tr9aNilfCxKQOD F9PFYbnIC1JkjM1wpFqzWRGTgfMudBStHPSrXzFykH5NawObKQFfz8JGNWnt3rHqQdW/47 aqkf+80LIuN5umGTFuBuRioGMg3wGUNA0oJH5MykWGT4tgJqzaJrdTjvqIPNN0o5ZbOKIR w2erqbPKq6LAEIFeRwXhbeqwh6uDZTYASrewXzNVESC2aPKFUmbGdI1Hmm4ogDZLGcqUiU gCZmFFr5lb1YvM83bUnFOuU9wTmDLdR7aFo0bTLYkl+SI6aFK//Mw20PDDn3FQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: <965933b9-bba2-42d0-8c9a-6b1727728a55@iki.fi> Date: Tue, 16 Jun 2026 09:37:42 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Fw:Re: Fw: ltree_compare in contrib/ltree/ltree_op.c overflows int32 on deep ltree comparisons, returning the wrong sign To: Ayush Tiwari Cc: =?UTF-8?B?546L6LeD5p6X?= , pgsql-bugs@postgresql.org References: Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 15/06/2026 18:24, Ayush Tiwari wrote: > On Mon, 15 Jun 2026 at 20:38, Heikki Linnakangas > wrote: > > On 13/06/2026 09:12, Ayush Tiwari wrote: > > This looks like a classic case of integer overflow that's > > happening in ltree_compare function in ltree_op.c. > > > > return (al->len - bl->len) * 10 * (an + 1); > > return res * 10 * (an + 1); > > return (a->numlevel - b->numlevel) * 10 * (an + 1); > > > > I think the calculation should be done as int64, something of > this sort: > > Yeah, that works. However, I note that the multiplication is only > really > needed by the ltree_penalty() caller. All the other callers just check > if the return value is less than, equal, or greater than zero. It feels > a little silly to do all that work of multiplication and clamping for > those callers. And for ltree_penalty(), the caller actually converts > the > return value to a float, so clamping it to int32 range feels a little > silly for that too. So I propose the attached, which splits the > ltree_compare() function into two variants: one for ltree_penalty() > that > returns a float, and one for others that don't care about the > "magnitude". It duplicates a little code, but I think it's easier to > reason about. What do you think? > > > I had thought initially of using the method you have added, > (not with float return-type though, I thought of planning to create > a duplicate function with int64 type just for ltree_penalty(), but float > type > is better) noting the same thing that rest callers just care about > comparison with 0. > > But thought backpatching it would be harder, hence I used the int64 > method. However, your patch looks much better than the ugly > behaviour and I agree it did not make sense to do those > multiplications at all other comparison functions. Great, committed. Thanks! - Heikki