Fixes #904 : Fixes sort_list function to maintain previous node data after running mergesort #908
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #904
Previously, the
sort_list()function, which is called bycJSONUtils_SortObject, wasn't managing theprevdata point of each node, while going through the different steps of mergesort. This caused the->prevvalue of the first item in the list, to point toNULLinstead of pointing to the last item in the list, leading to errors when you try to add new elements to the list.This commit fixes that by tracking and correctly assigning the
prevvalue at each step.Below is a script that tests the
sort_listfunctionality. It creates an object, sorts the object, shows thenextandprevvalues of each element and attempts to add new elements and sort again:The result of running the above script using the previous implementation of
sort_list()is the following:As shown above, once the object is sorted, the
->prevvalue of the first element in the list points toNULLinstead of the last element of the list.node->prevofa2points toNULLinstead ofv1. Once this error is made, users can no longer add to the list, as shown above. Every successive call tocJSON_AddItemToObjectresults in no change at all.Below is the result of running the script with the modifications of
sort_list()proposed in this commit:As seen above, the new
sort_list()ensures that thenode->prevvalue is maintained after sorting. Furthermore, nowcJSON_AddItemToObjectworks as expected.