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 1vieN6-00CH9F-1V for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jan 2026 20:00:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vieN4-008vx5-1R for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Jan 2026 20:00:06 +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 1vieN4-008vww-0G for pgsql-hackers@lists.postgresql.org; Wed, 21 Jan 2026 20:00:06 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vieN2-001mD4-16 for pgsql-hackers@postgresql.org; Wed, 21 Jan 2026 20:00:06 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-4801d1daf53so2490225e9.2 for ; Wed, 21 Jan 2026 12:00:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769025603; x=1769630403; darn=postgresql.org; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=sDQD4V+AOzYOfPpKHxDcdRY1BpjrYO1gFfE5IyB0cjg=; b=Az3HWU7m4msMVKn4dD3NUGLrhF7foJhXced7HPHENhsjAIVuRHBsO5mwLBzhzyLbaH gSf4pZyELzcgbLrSpqn+OfJHcrLIGgvpVxSbxh2f5ZXJtRp5KiEy4SfMA00yZ2iMhTeb hGs+QU9GgudRz0WI/aTBRCVVmlZoGZOL9vJ31PE6XHFTZI7G6J7hA3JnLJiD7C/gyfL2 SxCG4Y6mPa9wDjn0exXGDLrUTpiRr2CcyoA6ucClsBQLtYgE0PsiU75Y5WQNsti3Ou4+ syEbb9Ic4mQPx2Jz69M7xoQ0eHT+5Y7o+rgVWrGLTGikw8/mfZUMMiJokEnMnL1lzpCw T6Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769025603; x=1769630403; h=in-reply-to:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sDQD4V+AOzYOfPpKHxDcdRY1BpjrYO1gFfE5IyB0cjg=; b=khm4Kqhkb9iFWNWVCe7Rl1JK3DGd3HwxwmzFmyg9LALhMNUkY9OvyrBx333N9mlM0S mrkU5GoHrqzwy9PblaYfhs1DIWVKzGuuDU0YHlvTyQaiGehtUgge8eEMekQCai+iw5nb Ir28HaHZBlEYhZ+qzgE2toRINy0ooFbVHYWjSci2/LbdoYEPnm3HKWTHUiaWuoK1DdzI MPgKag3jPJeLX9c7HJClGroPdnwIY1X4TRHomHxY+FKbxakBpGBs8RJ0PfP/sAXCBa9h PruZYUq3g+/zFUr6mI8CDpZG+sYwXL29MmCkVmTPbB0p3V4c2Jl4hIpPuk3o8ecCMaSp 57Aw== X-Gm-Message-State: AOJu0YwzI2al99EZMrb+g6RUQljYLBIAren3usD4FlwB4ZyZ4v5Oyjvn f8eJ3qWOrlimtAHWRZ24hCZlrbbNR6tGFRgRvLXcZeNApyr/NpmNkAUK X-Gm-Gg: AZuq6aIJeh4+gpaW2Mohe7U/8+Ilczgmi2D9MAYg6yXkAkdnv8fpw8fRCPRe5A+dmpb aNx+Mg6nGumJh0425s/iR6dpGdS9me8PmqfLx2vLeo9WITd8lM+1dlyBeigg4D7nl6JIW2ozLEr 8Sj9X0JnT4cjgOfWzB7CtoS4l7Zhwp5U3YgcmYpI4t2yqyKIu/EicSa77VZJGst5plvSPRupqd4 78GqQ1IDCxSQCKe7kjAYcbtjXoFT/N7U2UJ+EHq0MTyuk/KWTIvG4EjoEBhkaaaK0K6KX4yle3Z BXhUKXckmnf772Fxr8NAAF+kRPjptQjh0BrZ94VdKJ2hPkQbxQJ0Tr0zfIzXIu6E2lL7mNAHb9I Y3uj0Onnv5t1Ids+BxogNlveYstcBRDyz4ydmv68z3uY0lPFypK2dqi5URSESccFPvQguoiqaLd 7FGjsZncSX2vKz X-Received: by 2002:a05:600c:1991:b0:47d:6140:3284 with SMTP id 5b1f17b1804b1-4801e34b5a7mr226345085e9.37.1769025602723; Wed, 21 Jan 2026 12:00:02 -0800 (PST) Received: from [192.168.0.50] ([89.149.93.129]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4804249c6b5sm33236515e9.1.2026.01.21.12.00.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jan 2026 12:00:01 -0800 (PST) Content-Type: multipart/alternative; boundary="------------6A58PTumvZaLaB9zrdVQosiE" Message-ID: Date: Wed, 21 Jan 2026 22:00:00 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Undefined behavior detected by new clang's ubsan To: John Naylor Cc: pgsql-hackers , Tom Lane References: <777bd201-6e3a-4da0-a922-4ea9de46a3ee@gmail.com> <7ea15f17-f001-4b5c-979d-b9cff4db46bb@gmail.com> <1710697.1768935128@sss.pgh.pa.us> Content-Language: en-US From: Alexander Lakhin In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------6A58PTumvZaLaB9zrdVQosiE Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hello John, 21.01.2026 12:05, John Naylor wrote: > As for the rest of the proposed fixes, most seem okay, but I have some nits: Thank you for spending time on this! I agree with all of your changes (except for one noted below) -- didn't mean to propose committable changes, just wanted to show the fixes that allowed check-world to pass. > heaptoast.c > memcpy(VARDATA(result) + > - (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > + (int)(curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > > Not sure about this one. It would be better if we reversing the > operands allowed us to avoid overflow in the first place: > > - (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > + chcpystrt + (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) > > Does that silence the warning? Unfortunately, no -- I still got: heaptoast.c:771:17: runtime error: addition of unsigned offset to 0x78120673fac6 overflowed to 0x78120673fa04 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior heaptoast.c:771:17 Best regards, Alexander --------------6A58PTumvZaLaB9zrdVQosiE Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
Hello John,

21.01.2026 12:05, John Naylor wrote:
As for the rest of the proposed fixes, most seem okay, but I have some nits:

Thank you for spending time on this!
I agree with all of your changes (except for one noted below) -- didn't
mean to propose committable changes, just wanted to show the fixes that
allowed check-world to pass.

heaptoast.c
     memcpy(VARDATA(result) +
-         (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt,
+         (int)(curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt,

Not sure about this one. It would be better if we reversing the
operands allowed us to avoid overflow in the first place:

-         (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt,
+         chcpystrt + (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset)

Does that silence the warning?

Unfortunately, no -- I still got:
heaptoast.c:771:17: runtime error: addition of unsigned offset to 0x78120673fac6 overflowed to 0x78120673fa04
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior heaptoast.c:771:17


Best regards,
Alexander
--------------6A58PTumvZaLaB9zrdVQosiE--