From 3ece458fe698324d1dbaf76e5c8b68d39e8ad466 Mon Sep 17 00:00:00 2001 From: millert Date: Tue, 4 Apr 2017 02:37:15 +0000 Subject: [PATCH] Rewrite tohexstr() to use the common idiom of converting a byte string to hex nybble by nybble. This avoids using the return value of snprintf() unchecked. OK deraadt@ --- usr.sbin/snmpd/snmpd.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/usr.sbin/snmpd/snmpd.c b/usr.sbin/snmpd/snmpd.c index 96caeeacf67..ba5661a2223 100644 --- a/usr.sbin/snmpd/snmpd.c +++ b/usr.sbin/snmpd/snmpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.c,v 1.35 2017/01/09 14:49:22 reyk Exp $ */ +/* $OpenBSD: snmpd.c,v 1.36 2017/04/04 02:37:15 millert Exp $ */ /* * Copyright (c) 2007, 2008, 2012 Reyk Floeter @@ -375,16 +375,19 @@ snmpd_engine_time(void) } char * -tohexstr(u_int8_t *str, int len) +tohexstr(u_int8_t *bstr, int len) { #define MAXHEXSTRLEN 256 static char hstr[2 * MAXHEXSTRLEN + 1]; - char *r = hstr; + static const char hex[] = "0123456789abcdef"; + int i; if (len > MAXHEXSTRLEN) len = MAXHEXSTRLEN; /* truncate */ - while (len-- > 0) - r += snprintf(r, len * 2, "%0*x", 2, *str++); - *r = '\0'; + for (i = 0; i < len; i++) { + hstr[i + i] = hex[bstr[i] >> 4]; + hstr[i + i + 1] = hex[bstr[i] & 0x0f]; + } + hstr[i + i] = '\0'; return hstr; } -- 2.20.1