8049225: Source class exposes public mutable array

Reviewed-by: hannesw, sundar
This commit is contained in:
Attila Szegedi 2014-07-03 11:18:38 +02:00
parent 1377f6380e
commit c6dda4ed20
3 changed files with 21 additions and 19 deletions
nashorn/src/jdk/nashorn/internal

@ -78,7 +78,7 @@ public final class OptimisticTypesPersistence {
}
final StringBuilder b = new StringBuilder(48);
// Base64-encode the digest of the source, and append the function id.
b.append(Base64.getUrlEncoder().encodeToString(source.getDigest())).append('-').append(functionId);
b.append(source.getDigest()).append('-').append(functionId);
// Finally, if this is a parameter-type specialized version of the function, add the parameter types to the file
// name.
if(paramTypes != null && paramTypes.length > 0) {
@ -286,7 +286,7 @@ public final class OptimisticTypesPersistence {
for(;;) {
final int l = in.read(buf);
if(l == -1) {
return Base64.getUrlEncoder().encodeToString(digest.digest());
return Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest());
}
digest.update(buf, 0, l);
}

@ -37,7 +37,6 @@ import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Base64;
import java.util.Map;
/**
@ -48,9 +47,6 @@ final class CodeStore {
private final File dir;
private final int minSize;
// Message digest to file name encoder
private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
// Default minimum size for storing a compiled script class
private final static int DEFAULT_MIN_SIZE = 1000;
@ -108,8 +104,7 @@ final class CodeStore {
return null;
}
final String digest = BASE64.encodeToString(source.getDigest());
final File file = new File(dir, digest);
final File file = new File(dir, source.getDigest());
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<CompiledScript>() {
@ -157,8 +152,7 @@ final class CodeStore {
}
}
final String digest = BASE64.encodeToString(source.getDigest());
final File file = new File(dir, digest);
final File file = new File(dir, source.getDigest());
final CompiledScript script = new CompiledScript(source, mainClassName, classBytes, constants);
try {

@ -45,6 +45,7 @@ import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Objects;
import java.util.WeakHashMap;
import jdk.nashorn.api.scripting.URLReader;
@ -52,7 +53,6 @@ import jdk.nashorn.internal.parser.Token;
import jdk.nashorn.internal.runtime.logging.DebugLogger;
import jdk.nashorn.internal.runtime.logging.Loggable;
import jdk.nashorn.internal.runtime.logging.Logger;
/**
* Source objects track the origin of JavaScript entities.
*/
@ -61,6 +61,9 @@ public final class Source implements Loggable {
private static final int BUF_SIZE = 8 * 1024;
private static final Cache CACHE = new Cache();
// Message digest to file name encoder
private final static Base64.Encoder BASE64 = Base64.getUrlEncoder().withoutPadding();
/**
* Descriptive name of the source as supplied by the user. Used for error
* reporting to the user. For example, SyntaxError will use this to print message.
@ -81,8 +84,8 @@ public final class Source implements Loggable {
/** Cached hash code */
private int hash;
/** Message digest */
private byte[] digest;
/** Base64-encoded SHA1 digest of this source object */
private volatile byte[] digest;
// Do *not* make this public, ever! Trusts the URL and content.
private Source(final String name, final String base, final Data data) {
@ -782,12 +785,17 @@ public final class Source implements Loggable {
}
/**
* Get a message digest for this source.
* Get a Base64-encoded SHA1 digest for this source.
*
* @return a message digest for this source
* @return a Base64-encoded SHA1 digest for this source
*/
public synchronized byte[] getDigest() {
if (digest == null) {
public String getDigest() {
return new String(getDigestBytes(), StandardCharsets.US_ASCII);
}
private byte[] getDigestBytes() {
byte[] ldigest = digest;
if (ldigest == null) {
final char[] content = data();
final byte[] bytes = new byte[content.length * 2];
@ -807,12 +815,12 @@ public final class Source implements Loggable {
if (getURL() != null) {
md.update(getURL().toString().getBytes(StandardCharsets.UTF_8));
}
digest = md.digest(bytes);
digest = ldigest = BASE64.encode(md.digest(bytes));
} catch (final NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
return digest;
return ldigest;
}
/**