plist: fix inverted strcmp in string to boolean conversion - #317
Open
arpitjain099 wants to merge 1 commit into
Open
plist: fix inverted strcmp in string to boolean conversion#317arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
plist_dict_get_bool() compared the string value with strcmp() but treated a non-zero return as a match. strcmp() returns 0 on equality, so the conditions were inverted: "true" produced 0, "false" produced 1, and any other string also produced 1. The error branch could never be reached, since it required both comparisons to return 0 at once. The result is that the API returns the opposite of the stored value for both valid boolean strings, and returns true for strings that are not booleans at all, instead of reporting the conversion error. Compare == 0 in both conditions: input before after true 0 1 false 1 0 not-a-bool 1 error TRUE 1 error (empty) 1 error Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
plist_dict_get_bool()compares the string value withstrcmp()but treats a non-zero return as a match.strcmp()returns 0 on equality, so both conditions are inverted:"true"takes the first comparison to 0, falls to theelse if, wherestrcmp("true", "false")is non-zero, and setsbval = 0."false"matches the first condition and setsbval = 1. Anything else also setsbval = 1. The error branch is unreachable, because reaching it would need both comparisons to return 0 for the same string.Behaviour of the two forms:
So today the API returns the opposite of the stored value for both valid boolean strings, and returns true for strings that are not booleans, rather than reporting the conversion error that the
PLIST_ERRcall was written for.The fix compares
== 0in both conditions.plist_dict_copy_bool()goes through the same helper and is fixed by the same change.Worth knowing for anyone reading this later: applications that use
plist_dict_get_bool()on parsed input to read a flag have been getting the inverted answer, so a value of"false"in a plist or JSON document reads as true.