Json_lexer.cpp bug
Posted: Fri Mar 30, 2018 4:03 pm
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:
will output
I think it can be traced back to the code in json_lexer.cpp starting at line 442 to 448 which are:
I think line 443 ignores the case when the digit after a negative sign is either 0 or 9.
I changed line 443 to 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.
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;
If variable "d" is set to -9.625, the output is the same.G:-0.625 D:-0.6250
{"DBL":625}
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 changed line 443 to
Code: Select all
if((c>='0') && (c<='9'))
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.