git: c675e0710ea6 - stable/15 - stdio: *memstream: slightly streamline growth function

Kyle Evans <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a753962.3c315.a2debe1__5277.7685619489$1786067414$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by kevans:

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

commit c675e0710ea6c3f8608fcb356ea49ae81e0aefd8
Author:     Kyle Evans <[email protected]>
AuthorDate: 2026-08-01 03:34:37 +0000
Commit:     Kyle Evans <[email protected]>
CommitDate: 2026-08-06 23:37:17 +0000

    stdio: *memstream: slightly streamline growth function
    
    Inverting the condition after realloc*() is a minor cleanup, but makes
    the success path a little cleaner to ease a future change.
    
    Reviewed by:    des, jhb
    Sponsored by:   Klara, Inc.
    
    (cherry picked from commit 28327c58ee6de7ddbdcf0e56352b257d37f2103d)
---
 lib/libc/stdio/open_memstream.c  | 17 ++++++++---------
 lib/libc/stdio/open_wmemstream.c | 16 +++++++---------
 2 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/lib/libc/stdio/open_memstream.c b/lib/libc/stdio/open_memstream.c
index 371022adf6b3..29de688376bf 100644
--- a/lib/libc/stdio/open_memstream.c
+++ b/lib/libc/stdio/open_memstream.c
@@ -62,17 +62,16 @@ memstream_grow(struct memstream *ms, fpos_t newoff)
 		newsize = newoff;
 	if (newsize > ms->len) {
 		buf = realloc(*ms->bufp, newsize + 1);
-		if (buf != NULL) {
+		if (buf == NULL)
+			return (0);
+
 #ifdef DEBUG
-			fprintf(stderr, "MS: %p growing from %zd to %zd\n",
-			    ms, ms->len, newsize);
+		fprintf(stderr, "MS: %p growing from %zd to %zd\n",
+		    ms, ms->len, newsize);
 #endif
-			memset(buf + ms->len + 1, 0, newsize - ms->len);
-			*ms->bufp = buf;
-			ms->len = newsize;
-			return (1);
-		}
-		return (0);
+		memset(buf + ms->len + 1, 0, newsize - ms->len);
+		*ms->bufp = buf;
+		ms->len = newsize;
 	}
 	return (1);
 }
diff --git a/lib/libc/stdio/open_wmemstream.c b/lib/libc/stdio/open_wmemstream.c
index 213d61fcd4dd..0bed4cff5d11 100644
--- a/lib/libc/stdio/open_wmemstream.c
+++ b/lib/libc/stdio/open_wmemstream.c
@@ -63,17 +63,15 @@ wmemstream_grow(struct wmemstream *ms, fpos_t newoff)
 		newsize = newoff;
 	if (newsize > ms->len) {
 		buf = reallocarray(*ms->bufp, newsize + 1, sizeof(wchar_t));
-		if (buf != NULL) {
+		if (buf == NULL)
+			return (0);
 #ifdef DEBUG
-			fprintf(stderr, "WMS: %p growing from %zd to %zd\n",
-			    ms, ms->len, newsize);
+		fprintf(stderr, "WMS: %p growing from %zd to %zd\n",
+		    ms, ms->len, newsize);
 #endif
-			wmemset(buf + ms->len + 1, 0, newsize - ms->len);
-			*ms->bufp = buf;
-			ms->len = newsize;
-			return (1);
-		}
-		return (0);
+		wmemset(buf + ms->len + 1, 0, newsize - ms->len);
+		*ms->bufp = buf;
+		ms->len = newsize;
 	}
 	return (1);
 }
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.