mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pascal Cuoq <cuoq@trust-in-soft.com>
To: "musl@lists.openwall.com" <musl@lists.openwall.com>
Subject: Re: un-UBify-strings
Date: Sun, 23 Sep 2018 03:10:14 +0000	[thread overview]
Message-ID: <6D4B315A-2D77-426B-99DC-29BC2B723396@trust-in-soft.com> (raw)
In-Reply-To: <20180923024511.GF17995@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 972 bytes --]


On 23 Sep 2018, at 04:45, Rich Felker <dalias@libc.org<mailto:dalias@libc.org>> wrote:
I'm also trying to fix the UB in
address range checks for implementing memmove as memcpy, etc. Is this
correct:

if ((uintptr_t)s-(uintptr_t)d-n <= -2*n) return memcpy(d, s, n);

?

It looks okay to me. You want to test whether (uintptr_t)s-(uintptr_t)d, computed as a mathematical integer, is between -n and n, and since uintptr_t is unsigned, you are using the well-known trick of aligning one of the bounds with 0 so that both inequalities can be tested in one instruction.

It would seen more natural to me to work on the right-hand side of zero, that it, to compute (uintptr_t)s-(uintptr_t)d+n and to check whether that is <= 2*n (overlap) or > 2*n (no overlap). The generated code may even be one instruction shorter. Apart from that, as long as we have the hypothesis that n <= UINTPTR_MAX/2, I cannot immediately see any reason why it would not work.

Pascal


[-- Attachment #2: Type: text/html, Size: 5904 bytes --]

  reply	other threads:[~2018-09-23  3:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-23  0:35 un-UBify-strings Rich Felker
2018-09-23  2:11 ` un-UBify-strings Pascal Cuoq
2018-09-23  2:32   ` un-UBify-strings Rich Felker
2018-09-23  2:45     ` un-UBify-strings Rich Felker
2018-09-23  3:10       ` Pascal Cuoq [this message]
2018-09-23  3:15         ` un-UBify-strings Rich Felker
2018-09-23  3:44           ` un-UBify-strings Pascal Cuoq
2018-09-23  4:02             ` un-UBify-strings Rich Felker
2018-09-23  3:45           ` un-UBify-strings Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6D4B315A-2D77-426B-99DC-29BC2B723396@trust-in-soft.com \
    --to=cuoq@trust-in-soft.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).