mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Tamir Duberstein <tamird@google.com>
To: NRK <nrk@disroot.org>
Cc: Rich Felker <dalias@libc.org>, musl@lists.openwall.com
Subject: Re: [musl] undefined behavior in fread.c
Date: Fri, 24 Feb 2023 11:12:11 -0500	[thread overview]
Message-ID: <CAK-_uh5faEm+biUshK_R9q_p7OqKv+_CsBYq-9V+1DhzE5Y97Q@mail.gmail.com> (raw)
In-Reply-To: <20230224151334.ycgddzdc4o5a4m5q@gen2.localdomain>

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

I agree, the caller's behavior is UB. I'll send them (freetype2) a patch.

That said, do we want to avoid internal UB here anyway?

- As mentioned earlier, glibc avoids the UB (and the lock).
- llvm-libc does the same starting with
https://github.com/llvm/llvm-project/commit/53c251b
- uclibc avoids the UB but still locks:
https://github.com/gittup/uClibc/blob/9dbf00b/libc/stdio/fread.c#L25
- FreeBSD avoids the UB but still locks:
https://svnweb.freebsd.org/base/head/lib/libc/stdio/fread.c?view=markup#l76
- Android (bionic) avoids the UB but still locks:
https://cs.android.com/android/platform/superproject/+/master:bionic/libc/stdio/stdio.cpp;l=1099;drc=4aa8f499f21ebf84101de34d68682d5388667001

Does this persuade?

On Fri, Feb 24, 2023 at 10:13 AM NRK <nrk@disroot.org> wrote:

> On Fri, Feb 24, 2023 at 09:42:00AM -0500, Tamir Duberstein wrote:
> > We could take the lock and still avoid UB with an early return.
>
> As Jens has pointed out, the UB in this case is the caller calling fread
> with NULL - not in musl.
>
> And on a sidenote, I've always found - especially for the various mem*
> functions - accepting 0 size but not accepting NULL arg (when n is 0) to
> be a poor choice. A lot of the value that accepting 0 size provides is
> diminished by not accepting NULL.
>
> And this affects more than just libc, too. Compilers like gcc/clang will
> see a call like `memcmp(p, q, 0)` and will ""determine"" `p` and `q` are
> non-null (which can lead to deleting any subsequent null-checks on those
> pointers).
>
> But anyways, that was just a small rant.
>
> As things currently are, *even if* musl deal with the NULL pointer - any
> caller calling fread with NULL is still in danger from compilers and
> needs to fix it on their side.
>
> - NRK
>

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

  reply	other threads:[~2023-02-24 16:12 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24 12:52 Tamir Duberstein
2023-02-24 13:34 ` Rich Felker
2023-02-24 13:53   ` Jₑₙₛ Gustedt
2023-02-24 13:55   ` NRK
2023-02-24 14:07     ` Rich Felker
2023-02-24 14:17       ` NRK
2023-02-24 14:42         ` Tamir Duberstein
2023-02-24 15:13           ` NRK
2023-02-24 16:12             ` Tamir Duberstein [this message]
2023-02-24 16:40               ` Jₑₙₛ Gustedt
2023-02-24 16:42                 ` Tamir Duberstein
2023-02-24 17:00                   ` Jₑₙₛ Gustedt
2023-02-24 17:07                     ` enh
2023-02-24 17:32                       ` Tamir Duberstein
2023-02-24 16:42                 ` enh
2023-02-24 20:07                 ` 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=CAK-_uh5faEm+biUshK_R9q_p7OqKv+_CsBYq-9V+1DhzE5Y97Q@mail.gmail.com \
    --to=tamird@google.com \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nrk@disroot.org \
    /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).