In sockaddr_snprintf(sbuf, len, fmt, sa), do not write the terminating
null character outside of the byte region [sbuf, sbuf + len). 1) If the length of the buffer is 0, do not write a null character at all. Previously, sockaddr_snprintf() may have been able to overwrite sbuf[-1] if len was 0. 2) If the length of the buffer, len, is greater than 0, then write the null at sbuf[len - 1]. Previously, sockaddr_snprintf() wrote the null at buf[len - 1], where `buf' was a "cursor" that did not necessarily equal `sbuf', the start of the buffer. Now, sockaddr_snprintf() always writes the null at sbuf[len - 1].
This commit is contained in:
parent
8628a88239
commit
bfa27930cb
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: sockaddr_snprintf.c,v 1.7 2006/12/09 20:18:43 dyoung Exp $ */
|
||||
/* $NetBSD: sockaddr_snprintf.c,v 1.8 2007/07/24 08:45:45 dyoung Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2004 The NetBSD Foundation, Inc.
|
||||
|
@ -37,7 +37,7 @@
|
|||
*/
|
||||
#include <sys/cdefs.h>
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.7 2006/12/09 20:18:43 dyoung Exp $");
|
||||
__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.8 2007/07/24 08:45:45 dyoung Exp $");
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <sys/types.h>
|
||||
|
@ -55,13 +55,13 @@ __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.7 2006/12/09 20:18:43 dyoung Exp $");
|
|||
#include <netdb.h>
|
||||
|
||||
int
|
||||
sockaddr_snprintf(char *buf, size_t len, const char *fmt,
|
||||
const struct sockaddr *sa)
|
||||
sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
|
||||
const struct sockaddr * const sa)
|
||||
{
|
||||
const void *a = NULL;
|
||||
char abuf[1024], nbuf[1024], *addr = NULL, *w = NULL;
|
||||
char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
|
||||
char *ebuf = &buf[len - 1], *sbuf = buf;
|
||||
char *ebuf = &sbuf[len - 1], *buf = sbuf;
|
||||
const char *ptr, *s;
|
||||
int p = -1;
|
||||
const struct sockaddr_at *sat = NULL;
|
||||
|
@ -73,8 +73,6 @@ sockaddr_snprintf(char *buf, size_t len, const char *fmt,
|
|||
|
||||
#define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
|
||||
while (/*CONSTCOND*/0)
|
||||
#define ADDN() do { if (buf < ebuf) *buf = '\0'; else buf[len - 1] = '\0'; } \
|
||||
while (/*CONSTCOND*/0)
|
||||
#define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
|
||||
while (/*CONSTCOND*/0)
|
||||
#define ADDNA() do { if (na) ADDS("N/A"); } \
|
||||
|
@ -230,6 +228,9 @@ sockaddr_snprintf(char *buf, size_t len, const char *fmt,
|
|||
na = 1;
|
||||
}
|
||||
done:
|
||||
ADDN();
|
||||
if (buf < ebuf)
|
||||
*buf = '\0';
|
||||
else if (len != 0)
|
||||
sbuf[len - 1] = '\0';
|
||||
return (int)(buf - sbuf);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue