git: a8c598de78fd - stable/15 - mpool/mpool_get.c: Avoid clobbering 'errno' when handling 'pread' errors

Bojan Novković <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a79f764.348bb.6ae7a33e__33301.0767166101$1786378238$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by bnovkov:

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

commit a8c598de78fd9ef6724d5f9366bc4b03fd5cbf9c
Author:     Bojan Novković <[email protected]>
AuthorDate: 2026-02-23 15:30:26 +0000
Commit:     Bojan Novković <[email protected]>
CommitDate: 2026-08-10 16:06:02 +0000

    mpool/mpool_get.c: Avoid clobbering 'errno' when handling 'pread' errors
    
    POSIX.1-2024 states that the 'free' function "shall not modify errno if
    ptr is a null pointer or a pointer previously returned as if by malloc()
    and not yet deallocated". However this is a fairly recent addition
    and non-compliant allocators might still clobber 'errno', causing
    'mpool_get' to return the wrong error code. Fix this by saving
    and restoring 'errno' after calling 'free'.
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    obiwac
    Differential Revision:  https://reviews.freebsd.org/D55463
    MFC after:      1 week
    
    (cherry picked from commit bce0c14fe19defeef4f02cfebc018e9adf979783)
---
 lib/libc/db/mpool/mpool.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/libc/db/mpool/mpool.c b/lib/libc/db/mpool/mpool.c
index 1bab66d73baf..0df19dc09e4d 100644
--- a/lib/libc/db/mpool/mpool.c
+++ b/lib/libc/db/mpool/mpool.c
@@ -214,10 +214,14 @@ mpool_get(MPOOL *mp, pgno_t pgno,
 	/* Read in the contents. */
 	off = mp->pagesize * pgno;
 	if ((nr = pread(mp->fd, bp->page, mp->pagesize, off)) != (ssize_t)mp->pagesize) {
+		int serrno;
+
 		switch (nr) {
 		case -1:
 			/* errno is set for us by pread(). */
+			serrno = errno;
 			free(bp);
+			errno = serrno;
 			mp->curcache--;
 			return (NULL);
 		case 0:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.