Remove use of sprintf for ftype string formatting

Change-Id: I656d6193aad740ab88bf16fb25c202e766e3092a
Reviewed-on: https://code.wireshark.org/review/7616
Petri-Dish: Michael Mann <mmann78@netscape.net>
Reviewed-by: Michael Mann <mmann78@netscape.net>
This commit is contained in:
Michael Mann 2015-03-07 10:02:08 -05:00
parent b5d062ba57
commit 0bec88518f
4 changed files with 39 additions and 60 deletions

View File

@ -20,13 +20,13 @@
#include "config.h" #include "config.h"
#include <stdio.h>
#include <ftypes-int.h> #include <ftypes-int.h>
#include <string.h> #include <string.h>
#include <epan/addr_resolv.h> #include <epan/addr_resolv.h>
#include <epan/strutil.h> #include <epan/strutil.h>
#include <epan/oids.h> #include <epan/oids.h>
#include <epan/osi-utils.h> #include <epan/osi-utils.h>
#include <epan/to_str-int.h>
#define CMP_MATCHES cmp_matches #define CMP_MATCHES cmp_matches
@ -140,39 +140,26 @@ system_id_to_repr(fvalue_t *fv, ftrepr_t rtype, int field_display, char *buf)
static void static void
bytes_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display, char *buf) bytes_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display, char *buf)
{ {
guint8 *c;
char *write_cursor;
unsigned int i;
char separator; char separator;
c = fv->value.bytes->data; switch(field_display)
write_cursor = buf; {
case SEP_DOT:
for (i = 0; i < fv->value.bytes->len; i++) { separator = '.';
if (i == 0) { break;
sprintf(write_cursor, "%02x", *c++); case SEP_DASH:
write_cursor += 2; separator = '-';
} break;
else { case SEP_SPACE:
switch(field_display) case SEP_COLON:
{ case BASE_NONE:
case SEP_DOT: default:
separator = '.'; separator = ':';
break; break;
case SEP_DASH:
separator = '-';
break;
case SEP_SPACE:
case SEP_COLON:
case BASE_NONE:
default:
separator = ':';
break;
}
sprintf(write_cursor, "%c%02x", separator, *c++);
write_cursor += 3;
}
} }
buf = bytes_to_hexstr_punct(buf, fv->value.bytes->data, fv->value.bytes->len, separator);
*buf = '\0';
} }
static void static void

View File

@ -20,11 +20,11 @@
#include "config.h" #include "config.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include "ftypes-int.h" #include "ftypes-int.h"
#include <epan/addr_resolv.h> #include <epan/addr_resolv.h>
#include <epan/to_str-int.h>
#include <wsutil/pint.h> #include <wsutil/pint.h>
@ -243,16 +243,9 @@ integer_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *b
} }
static int static int
uinteger_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display) uinteger_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
{ {
if (field_display == BASE_HEX) return 10; /* enough for 2^32-1, in decimal or 0xXXXXXXXX */
{
return 11; /* fits 0x%08x */
}
else
{
return 10; /* enough for 2^32-1, in decimal */
}
} }
static void static void
@ -261,7 +254,11 @@ uinteger_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display, char *buf)
if (field_display == BASE_HEX) if (field_display == BASE_HEX)
{ {
/* This format perfectly fits into 11 bytes. */ /* This format perfectly fits into 11 bytes. */
sprintf(buf, "0x%08x", fv->value.uinteger); *buf++ = '0';
*buf++ = 'x';
buf = dword_to_hex(buf, fv->value.uinteger);
*buf++ = '\0';
} }
else else
{ {
@ -304,7 +301,7 @@ ipxnet_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
static void static void
ipxnet_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf) ipxnet_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf)
{ {
sprintf(buf, "0x%08x", fv->value.uinteger); uinteger_to_repr(fv, rtype, BASE_HEX, buf);
} }
static gboolean static gboolean

View File

@ -344,9 +344,15 @@ absolute_val_to_repr(fvalue_t *fv, ftrepr_t rtype, int field_display _U_, char *
gchar *rep = abs_time_to_str(NULL, &fv->value.time, ABSOLUTE_TIME_LOCAL, gchar *rep = abs_time_to_str(NULL, &fv->value.time, ABSOLUTE_TIME_LOCAL,
rtype == FTREPR_DISPLAY); rtype == FTREPR_DISPLAY);
if (rtype == FTREPR_DFILTER) { if (rtype == FTREPR_DFILTER) {
sprintf(buf, "\"%s\"", rep); *buf++ = '\"';
} else { }
strcpy(buf, rep);
strcpy(buf, rep);
if (rtype == FTREPR_DFILTER) {
buf += strlen(rep);
*buf++ = '\"';
*buf++ = '\0';
} }
wmem_free(NULL, rep); wmem_free(NULL, rep);
} }

View File

@ -20,8 +20,8 @@
#include "config.h" #include "config.h"
#include <stdio.h>
#include <ftypes-int.h> #include <ftypes-int.h>
#include <epan/to_str-int.h>
#include <string.h> #include <string.h>
#include <epan/exceptions.h> #include <epan/exceptions.h>
@ -141,25 +141,14 @@ static void
val_to_repr(fvalue_t *fv, ftrepr_t rtype, int field_display _U_, char * volatile buf) val_to_repr(fvalue_t *fv, ftrepr_t rtype, int field_display _U_, char * volatile buf)
{ {
guint length; guint length;
const guint8 *c;
unsigned int i;
g_assert(rtype == FTREPR_DFILTER); g_assert(rtype == FTREPR_DFILTER);
TRY { TRY {
length = tvb_length(fv->value.tvb); length = tvb_length(fv->value.tvb);
c = tvb_get_ptr(fv->value.tvb, 0, length);
for (i = 0; i < length; i++) { buf = bytes_to_hexstr_punct(buf, tvb_get_ptr(fv->value.tvb, 0, length), length, ':');
if (i == 0) { *buf = '\0';
sprintf((char *)buf, "%02x", *c++);
buf += 2;
}
else {
sprintf((char *)buf, ":%02x", *c++);
buf += 3;
}
}
} }
CATCH_ALL { CATCH_ALL {
/* nothing */ /* nothing */