Bug 291677 - FreeBSD 15.0 realloc does not set errno for large size values
Summary: FreeBSD 15.0 realloc does not set errno for large size values
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 15.0-CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Brooks Davis
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2025-12-15 02:04 UTC by Collin Funk
Modified: 2026-03-13 13:23 UTC (History)
3 users (show)

See Also:
brooks: mfc-stable15+
brooks: mfc-stable14-
brooks: mfc-stable13-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Collin Funk 2025-12-15 02:04:44 UTC
Using the following test program:

```
$ cat main.c 
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
  errno = 1729;
  void *p = malloc (1);
  printf ("p = 0x%jx, errno = %d\n", (uintmax_t) p, errno);
  p = realloc (p, PTRDIFF_MAX);
  printf ("p = 0x%jx, errno = %d\n", (uintmax_t) p, errno);
}
```

Here is the output on FreeBSD 14.3:

```
$ uname -r && cc main.c && ./a.out 
14.3-RELEASE-p5
p = 0x2bf54fe08008, errno = 1729
p = 0x0, errno = 12
```

Here is the output on FreeBSD 15.0:

```
$ uname -r && cc main.c && ./a.out 
15.0-RELEASE
p = 0x1b6e8dc08000, errno = 1729
p = 0x0, errno = 1729
```

So for large size values on FreeBSD 15.0 `realloc` fails but does not set `errno` to `ENOMEM` like  FreeBSD 14.3.

This was found in the Gnulib test suite by Bruno Haible [1]. He wrote:

> The reason is that realloc (p, size) sets errno when failing only when
> size <= 0x700000000000000; it leaves errno untouched if
> size > 0x700000000000000.

[1] https://lists.gnu.org/archive/html/bug-gnulib/2025-12/msg00021.html
Comment 1 Brooks Davis freebsd_committer freebsd_triage 2025-12-15 12:03:19 UTC
Sigh.  It looks like jemalloc refactored realloc() to use rallocx's internals in the common case and didn't account for the need to set errno.  The following untested patch likely fixes it (it might not be the best fix, but I think it does the job). Over all this doesn't give me great confidence in the correctness of this whole update. :(

```
diff --git a/contrib/jemalloc/src/jemalloc.c b/contrib/jemalloc/src/jemalloc.c
index e4b183d1a24d..30ad6bab44f0 100644
--- a/contrib/jemalloc/src/jemalloc.c
+++ b/contrib/jemalloc/src/jemalloc.c
@@ -3630,11 +3630,9 @@ je_realloc(void *ptr, size_t size) {
 	if (likely(ptr != NULL && size != 0)) {
 		void *ret = do_rallocx(ptr, size, 0, true);
 		LOG("core.realloc.exit", "result: %p", ret);
-		return ret;
 	} else if (ptr != NULL && size == 0) {
 		void *ret = do_realloc_nonnull_zero(ptr);
 		LOG("core.realloc.exit", "result: %p", ret);
-		return ret;
 	} else {
 		/* realloc(NULL, size) is equivalent to malloc(size). */
 		void *ret;
@@ -3663,6 +3661,12 @@ je_realloc(void *ptr, size_t size) {
 		LOG("core.realloc.exit", "result: %p", ret);
 		return ret;
 	}
+
+	if (unlikely(ret == NULL)) {
+		set_errno(ENOMEM);
+	}
+
+	return ret;
 }
 
 JEMALLOC_ALWAYS_INLINE size_t
```

