If "snprintf()" can't print all the data because there's not enough

room, it might return -1 in some versions of glibc; check for that, and
quit if that happens.

It might also return the number of characters that would've been printed
had there been enough room; this means that a loop that does

	n += snprintf (buf + n, BUF_LENGTH - n, ...);

may end up making "n" bigger than BUF_LENGTH, and "snprintf()" might not
sanely handle being passed a negative length, so if "n" isn't less than
the total length of the string buffer, don't add stuff to it.

svn path=/trunk/; revision=3952
This commit is contained in:
Guy Harris 2001-09-25 02:21:15 +00:00
parent 7ee55bfd6b
commit 12db23546d
1 changed files with 12 additions and 5 deletions

View File

@ -3,7 +3,7 @@
* Copyright 2000, Axis Communications AB
* Inquiries/bugreports should be sent to Johan.Jorgensen@axis.com
*
* $Id: packet-ieee80211.c,v 1.39 2001/09/25 00:34:24 guy Exp $
* $Id: packet-ieee80211.c,v 1.40 2001/09/25 02:21:15 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@ -597,7 +597,7 @@ add_tagged_field (proto_tree * tree, tvbuff_t * tvb, int offset)
const guint8 *tag_data_ptr;
guint32 tag_no, tag_len;
unsigned int i;
int n;
int n, ret;
char out_buff[SHORT_STR];
@ -667,13 +667,20 @@ add_tagged_field (proto_tree * tree, tvbuff_t * tvb, int offset)
strcpy (out_buff, "Supported rates: ");
n = strlen (out_buff);
for (i = 0; i < tag_len; i++)
for (i = 0; i < tag_len && n < SHORT_STR; i++)
{
n += snprintf (out_buff + n, SHORT_STR - n, "%2.1f%s ",
ret = snprintf (out_buff + n, SHORT_STR - n, "%2.1f%s ",
(tag_data_ptr[i] & 0x7F) * 0.5,
(tag_data_ptr[i] & 0x80) ? "(B)" : "");
if (ret == -1) {
/* Some versions of snprintf return -1 if they'd truncate
the output. */
break;
}
n += ret;
}
snprintf (out_buff + n, SHORT_STR - n, "[Mbit/sec]");
if (n < SHORT_STR)
snprintf (out_buff + n, SHORT_STR - n, "[Mbit/sec]");
proto_tree_add_string (tree, tag_interpretation, tvb, offset + 2,
tag_len, out_buff);