#1 2011-05-08 04:02:49

Wayne
Member
Registered: 2011-02-22
Posts: 4

Unexpected output of GetJSONObjectAsSQL

Using version 1.12 .. I looked at the notes for 1.13 but didn't see anything related to this.

The handling of null values seems incorrect in GetJSONObjectAsSQL. I think the pointer, "P", doesn't get Inc'd past the null value when GetValue is called. Here's some test code that should illustrate the point. When the parser hits the null values it doesn't update P correctly, resulting in an empty string for the field name, and the actual field name where the value should be.

sReference := '(ID,Name,Role,Last Login,First Login,Department) VALUES (:(1):,:(''Alice''):,:(''User''):,:(null):,:(null):,:(null):,:(''Sales''):)';
sTest := GetJSONObjectAsSQL('{"ID":1,"Name":"Alice","Role":"User","Last Login":null,"First Login":null,"Department":"Sales"}]', false, true);
Check(sReference = sTest);
// Instead output is invalid SQL, with field names and values all mixed up:
// (ID,Name,Role,Last Login,,,Sales) VALUES (:(1):,:('Alice'):,:('User'):,:(null):,:('First Login'):,:('Department'):,:():)

UPDATE: There is also some kind of memory corruption occurring. The string I receive back from GetJSONObjectAsSQL will have varying lengths of garbage at the end of it, sometimes with the contents of previously allocated strings. This occurs only when the JSON string passed to GetJSONObjectAsSQL contains NULL values.

Line 7532 in SQLite3Commons.pas sets the return value of GetValue to 'null', but nothing updates the position of the pointer, P. When the GetValue function returns (@7636), the value of P is such that the "null" value is still in the string. It does not advance to the next field name. Thus, when GetJSONField is called (@7632) on a string like 'null,"First Login":null...' it returns an empty string. This breaks the rest of the parsing, and apparently the size of the string gets set to include bogus memory, resulting in very interesting errors.

Last edited by Wayne (2011-05-10 17:52:04)

Offline

#2 2012-01-23 10:16:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: Unexpected output of GetJSONObjectAsSQL

There was indeed a problem when handling null values in JSON content.

This has been fixed in http://synopse.info/fossil/info/a8dc9c352e

I've added an associated regression test.

Thanks for the report!

Offline

Board footer

Powered by FluxBB