GithubHelp home page GithubHelp logo

ujson4c's People

Contributors

uucidl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ujson4c's Issues

BUG: no memory allocation check

In case of a NULL hf parameter, UJDecode will allocate an initial heap (initialHeap) and start deref'ing it with no check of allocation result.

BUG: UJObjectUnpack doesn't respect output objects sequence

The function tries to match decoded JSON objects to the given keys received as arguments and store the corresponding objects back into the out parameters.
The bug stems from the fact that walking the list of va arguments is synchronised with the iteration over the members of the decoded object, not the keys. Since the order of the members into a JSON object can vary, the allocation to the output references can be wrong.

Also, the usage of va_arg in the function receives the wrong type paramter: UJObject instead of UJObject *, though this has little consequence, since both are pointers.

This is likely the cause of #7.

Below is a proposed patch to fix the issue (as well the local mixing of tabs and WSes).

index 4b5a62d..161d909 100644
--- a/src/ujdecode.c
+++ b/src/ujdecode.c
@@ -686,10 +686,10 @@ int UJObjectUnpack(UJObject objObj, int keys, const char *format, const wchar_t
 	int ki;
 	int ks = 0;
 	const wchar_t *keyNames[64];
-  va_list args;
-  UJObject *outValue;
+	UJObject *outValues[64];
+	va_list args;
+	UJObject *outValue;
 
-  va_start(args, _keyNames);
 
   if (!UJIsObject(objObj))
 	{
@@ -703,10 +703,14 @@ int UJObjectUnpack(UJObject objObj, int keys, const char *format, const wchar_t
 		return -1;
 	}
 
+	va_start(args, _keyNames);
 	for (ki = 0; ki < keys; ki ++)
 	{
 		keyNames[ki] = _keyNames[ki];
+		outValue = va_arg(args, UJObject *);
+		outValues[ki] = outValue;
 	}
+	va_end(args);
 	
 	while (UJIterObject(&iter, &key, &value))
 	{
@@ -731,12 +735,10 @@ int UJObjectUnpack(UJObject objObj, int keys, const char *format, const wchar_t
 
 			found ++;
 
-      outValue = va_arg(args, UJObject);
-
-      if (outValue != NULL)
-      {
-  			*outValue = value;
-      }
+			if (outValues[ki]) 
+			{
+				*outValues[ki] = value;
+			}
 			keyNames[ki] = NULL;
 
 			if (ki == ks)
@@ -746,7 +748,6 @@ int UJObjectUnpack(UJObject objObj, int keys, const char *format, const wchar_t
 		}
 	}
 
-  va_end(args);
 
 	return found;
 }

Typo/Bug int UJIsInteger(UJObject *obj);

*int UJIsInteger(UJObject obj);

This check function is requesting pointer to a UJObject which is different from all the other check functions. Should be UJObject as the internal code also does not handle pointer to a UJObject

BUG: parsed string length not correctly considered

The library does not correctly consider the length when parsing the input string. If the string is not a NTS, the library will read past the indicated limit.

If the library is passed a correctly formatted JSON and the character next to the indicated end is a non-white space character, the parsing will succeed. But:

  • if the JSON is incorrect, the parsing can crash;
  • if the end+1 character is a white string, the parsing will incorrectly fail (for a correct JSON string).

dump as well as load functionality

ujson4c is great, I'm using it in a julia package uJSON.jl.

Is there any chance of adding "dump" functionality here as well as "load" so I don't have to route about in ujson package?

Object unpack may value to wrong output.

If one of the keys parsed by UJObjectUnpack() could is not found, the order which the found keys are stored in output objects is wrong. For instance, following code:

#include <stdio.h>
#include "ujdecode.h"

int main() {
	UJObject obj;
	void *state;
	const char input[] = "{\"a\": {}, \"b\": 200 }";
	size_t cbInput = sizeof(input) - 1;

	const wchar_t *keys[] = { L"a", L"b"};
	UJObject a, b;

	obj = UJDecode(input, cbInput, NULL, &state);

	int ret = UJObjectUnpack(obj, 2, "NN", keys, &a, &b);
	printf("Return value: %d, a = %g, b = %g\n", ret, UJNumericFloat(a), UJNumericFloat(b));

	UJFree(state);
}

will output:

Return value: 1, a = 200, b = 0

Crash on malformed json input.

Following code is enough to crash the program, in function UJObjectUnpack():

#include <stdio.h>
#include "ujdecode.h"

int main() {
	UJObject obj;
	void *state;
	const char input[] = "{\"a\": {}, \"b\": 200 } }";
	size_t cbInput = sizeof(input) - 1;

	const wchar_t *keys[] = { L"a", L"b"};
	UJObject a, b;

	obj = UJDecode(input, cbInput, NULL, &state);

	UJObjectUnpack(obj, 2, "ON", keys, &a, &b);

	UJFree(state);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.