Skip to content

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

Closed
jrobhoward wants to merge 2 commits intojava-native-access:masterfrom
jrobhoward:jhoward/allow_zerolen_xattr_macos
Closed

Avoid exception when getting zero-length xattr on mac#1041
jrobhoward wants to merge 2 commits intojava-native-access:masterfrom
jrobhoward:jhoward/allow_zerolen_xattr_macos

Conversation

@jrobhoward
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants