Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

BUG: UJObjectUnpack doesn't respect output objects sequence #10

Open
bpintea opened this issue Feb 19, 2018 · 0 comments
Open

BUG: UJObjectUnpack doesn't respect output objects sequence #10

bpintea opened this issue Feb 19, 2018 · 0 comments

Comments

@bpintea
Copy link

bpintea commented Feb 19, 2018

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;
 }
bpintea added a commit to elastic/elasticsearch-sql-odbc that referenced this issue Feb 19, 2018
- fix a bug in UJObjectUnpack() function with assignment of output
  parameters ( esnme/ujson4c#10 )
- fix a bug in UUJDecod() function with un-checked mem allocation
  ( esnme/ujson4c#9 )
bpintea added a commit to elastic/elasticsearch-sql-odbc that referenced this issue Jun 4, 2018
- fix a bug in UJObjectUnpack() function with assignment of output
  parameters ( esnme/ujson4c#10 )
- fix a bug in UUJDecod() function with un-checked mem allocation
  ( esnme/ujson4c#9 )
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant