NetBSD/sys/dev/dev_verbose.c
pgoyette 1656c8e745 Avoid writing beyond the end of the buffer we were given.
This should actually cure the "stack overflow" reported earlier (and
was worked around by increasing the size of the buffer).
2016-10-26 06:10:39 +00:00

99 lines
2.8 KiB
C

/* $NetBSD: dev_verbose.c,v 1.3 2016/10/26 06:10:39 pgoyette Exp $ */
/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dev_verbose.c,v 1.3 2016/10/26 06:10:39 pgoyette Exp $");
#include <sys/param.h>
#ifdef _KERNEL
#include <sys/systm.h>
#else
#include <stdio.h>
#include <string.h>
#endif
#include <dev/dev_verbose.h>
static const char *
dev_untokenstring(const char *words, const uint16_t *token, char *buf,
size_t len)
{
char *cp = buf;
size_t newlen;
buf[0] = '\0';
for (; *token != 0; token++) {
newlen = strlcat(buf, words + *token, len - 2);
if (newlen > len - 2)
newlen = len - 2;
cp = buf + newlen;
cp[0] = ' ';
cp[1] = '\0';
}
*cp = '\0';
return cp != buf ? buf : NULL;
}
const char *
dev_findvendor(char *buf, size_t len, const char *words, size_t nwords,
const uint16_t *vendors, size_t nvendors, uint16_t vendor)
{
size_t n;
for (n = 0; n < nvendors; n++) {
if (vendors[n] == vendor)
return dev_untokenstring(words, &vendors[n + 1],
buf, len);
/* Skip Tokens */
n++;
while (n < nvendors && vendors[n] != 0)
n++;
}
snprintf(buf, len, "vendor %4.4x", vendor);
return NULL;
}
const char *
dev_findproduct(char *buf, size_t len, const char *words, size_t nwords,
const uint16_t *products, size_t nproducts, uint16_t vendor,
uint16_t product)
{
size_t n;
for (n = 0; n < nproducts; n++) {
if (products[n] == vendor && products[n + 1] == product)
return dev_untokenstring(words, &products[n + 2],
buf, len);
/* Skip Tokens */
n += 2;
while (n < nproducts && products[n] != 0)
n++;
}
snprintf(buf, len, "product %4.4x", product);
return NULL;
}