Skip to content

Commit

Permalink
Make sbuf_drain safe for external use
Browse files Browse the repository at this point in the history
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 cad4fd0)
  • Loading branch information
rscheff committed Apr 16, 2021
1 parent dc5281a commit 424f636
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sys/kern/subr_sbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,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);

if (SBUF_DODRAINTOEOR(s) && s->s_rec_off == 0)
return (s->s_error = EDEADLK);
len = s->s_drain_func(s->s_drain_arg, s->s_buf,
Expand Down

0 comments on commit 424f636

Please sign in to comment.