Signal error on field values that cannot be safely represented as strings instead of crashing.

This fixes bug 2845.

svn path=/trunk/; revision=26749
This commit is contained in:
Balint Reczey 2008-11-11 13:32:51 +00:00
parent f9c07203cb
commit 41723a5ef2
4 changed files with 29 additions and 8 deletions

View File

@ -130,7 +130,7 @@ val_repr_len(fvalue_t *fv, ftrepr_t rtype)
{ {
guint length; guint length;
g_assert(rtype == FTREPR_DFILTER); if (rtype != FTREPR_DFILTER) return -1;
length = tvb_length(fv->value.tvb); length = tvb_length(fv->value.tvb);
/* 3 bytes for each byte of the byte "NN:" minus 1 byte /* 3 bytes for each byte of the byte "NN:" minus 1 byte
* as there's no trailing ":". */ * as there's no trailing ":". */

View File

@ -303,7 +303,13 @@ fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf)
{ {
g_assert(fv->ftype->val_to_string_repr); g_assert(fv->ftype->val_to_string_repr);
if (!buf) { if (!buf) {
buf = g_malloc0(fvalue_string_repr_len(fv, rtype) + 1); int len;
if ((len = fvalue_string_repr_len(fv, rtype)) >= 0) {
buf = g_malloc0(len + 1);
} else {
/* the value cannot be represented in the given representation type (rtype) */
return NULL;
}
} }
fv->ftype->val_to_string_repr(fv, rtype, buf); fv->ftype->val_to_string_repr(fv, rtype, buf);
return buf; return buf;

View File

@ -280,6 +280,9 @@ fvalue_from_string(ftenum_t ftype, char *s, LogFunc logfunc);
/* Returns the length of the string required to hold the /* Returns the length of the string required to hold the
* string representation of the the field value. * string representation of the the field value.
*
* Returns -1 if the string cannot be represented in the given rtype.
*
* The length DOES NOT include the terminating NUL. */ * The length DOES NOT include the terminating NUL. */
int int
fvalue_string_repr_len(fvalue_t *fv, ftrepr_t rtype); fvalue_string_repr_len(fvalue_t *fv, ftrepr_t rtype);
@ -291,7 +294,9 @@ fvalue_string_repr_len(fvalue_t *fv, ftrepr_t rtype);
* The pointer to the beginning of the string representation is * The pointer to the beginning of the string representation is
* returned. If 'buf' was NULL, this points to the newly-allocated * returned. If 'buf' was NULL, this points to the newly-allocated
* memory. if 'buf' was non-NULL, then the return value will be * memory. if 'buf' was non-NULL, then the return value will be
* 'buf'. */ * 'buf'.
*
* Returns NULL if the string cannot be represented in the given rtype.*/
extern char * extern char *
fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf); fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf);

View File

@ -125,9 +125,15 @@ WSLUA_METAMETHOD FieldInfo__call(lua_State* L) {
return 1; return 1;
} }
case FT_STRING: case FT_STRING:
case FT_STRINGZ: case FT_STRINGZ: {
lua_pushstring(L,fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL)); gchar* repr = fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
if (repr)
lua_pushstring(L,repr);
else
luaL_error(L,"field cannot be represented as string because it may contain invalid characters");
return 1; return 1;
}
case FT_BYTES: case FT_BYTES:
case FT_UINT_BYTES: case FT_UINT_BYTES:
case FT_GUID: case FT_GUID:
@ -147,9 +153,13 @@ WSLUA_METAMETHOD FieldInfo__tostring(lua_State* L) {
/* the string representation of the field */ /* the string representation of the field */
FieldInfo fi = checkFieldInfo(L,1); FieldInfo fi = checkFieldInfo(L,1);
if (fi) { if (fi) {
if (fi->value.ftype->val_to_string_repr) if (fi->value.ftype->val_to_string_repr) {
lua_pushstring(L,fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL)); gchar* repr = fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
if (repr)
lua_pushstring(L,repr);
else else
luaL_error(L,"field cannot be represented as string because it may contain invalid characters");
} else
luaL_error(L,"field has no string representation"); luaL_error(L,"field has no string representation");
} }
return 1; return 1;