Skip to content

Commit bc22845

Browse files
Avoid exception when getting zero-length xattrs
Closes: #1041
1 parent f67b15e commit bc22845

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Bug Fixes
1414
---------
1515
* [#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).
1616
* [#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).
17+
* [#1041](https://github.com/java-native-access/jna/pull/1041): Avoid IllegalArgumentException when reading xattrs with zero length - [@jrobhoward](https://github.com/jrobhoward).
1718

1819
Release 5.1.0
1920
=============

contrib/platform/src/com/sun/jna/platform/linux/XAttrUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ public static Memory getXAttrAsMemory(String path, String name) throws IOExcepti
255255
throw new IOException("errno: " + eno);
256256
}
257257

258+
if (retval.longValue() == 0) {
259+
return null;
260+
}
261+
258262
valueMem = new Memory(retval.longValue());
259263
retval = XAttr.INSTANCE.getxattr(path, name, valueMem, new size_t(valueMem.size()));
260264
if (retval.longValue() < 0) {
@@ -352,6 +356,10 @@ public static Memory lGetXAttrAsMemory(String path, String name) throws IOExcept
352356
throw new IOException("errno: " + eno);
353357
}
354358

359+
if (retval.longValue() == 0) {
360+
return null;
361+
}
362+
355363
valueMem = new Memory(retval.longValue());
356364
retval = XAttr.INSTANCE.lgetxattr(path, name, valueMem, new size_t(valueMem.size()));
357365
if (retval.longValue() < 0) {
@@ -445,6 +453,10 @@ public static Memory fGetXAttrAsMemory(int fd, String name) throws IOException {
445453
throw new IOException("errno: " + eno);
446454
}
447455

456+
if (retval.longValue() == 0) {
457+
return null;
458+
}
459+
448460
valueMem = new Memory(retval.longValue());
449461
retval = XAttr.INSTANCE.fgetxattr(fd, name, valueMem, new size_t(valueMem.size()));
450462
if (retval.longValue() < 0) {

contrib/platform/src/com/sun/jna/platform/mac/XAttrUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public static String getXAttr(String path, String name) {
5858
if (bufferLength < 0)
5959
return null;
6060

61+
if (bufferLength == 0)
62+
return "";
63+
6164
Memory valueBuffer = new Memory(bufferLength);
6265
long valueLength = XAttr.INSTANCE.getxattr(path, name, valueBuffer, bufferLength, 0, 0);
6366

contrib/platform/test/com/sun/jna/platform/linux/XAttrUtilTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.sun.jna.platform.linux;
2626

27+
import com.sun.jna.Memory;
2728
import java.io.File;
2829
import java.io.IOException;
2930
import java.util.Collection;
@@ -33,12 +34,16 @@
3334
import static org.junit.Assert.assertEquals;
3435
import static org.junit.Assert.assertFalse;
3536
import static org.junit.Assert.assertTrue;
37+
import static org.junit.Assert.assertNull;
38+
import static org.junit.Assert.assertNotNull;
3639

3740
public class XAttrUtilTest {
3841
private static final String TEST_STRING = "Žluťoučký kůň úpěl nebo tak něco.";
3942
private static final String TEST_STRING_2 = "Příliš žluťoučký kůň úpěl ďábelské ódy.";
43+
private static final String TEST_EMPTY_STRING = "";
4044
private static final String TEST_ATTRIBUTE = "user.test";
4145
private static final String TEST_ATTRIBUTE_FOO = TEST_ATTRIBUTE + ".foo";
46+
private static final String TEST_ATTRIBUTE_EMPTY = TEST_ATTRIBUTE + ".empty";
4247

4348
@Test
4449
public void setXAttr() throws IOException {
@@ -66,5 +71,26 @@ public void setXAttr() throws IOException {
6671
xattrs = XAttrUtil.lListXAttr(file.getAbsolutePath());
6772
assertFalse(xattrs.contains(TEST_ATTRIBUTE));
6873
assertTrue(xattrs.contains(TEST_ATTRIBUTE_FOO));
74+
75+
file.delete();
76+
}
77+
78+
@Test
79+
public void testGetXAttr() throws IOException {
80+
File file = File.createTempFile("xattr", "test");
81+
file.deleteOnExit();
82+
83+
XAttrUtil.setXAttr(file.getAbsolutePath(), TEST_ATTRIBUTE_EMPTY, TEST_EMPTY_STRING);
84+
85+
Memory memoryReadMissing = XAttrUtil.getXAttrAsMemory(file.getAbsolutePath(), TEST_ATTRIBUTE_EMPTY);
86+
byte[] byteReadMissing = XAttrUtil.getXAttrBytes(file.getAbsolutePath(), TEST_ATTRIBUTE_EMPTY);
87+
String stringReadMissing = XAttrUtil.getXAttr(file.getAbsolutePath(), TEST_ATTRIBUTE_EMPTY);
88+
assertNull(memoryReadMissing);
89+
assertNotNull(byteReadMissing);
90+
assertEquals(0, byteReadMissing.length);
91+
assertNotNull(stringReadMissing);
92+
assertTrue(stringReadMissing.isEmpty());
93+
94+
file.delete();
6995
}
7096
}

contrib/platform/test/com/sun/jna/platform/mac/XAttrUtilTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package com.sun.jna.platform.mac;
1414

15+
import com.sun.jna.Memory;
16+
import com.sun.jna.Pointer;
1517
import java.io.File;
1618
import java.util.Arrays;
1719
import java.util.List;
@@ -58,6 +60,10 @@ public void testGetXAttr() {
5860
value = XAttrUtil.getXAttr(testPath, "JNA");
5961

6062
assertEquals(Arrays.toString("Java Native Access".getBytes()), Arrays.toString(value.getBytes()));
63+
64+
XAttr.INSTANCE.setxattr(testPath, "JNA.empty", Pointer.NULL, 0, 0, 0);
65+
value = XAttrUtil.getXAttr(testPath, "JNA.empty");
66+
assertEquals("", value);
6167
}
6268

6369
public void testSetXAttr() {

0 commit comments

Comments
 (0)