Skip to content

Commit

Permalink
Avoid exception when getting zero-length xattrs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasblaesing committed Dec 8, 2018
1 parent f67b15e commit fa5bc34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bug Fixes
---------
* [#1036](https://github.com/java-native-access/jna/issues/1036): `Advapi32Util.registryValueExists` called on non existing key raises exception instead of returning `false` - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#384](https://github.com/java-native-access/jna/issues/384): Android only supports loading libraries through the JVM `System#loadLibrary` mechanism, defaulting `jna.nosys` to `true` disabled that code path - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1041](https://github.com/java-native-access/jna/pull/1041): Avoid IllegalArgumentException when reading xattrs with zero length - [@jrobhoward](https://github.com/jrobhoward).

Release 5.1.0
=============
Expand Down
12 changes: 12 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/linux/XAttrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ public static Memory getXAttrAsMemory(String path, String name) throws IOExcepti
throw new IOException("errno: " + eno);
}

if (retval.longValue() == 0) {
return null;
}

valueMem = new Memory(retval.longValue());
retval = XAttr.INSTANCE.getxattr(path, name, valueMem, new size_t(valueMem.size()));
if (retval.longValue() < 0) {
Expand Down Expand Up @@ -352,6 +356,10 @@ public static Memory lGetXAttrAsMemory(String path, String name) throws IOExcept
throw new IOException("errno: " + eno);
}

if (retval.longValue() == 0) {
return null;
}

valueMem = new Memory(retval.longValue());
retval = XAttr.INSTANCE.lgetxattr(path, name, valueMem, new size_t(valueMem.size()));
if (retval.longValue() < 0) {
Expand Down Expand Up @@ -445,6 +453,10 @@ public static Memory fGetXAttrAsMemory(int fd, String name) throws IOException {
throw new IOException("errno: " + eno);
}

if (retval.longValue() == 0) {
return null;
}

valueMem = new Memory(retval.longValue());
retval = XAttr.INSTANCE.fgetxattr(fd, name, valueMem, new size_t(valueMem.size()));
if (retval.longValue() < 0) {
Expand Down
3 changes: 3 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public static String getXAttr(String path, String name) {
if (bufferLength < 0)
return null;

if (bufferLength == 0)
return "";

Memory valueBuffer = new Memory(bufferLength);
long valueLength = XAttr.INSTANCE.getxattr(path, name, valueBuffer, bufferLength, 0, 0);

Expand Down

0 comments on commit fa5bc34

Please sign in to comment.