aboutsummaryrefslogtreecommitdiffstats
path: root/epan/to_str.c
diff options
context:
space:
mode:
authorChris Maynard <Christopher.Maynard@GTECH.COM>2011-12-13 16:50:34 +0000
committerChris Maynard <Christopher.Maynard@GTECH.COM>2011-12-13 16:50:34 +0000
commit47116e864cdbae91d5980428976f48c0e92c15c1 (patch)
tree08eb00e999d7929097163082a7ca9ebc840ad687 /epan/to_str.c
parentd94bd07f99438278cb11f24b00571ab2907b6bdb (diff)
Protect against NULL pointers as input. Based on the stacktrace provided, this should avoid crashes reported in https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=6634
svn path=/trunk/; revision=40176
Diffstat (limited to 'epan/to_str.c')
-rw-r--r--epan/to_str.c44
1 files changed, 26 insertions, 18 deletions
diff --git a/epan/to_str.c b/epan/to_str.c
index 59382a83ad..7a54f6fcd7 100644
--- a/epan/to_str.c
+++ b/epan/to_str.c
@@ -106,24 +106,32 @@ dword_to_hex_punct(char *out, guint32 dword, char punct) {
/* buffer need to be at least len * 2 size */
char *
bytes_to_hexstr(char *out, const guint8 *ad, guint32 len) {
- guint32 i;
+ guint32 i;
- for (i = 0; i < len; i++)
- out = byte_to_hex(out, ad[i]);
- return out;
+ if (!ad || !len) {
+ *out = '\0';
+ } else {
+ for (i = 0; i < len; i++)
+ out = byte_to_hex(out, ad[i]);
+ }
+ return out;
}
/* buffer need to be at least len * 3 - 1 size */
char *
bytes_to_hexstr_punct(char *out, const guint8 *ad, guint32 len, char punct) {
- guint32 i;
-
- out = byte_to_hex(out, ad[0]);
- for (i = 1; i < len; i++) {
- *out++ = punct;
- out = byte_to_hex(out, ad[i]);
- }
- return out;
+ guint32 i;
+
+ if (!ad || !len) {
+ *out = 0;
+ } else {
+ out = byte_to_hex(out, ad[0]);
+ for (i = 1; i < len; i++) {
+ *out++ = punct;
+ out = byte_to_hex(out, ad[i]);
+ }
+ }
+ return out;
}
/* Routine to convert a sequence of bytes to a hex string, one byte/two hex
@@ -142,7 +150,7 @@ bytestring_to_str(const guint8 *ad, const guint32 len, const char punct) {
if ( ((int) len) < 0)
return "";
- if (!len)
+ if (!ad || !len)
return "";
if (punct)
@@ -171,7 +179,7 @@ bytes_to_str(const guint8 *bd, int bd_len) {
int truncated = 0;
cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
- if (bd_len <= 0) { cur[0] = '\0'; return cur; }
+ if (!bd || bd_len <= 0) { cur[0] = '\0'; return cur; }
if (bd_len > MAX_BYTE_STR_LEN/2) { /* bd_len > 24 */
truncated = 1;
@@ -197,14 +205,14 @@ bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct) {
int truncated = 0;
if (!punct)
- return bytes_to_str(bd, bd_len);
+ return bytes_to_str(bd, bd_len);
cur=ep_alloc(MAX_BYTE_STR_LEN+3+1);
- if (bd_len <= 0) { cur[0] = '\0'; return cur; }
+ if (!bd || bd_len <= 0) { cur[0] = '\0'; return cur; }
if (bd_len > MAX_BYTE_STR_LEN/3) { /* bd_len > 16 */
- truncated = 1;
- bd_len = MAX_BYTE_STR_LEN/3;
+ truncated = 1;
+ bd_len = MAX_BYTE_STR_LEN/3;
}
cur_ptr = bytes_to_hexstr_punct(cur, bd, bd_len, punct); /* max MAX_BYTE_STR_LEN-1 bytes */