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

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

@bpintea

Description

@bpintea

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;
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions