Json_lexer.cpp bug

Discussion to talk about software related topics only.
Post Reply
ecasey
Posts: 164
Joined: Sat Mar 26, 2011 9:34 pm

Json_lexer.cpp bug

Post by ecasey »

There appears to be a bug in this file where a ParsedJsonDataSet.Add of a double will be wrong for a negative float number when the first digit after the minus sign is either a zero or a nine. The Json output drops the "0" or the "9" and the following decimal point. This code:

Code: Select all

	float d =  - 0.625;
		char tmp[80]; snprintf(tmp,20, "G:%g  D:%f",d,d);
		cout << tmp << endl;
		ParsedJsonDataSet test;
		test.StartBuilding();
		test.Add("DBL",d);
		test.DoneBuilding();
		test.PrintObject();
		cout << endl;
will output
G:-0.625 D:-0.6250
{"DBL":625}
If variable "d" is set to -9.625, the output is the same.

I think it can be traced back to the code in json_lexer.cpp starting at line 442 to 448 which are:

Code: Select all

442	  case STATE_NUM_FIRST_NEG  :
443		 if((c>='1') && (c<'9'))
444			{
445			 AddNumberChar(c);
446			 m_state=STATE_NUM_FIRST_DIG;
447			}
448		 break;
I think line 443 ignores the case when the digit after a negative sign is either 0 or 9.
I changed line 443 to

Code: Select all

		 if((c>='0') && (c<='9'))
and it seems to works fine (note that there are two changes in the line).

I am working in the field on a system controlling a pressure vessel and I don't want to blow it up if I am wrong about the bug. It is a good thing that the bug results in a high side temperature report. I can't do a lot of testing in the field, so I would appreciate if someone could have a look at this and let me know if my fix will not break something else.

Note that there are mechanical safety measures in place that would keep the system from actually causing harm (other than to my reputation).

Thanks

Ed.
User avatar
Forrest
Posts: 283
Joined: Wed Apr 23, 2008 10:05 am

Re: Json_lexer.cpp bug

Post by Forrest »

Hello Ed,

This change looks good. We have patched our source, further releases will contain the fix. Thanks for the submission

Code: Select all

---
 system/webclient/json_lexer.cpp | 4 ++--
 1 file changed, 1 insertions(+), 1 deletions(-)

diff --git a/system/webclient/json_lexer.cpp b/system/webclient/json_lexer.cpp
index eed72ef91..f1d09d487 100644
--- a/system/webclient/json_lexer.cpp
+++ b/system/webclient/json_lexer.cpp
@@ -429,7 +429,7 @@ void ParsedJsonDataSet::ProcessChar( unsigned char c )
 
             break;
         case STATE_NUM_FIRST_NEG:
-            if( (c >= '1') && (c < '9') )
+            if( (c >= '0') && (c <='9') )
             {
                 AddNumberChar( c );
                 m_state = STATE_NUM_FIRST_DIG;

Forrest Stanley
Project Engineer
NetBurner, Inc

NetBurner Learn Articles: http://www.netburner.com/learn
Post Reply