git: 2e96d6ef392e - stable/11 - Make sbuf_drain safe for external use

Richard Scheffenegger rscheff at FreeBSD.org
Fri Apr 16 19:41:00 UTC 2021


The branch stable/11 has been updated by rscheff:

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

commit 2e96d6ef392e2280a8cb39d89932596c3524ddda
Author:     Richard Scheffenegger <rscheff at FreeBSD.org>
AuthorDate: 2021-04-02 18:11:45 +0000
Commit:     Richard Scheffenegger <rscheff at FreeBSD.org>
CommitDate: 2021-04-16 19:39:52 +0000

    Make sbuf_drain safe for external use
    
    While sbuf_drain was an internal function, two
    KASSERTS checked the sanity of it being called.
    However, an external caller may be ignorant if
    there is any data to drain, or if an error has
    already accumulated. Be nice and return immediately
    with the accumulated error.
    
    MFC after: 2 weeks
    Reviewed By: tuexen, #transport
    Sponsored by: NetApp, Inc.
    Differential Revision: https://reviews.freebsd.org/D29544
    
    (cherry picked from commit cad4fd0365a5e3235e715e072e6ee9dffaa7a3ab)
---
 sys/kern/subr_sbuf.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/sys/kern/subr_sbuf.c b/sys/kern/subr_sbuf.c
index 164209988670..01865ac259ab 100644
--- a/sys/kern/subr_sbuf.c
+++ b/sys/kern/subr_sbuf.c
@@ -360,8 +360,13 @@ sbuf_drain(struct sbuf *s)
 {
 	int len;
 
-	KASSERT(s->s_len > 0, ("Shouldn't drain empty sbuf %p", s));
-	KASSERT(s->s_error == 0, ("Called %s with error on %p", __func__, s));
+	/*
+	 * Immediately return when no work to do,
+	 * or an error has already been accumulated.
+	 */
+	if ((s->s_len == 0) || (s->s_error != 0))
+		return(s->s_error);
+
 	len = s->s_drain_func(s->s_drain_arg, s->s_buf, s->s_len);
 	if (len < 0) {
 		s->s_error = -len;


More information about the dev-commits-src-all mailing list