Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track JsonFactory in CharsToNameCanonicalizer #1086

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static int collectDefaults() {
* It should not be linked back to the original blueprint, to
* avoid contents from leaking between factories.
*/
protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot();
protected final transient CharsToNameCanonicalizer _rootCharSymbols = CharsToNameCanonicalizer.createRoot(this);

/**
* Alternative to the basic symbol table, some stream-based
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public final class CharsToNameCanonicalizer
*/
final protected AtomicReference<TableInfo> _tableInfo;

/**
* @since 2.16
*/
final protected JsonFactory _jsonFactory;
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved

/**
* Seed value we use as the base to make hash codes non-static between
* different runs, but still stable for lifetime of a single symbol table
Expand Down Expand Up @@ -229,9 +234,10 @@ public final class CharsToNameCanonicalizer
/**
* Main method for constructing a root symbol table instance.
*/
private CharsToNameCanonicalizer(int seed)
private CharsToNameCanonicalizer(JsonFactory jsonFactory, int seed)
{
_parent = null;
_jsonFactory = jsonFactory;
_seed = seed;

// these settings don't really matter for the bootstrap instance
Expand All @@ -253,6 +259,7 @@ private CharsToNameCanonicalizer(CharsToNameCanonicalizer parent, int flags, int
TableInfo parentState)
{
_parent = parent;
_jsonFactory = parent._jsonFactory;
_seed = seed;
_tableInfo = null; // not used by child tables
_flags = flags;
Expand Down Expand Up @@ -288,6 +295,7 @@ private CharsToNameCanonicalizer(CharsToNameCanonicalizer parent, int flags, int
* storing and sharing underlying symbol arrays as needed.
*
* @return Root instance to use for constructing new child instances
* @deprecated use {@link #createRoot(JsonFactory)}
*/
public static CharsToNameCanonicalizer createRoot() {
// Need to use a variable seed, to thwart hash-collision based attacks.
Expand All @@ -299,12 +307,38 @@ public static CharsToNameCanonicalizer createRoot() {
return createRoot(seed);
}


/**
* Method called to create root canonicalizer for a {@link com.fasterxml.jackson.core.JsonFactory}
* instance. Root instance is never used directly; its main use is for
* storing and sharing underlying symbol arrays as needed.
*
* @param jsonFactory the {@link JsonFactory}
* @return Root instance to use for constructing new child instances
* @since 2.16
*/
public static CharsToNameCanonicalizer createRoot(JsonFactory jsonFactory) {
// Need to use a variable seed, to thwart hash-collision based attacks.
// 14-Feb-2017, tatu: not sure it actually helps, at all, since it won't
// change mixing or any of the steps. Should likely just remove in future.
long now = System.currentTimeMillis();
// ensure it's not 0; and might as well require to be odd so:
int seed = (((int) now) + ((int) (now >>> 32))) | 1;
return createRoot(jsonFactory, seed);
}

@Deprecated // since 2.16
protected static CharsToNameCanonicalizer createRoot(int seed) {
return new CharsToNameCanonicalizer(seed);
return new CharsToNameCanonicalizer(null, seed);
}

protected static CharsToNameCanonicalizer createRoot(JsonFactory jsonFactory, int seed) {
return new CharsToNameCanonicalizer(jsonFactory, seed);
}

/**
* "Factory" method; will create a new child instance of this symbol
* table. It will be a copy-on-write instance, ie. it will only use
* table. It will be a copy-on-write instance, i.e. it will only use
* read-only copy of parent's data, but when changes are needed, a
* copy will be created.
*<p>
Expand Down