Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Avoid exception when getting zero-length xattr on mac #1041

Conversation

jrobhoward
Copy link

On MacOS, reading a zero-sized xattr will result in an IllegalArgumentException:

Exception in thread "main" java.lang.IllegalArgumentException: Allocation size must be greater than zero
	at com.sun.jna.Memory.<init>(Memory.java:111)
	at com.sun.jna.platform.mac.XAttrUtil.getXAttr(XAttrUtil.java:61)

The code change associated with this pull request returns an empty string before attempting to allocate Memory(0).

  • Allowing the caller to distinguish between (an xattr that does not exist, return value=null) and (an empty xattr, return value="").

This only appears to be a problem on MacOS' implementation of getXAttr(). Linux will return an empty string.

  • The Linux version does have getXAttrAsMemory() methods that will produce a similar problem, but I'm not sure what would be the most appropriate: (IllegalArgumentException, IOException, null return value) --so I didn't make any changes.

@matthiasblaesing
Copy link
Member

Thank you - the change looks reasonable. Could you please also add an entry to the Bugfixes section of CHANGES.md? Thank you.

For the other methods: I would not use exceptions - it is not exceptional, that a value is not set/set to the empty string. So if a method returns a Memory object and a length of 0 is indicated, I would return null for the Memory object. For a byte[] there is a valid value representing the "empty" byte array.

@jrobhoward
Copy link
Author

I added the changelog entry, and updated the Linux XAttrUtil implementation so it returns null instead of throwing exceptions when attempting to create zero-length Memory objects. I did not alter getXAttrBytes(), since it should already be returning a valid zero-byte array.

matthiasblaesing added a commit to matthiasblaesing/jna that referenced this pull request Dec 8, 2018
@matthiasblaesing
Copy link
Member

Merged via: bc22845

Your changes are completely included - I added unittests to verify the behavior and moved the changelog entry to the correct place.

I have to apologize, the commit only lists me as author - I did multiple rebases and somewhere along the way, your authorship got lost. I ensured though, that you are listed as author in the CHANGES.md.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants