newbus: use ssize_t to match sb's len and size, fix ordering of space check

Both s_len and s_size are ssize_t, so their differece is also more
properly a ssize_t not a size_t. Also, assert that len is <= size when
we enter. This should always be the case. Ensure that we have that one
byte that we write to the end of the buffer before we do so, though
the error should already be set on the buffer if not, and the only
times we supply 'partial' buffers they should be plenty large.

Reviewed by: cem, jhb (prior version, I did cem's suggestion)
Differential Revsion: https://reviews.freebsd.org/D26752
This commit is contained in:
Warner Losh 2020-10-12 22:07:44 +00:00
parent f91b0c1c18
commit e59db46854

View File

@ -4956,18 +4956,19 @@ static int
bus_child_pnpinfo_sb(device_t dev, struct sbuf *sb)
{
char *p;
size_t space;
ssize_t space;
MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
MPASS(sb->s_size >= sb->s_len);
if (sb->s_error != 0)
return (-1);
p = EOB(sb);
*p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
space = SPACE(sb);
if (space <= 1) {
sb->s_error = ENOMEM;
return (-1);
}
p = EOB(sb);
*p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
bus_child_pnpinfo_str(dev, p, space);
sb->s_len += strlen(p);
return (0);
@ -4985,18 +4986,19 @@ static int
bus_child_location_sb(device_t dev, struct sbuf *sb)
{
char *p;
size_t space;
ssize_t space;
MPASS((sb->s_flags & SBUF_INCLUDENUL) == 0);
MPASS(sb->s_size >= sb->s_len);
if (sb->s_error != 0)
return (-1);
p = EOB(sb);
*p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
space = SPACE(sb);
if (space <= 1) {
sb->s_error = ENOMEM;
return (-1);
}
p = EOB(sb);
*p = '\0'; /* sbuf buffer isn't NUL terminated until sbuf_finish() */
bus_child_location_str(dev, p, space);
sb->s_len += strlen(p);
return (0);