(Category reset to bin as this isn't a kernel issue)
Comment 2 Mark Linimon freebsd_committer freebsd_triage 2025-12-15 12:55:53 UTC
^Triage: set flag to denote inline patch.
Comment 3 Collin Funk 2025-12-15 23:43:20 UTC
Thanks Mark, I was unsure if libc really fit under the kernel category. But it certainly doesn't fit under bin.

I mentioned that it was likely a jemalloc bug on the gnulib mailing list, and was a bit unsure if it would be fixed given that jemalloc is no longer maintained [1].

[1] https://lists.gnu.org/archive/html/bug-gnulib/2025-12/msg00022.html
Comment 4 Ed Maste freebsd_committer freebsd_triage 2025-12-19 19:21:14 UTC
(In reply to Collin Funk from comment #3)
We're responsible for it in FreeBSD as long as it is our libc allocator regardless of what happens with jemalloc upstream (or forks); if we have to patch it ourselves we will.
Comment 5 Brooks Davis freebsd_committer freebsd_triage 2026-02-09 21:35:19 UTC
I've failed to find time to test this. Adding Minsoo Choo to the CC line in hopes he has time to clean up this issue from the import he spearheaded.
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-03-09 12:37:56 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2c5cd07828ad76c332e3bedc29fc641809e85396

commit 2c5cd07828ad76c332e3bedc29fc641809e85396
Author:     Qi Wang <interwq@gwu.edu>
AuthorDate: 2026-03-03 11:55:23 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-03-09 12:35:39 +0000

    rallocx path: only set errno on the realloc case.

    PR:             291677
    Obtained from:  jemalloc (commit 83b075789b4239035931c1ee212576d00153bbf0)
    Fixes:          c43cad871720 ("jemalloc: Merge from jemalloc 5.3.0 vendor branch")
    MFC after:      3 days
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2059

 contrib/jemalloc/src/jemalloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2026-03-09 12:37:57 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=5583b64f230fe0ea4e3d4bf4566205b521190fbb

commit 5583b64f230fe0ea4e3d4bf4566205b521190fbb
Author:     Juhyung Park <qkrwngud825@gmail.com>
AuthorDate: 2026-03-03 09:59:33 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-03-09 12:35:25 +0000

    Set errno to ENOMEM on rallocx() OOM failures

    realloc() and rallocx() shares path, and realloc() should set errno to
    ENOMEM upon OOM failures.

    PR:             291677
    Obtained from:  jemalloc (commit 38056fea64c34ca4fef0a16212776eaa4de80b78)
    Fixes:          c43cad871720 ("jemalloc: Merge from jemalloc 5.3.0 vendor branch")
    MFC after:      3 days
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2059

 contrib/jemalloc/src/jemalloc.c | 1 +
 1 file changed, 1 insertion(+)
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-03-13 09:14:47 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=b4d8d9bde083b466bc0728c6400cf5e6439f44e3

commit b4d8d9bde083b466bc0728c6400cf5e6439f44e3
Author:     Qi Wang <interwq@gwu.edu>
AuthorDate: 2026-03-03 11:55:23 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-03-13 09:13:16 +0000

    rallocx path: only set errno on the realloc case.

    PR:             291677
    Obtained from:  jemalloc (commit 83b075789b4239035931c1ee212576d00153bbf0)
    Fixes:          c43cad871720 ("jemalloc: Merge from jemalloc 5.3.0 vendor branch")
    MFC after:      3 days
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2059

    (cherry picked from commit 2c5cd07828ad76c332e3bedc29fc641809e85396)

 contrib/jemalloc/src/jemalloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-03-13 09:14:48 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=3abef030d31a9837ac7ba45bd8ee17340fabd120

commit 3abef030d31a9837ac7ba45bd8ee17340fabd120
Author:     Juhyung Park <qkrwngud825@gmail.com>
AuthorDate: 2026-03-03 09:59:33 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-03-13 09:13:08 +0000

    Set errno to ENOMEM on rallocx() OOM failures

    realloc() and rallocx() shares path, and realloc() should set errno to
    ENOMEM upon OOM failures.

    PR:             291677
    Obtained from:  jemalloc (commit 38056fea64c34ca4fef0a16212776eaa4de80b78)
    Fixes:          c43cad871720 ("jemalloc: Merge from jemalloc 5.3.0 vendor branch")
    MFC after:      3 days
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2059

    (cherry picked from commit 5583b64f230fe0ea4e3d4bf4566205b521190fbb)

 contrib/jemalloc/src/jemalloc.c | 1 +
 1 file changed, 1 insertion(+)
Comment 10 Brooks Davis freebsd_committer freebsd_triage 2026-03-13 13:23:42 UTC
I've committed a fix and merged to stable/15.  I'm leaning toward an errata not being worth it as nothing will be able to rely on correct behavior until 15.1.  I could be persuaded otherwise, but am short on time at the moment.