Ask Your Question
0

floating point issue in wsgd dissector

asked 2023-01-25 12:38:53 +0000

abs gravatar image

updated 2023-01-25 15:48:42 +0000

i have implemented the following function in a wsgd dissector:

function string hex_and_float (in uint32 value)
{
  hide var uint32 b0 = (value & 0xFF000000) >> 8;
  hide var uint32 b1 = (value & 0x00FF0000) << 8;
  hide var uint32 b2 = (value & 0x0000FF00) >> 8 ;
  hide var uint32 b3 = (value & 0x000000FF) << 8;
  hide var uint32 i = b0 + b1 + b2 + b3;
  hide var string str = print("0x%x (%.2e)", i, i);
  return  str;
}

The string returned is correct for the hexadecimal part but not for the floating part. It seems that the variable i is considered as a 64 bits float. But it's a 32 bit float.

Example of output of hex_and_float():

0x428c4dad (5.52e-315)

How to force the print function to consider i as a 32 bits float ?

edit retag flag offensive close merge delete

Comments

With the disclaimer that all this is very non-portable, but how about:

float *fp = (float *)&i;
float f = *fp;
print("0x%x (%.2e)", i, f);
Jaap gravatar imageJaap ( 2023-01-25 15:25:16 +0000 )edit

The function hex_and_float() is written in WSGD syntax not C.

abs gravatar imageabs ( 2023-01-25 15:47:58 +0000 )edit

The value for a uint32 can exceed the range of a float32.

If you know the value will be less than max for a float32, how about:
hide var float32 flt = i;
or
hide var float32 flt = b0 + b1 + b2 + b3;

Chuckc gravatar imageChuckc ( 2023-01-25 16:38:45 +0000 )edit

Yes the value will always fit in a float32. Already tested:

hide var float32 flt32 = b0 + b1 + b2 + b3;
hide var float64 flt64 = flt32;

It's not working. The issue seems to be located in the print() function. Any number given to %f or %e seems promoted to 64 bits float. It may be related to Wireskark running in 64 bits ?

abs gravatar imageabs ( 2023-01-25 16:49:06 +0000 )edit

byte_interpret.cpp:
promote_printf_string_to_64bits(words[0]);

Maybe open an issue with WSGD? WireShark Generic Dissector -Issues

Chuckc gravatar imageChuckc ( 2023-01-25 16:58:25 +0000 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-06 08:20:05 +0000

abs gravatar image

updated 2023-02-06 08:20:19 +0000

Workaround has been found with help from WSGD developers. cf. https://gitlab.com/wsgd/wsgd/-/issues/4

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-01-25 12:38:00 +0000

Seen: 183 times

Last updated: Jan 25 '23