Skip to content
Permalink
Browse files
8277093: Vector should throw ClassNotFoundException for a missing cla…
…ss of an element

Reviewed-by: smarks
  • Loading branch information
Roger Riggs committed Nov 23, 2021
1 parent ed45df2 commit 857163336f357f01e1c0a5061214ab7d18133c1b
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1158,6 +1158,12 @@ private void readObject(ObjectInputStream in)
ObjectInputStream.GetField gfields = in.readFields();
int count = gfields.get("elementCount", 0);
Object[] data = (Object[])gfields.get("elementData", null);
if (data == null && !gfields.defaulted("elementData") && count > 0) {
// If elementData is null due to 8276665 throwing this exception will not
// overwrite the original ClassNotFoundException exception.
// That exception has been recorded and will be thrown from OIS.readObject.
throw new ClassNotFoundException("elementData is null");
}
if (count < 0 || data == null || count > data.length) {
throw new StreamCorruptedException("Inconsistent vector internals");
}
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8273660
* @summary The class of an element of a Vector may not be found; test that Vector allows
* the CNFE to be thrown.
* @run testng VectorElementCNFE
*/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.nio.charset.StandardCharsets;
import java.util.Vector;

import org.testng.annotations.Test;

import org.testng.Assert;

public class VectorElementCNFE {

/**
* Test a Vector holding a reference to a class instance that will not be found.
* @throws IOException If any other exception occurs
*/
@Test
private static void test1() throws IOException {

Role role = new Role();
Vector<Role> vector = new Vector<>();
vector.add(role);

// Modify the byte stream to change the classname to be deserialized to
// XectorElementCNFE$Role.
byte[] bytes = writeObject(vector);

String s = new String(bytes, StandardCharsets.ISO_8859_1); // Map bytes to chars
int off = s.indexOf(Role.class.getName());
Assert.assertTrue(off >= 0, "classname Role not found");

System.out.println("Clasname Role offset: " + off);
bytes[off] = (byte) 'X'; // replace V with X -> Class not found

// Deserialize the Vector expecting a ClassNotFoundException
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
try {
Object obj = in.readObject();
System.out.println("Read: " + obj);
Assert.fail("Should not reach here, an exception should always occur");
} catch (ClassNotFoundException cnfe) {
// Expected ClassNotFoundException
String expected = "XectorElementCNFE$Role";
Assert.assertEquals(expected, cnfe.getMessage(), "Wrong classname");
System.out.println("Normal: " + cnfe);
}
// Other exceptions cause the test to fail
}

/**
* Test deserializing a Vector in which there is no "elementData" field.
* @throws IOException If any other exception occurs
*/
@Test
private static void test2() throws IOException {

Role role = new Role();
Vector<Role> vector = new Vector<>();
vector.add(role);

// Modify the byte stream effectively remove the "elementData" field
// by changing fieldName to be deserialized to "XelementData".
byte[] bytes = writeObject(vector);

String s = new String(bytes, StandardCharsets.ISO_8859_1); // Map bytes to chars
int off = s.indexOf("elementData");
Assert.assertTrue(off >= 0, "field elementData not found");

System.out.println("elementData offset: " + off);
bytes[off] = (byte) 'X'; // replace 'e' with X -> field elementData not found

// Deserialize the Vector expecting a StreamCorruptedException
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
try {
Object obj = in.readObject();
System.out.println("Read: " + obj);
Assert.fail("Should not reach here, an exception should always occur");
} catch (StreamCorruptedException sce) {
// Expected StreamCorruptedException
String expected = "Inconsistent vector internals";
Assert.assertEquals(expected, sce.getMessage(), "Wrong exception message");
System.out.println("Normal: " + sce);
} catch (ClassNotFoundException cnfe) {
Assert.fail("CNFE not expected", cnfe);
}
// Other exceptions cause the test to fail
}

private static byte[] writeObject(Object o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream os = new ObjectOutputStream(baos)) {
os.writeObject(o);
}
return baos.toByteArray();
}

static class Role implements Serializable {
private static final long serialVersionUID = 0L;

Role() {}
}
}

3 comments on commit 8571633

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 8571633 Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 8571633 Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 8571633 Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin Could not automatically backport 85716333 to openjdk/jdk11u-dev due to conflicts in the following files:

  • src/java.base/share/classes/java/util/Vector.java

To manually resolve these conflicts run the following commands in your personal fork of openjdk/jdk11u-dev:

$ git checkout -b GoeLin-backport-85716333
$ git fetch --no-tags https://git.openjdk.java.net/jdk17u 857163336f357f01e1c0a5061214ab7d18133c1b
$ git cherry-pick --no-commit 857163336f357f01e1c0a5061214ab7d18133c1b
$ # Resolve conflicts
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 857163336f357f01e1c0a5061214ab7d18133c1b'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk11u-dev with the title Backport 857163336f357f01e1c0a5061214ab7d18133c1b.

Please sign in to comment.