From 5f9b323eb09affbccff43d31a7026a911121e89c Mon Sep 17 00:00:00 2001 From: yliu Date: Fri, 28 Aug 2015 10:53:53 +0800 Subject: [PATCH] HDFS-8963. Fix incorrect sign extension of xattr length in HDFS-8900. (Colin Patrick McCabe via yliu) --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ .../hadoop/hdfs/server/namenode/XAttrFormat.java | 14 ++++++++++---- .../hdfs/server/namenode/TestXAttrFeature.java | 12 ++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index a6c0e74211a..e6e11dd0c5e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -899,6 +899,9 @@ Release 2.8.0 - UNRELEASED HDFS-8969. Clean up findbugs warnings for HDFS-8823 and HDFS-8932. (Anu Engineer via wheat9) + HDFS-8963. Fix incorrect sign extension of xattr length in HDFS-8900. + (Colin Patrick McCabe via yliu) + Release 2.7.2 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/XAttrFormat.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/XAttrFormat.java index 6167dac445e..7e704d0226c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/XAttrFormat.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/XAttrFormat.java @@ -61,12 +61,15 @@ class XAttrFormat { for (int i = 0; i < attrs.length;) { XAttr.Builder builder = new XAttr.Builder(); // big-endian - int v = Ints.fromBytes(attrs[i++], attrs[i++], attrs[i++], attrs[i++]); + int v = Ints.fromBytes(attrs[i], attrs[i + 1], + attrs[i + 2], attrs[i + 3]); + i += 4; int ns = (v >> XATTR_NAMESPACE_OFFSET) & XATTR_NAMESPACE_MASK; int nid = v & XATTR_NAME_MASK; builder.setNameSpace(XATTR_NAMESPACE_VALUES[ns]); builder.setName(XAttrStorage.getName(nid)); - int vlen = (attrs[i++] << 8) | attrs[i++]; + int vlen = ((0xff & attrs[i]) << 8) | (0xff & attrs[i + 1]); + i += 2; if (vlen > 0) { byte[] value = new byte[vlen]; System.arraycopy(attrs, i, value, 0, vlen); @@ -94,12 +97,15 @@ class XAttrFormat { XAttr xAttr = XAttrHelper.buildXAttr(prefixedName); for (int i = 0; i < attrs.length;) { // big-endian - int v = Ints.fromBytes(attrs[i++], attrs[i++], attrs[i++], attrs[i++]); + int v = Ints.fromBytes(attrs[i], attrs[i + 1], + attrs[i + 2], attrs[i + 3]); + i += 4; int ns = (v >> XATTR_NAMESPACE_OFFSET) & XATTR_NAMESPACE_MASK; int nid = v & XATTR_NAME_MASK; XAttr.NameSpace namespace = XATTR_NAMESPACE_VALUES[ns]; String name = XAttrStorage.getName(nid); - int vlen = (attrs[i++] << 8) | attrs[i++]; + int vlen = ((0xff & attrs[i]) << 8) | (0xff & attrs[i + 1]); + i += 2; if (xAttr.getNameSpace() == namespace && xAttr.getName().equals(name)) { if (vlen > 0) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestXAttrFeature.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestXAttrFeature.java index fcb157ee07d..5b0922d0913 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestXAttrFeature.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestXAttrFeature.java @@ -43,6 +43,14 @@ public class TestXAttrFeature { static final String name7 = "raw.a7"; static final byte[] value7 = {0x011, 0x012, 0x013}; static final String name8 = "user.a8"; + static final String bigXattrKey = "user.big.xattr.key"; + static final byte[] bigXattrValue = new byte[128]; + + static { + for (int i = 0; i < bigXattrValue.length; i++) { + bigXattrValue[i] = (byte) (i & 0xff); + } + } static byte[] randomBytes(int len) { Random rand = new Random(); @@ -74,12 +82,14 @@ public class TestXAttrFeature { XAttr a5 = XAttrHelper.buildXAttr(name5, value5); XAttr a6 = XAttrHelper.buildXAttr(name6, value6); XAttr a7 = XAttrHelper.buildXAttr(name7, value7); + XAttr bigXattr = XAttrHelper.buildXAttr(bigXattrKey, bigXattrValue); xAttrs.add(a2); xAttrs.add(a3); xAttrs.add(a4); xAttrs.add(a5); xAttrs.add(a6); xAttrs.add(a7); + xAttrs.add(bigXattr); feature = new XAttrFeature(xAttrs); XAttr r2 = feature.getXAttr(name2); @@ -94,6 +104,8 @@ public class TestXAttrFeature { assertTrue(a6.equals(r6)); XAttr r7 = feature.getXAttr(name7); assertTrue(a7.equals(r7)); + XAttr rBigXattr = feature.getXAttr(bigXattrKey); + assertTrue(bigXattr.equals(rBigXattr)); List rs = feature.getXAttrs(); assertEquals(rs.size(), xAttrs.size()); for (int i = 0; i < rs.size(); i++) {