Skip to content

Fix StackOverflowError by deeply nested server-timestamps. #4715

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

Merged
merged 5 commits into from
Mar 8, 2023
Merged
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
1 change: 1 addition & 0 deletions firebase-firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased
* [feature] Add support for disjunctions in queries (`OR` queries).
* [fixed] Fixed stack overflow caused by deeply nested server timestamps (#4702).

# 24.4.4
* [changed] Relaxed certain query validations performed by the SDK (#4231).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public static Value valueOf(Timestamp localWriteTime, @Nullable Value previousVa
.putFields(TYPE_KEY, encodedType)
.putFields(LOCAL_WRITE_TIME_KEY, encodeWriteTime);

if (previousValue != null) {
mapRepresentation.putFields(PREVIOUS_VALUE_KEY, previousValue);
Value actualPreviousValue = previousValue == null ? null : getPreviousValue(previousValue);
if (actualPreviousValue != null) {
mapRepresentation.putFields(PREVIOUS_VALUE_KEY, actualPreviousValue);
}

return Value.newBuilder().setMapValue(mapRepresentation).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,29 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import com.google.firebase.Timestamp;
import com.google.firebase.firestore.FieldValue;
import com.google.firebase.firestore.core.Query;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.FieldIndex;
import com.google.firebase.firestore.model.FieldPath;
import com.google.firebase.firestore.model.ObjectValue;
import com.google.firebase.firestore.model.ServerTimestamps;
import com.google.firebase.firestore.model.mutation.FieldMask;
import com.google.firebase.firestore.model.mutation.FieldTransform;
import com.google.firebase.firestore.model.mutation.PatchMutation;
import com.google.firebase.firestore.model.mutation.Precondition;
import com.google.firebase.firestore.model.mutation.ServerTimestampOperation;
import com.google.firestore.v1.Value;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand Down Expand Up @@ -290,4 +307,60 @@ public void testIndexesServerTimestamps() {
assertOverlayTypes(keyMap("coll/a", CountingQueryEngine.OverlayType.Set));
assertQueryReturned("coll/a");
}

@Test
public void testDeeplyNestedServerTimestamps() {
Timestamp timestamp = Timestamp.now();
Value initialServerTimestamp = ServerTimestamps.valueOf(timestamp, null);
Map<String, Value> fields =
new HashMap<String, Value>() {
{
put("timestamp", ServerTimestamps.valueOf(timestamp, initialServerTimestamp));
}
};
FieldPath path = FieldPath.fromSingleSegment("timestamp");
FieldMask mask =
FieldMask.fromSet(
new HashSet<FieldPath>() {
{
add(path);
}
});
FieldTransform fieldTransform =
new FieldTransform(path, ServerTimestampOperation.getInstance());
List<FieldTransform> fieldTransforms =
new ArrayList<FieldTransform>() {
{
add(fieldTransform);
}
};
AtomicReference<Throwable> error = new AtomicReference<>();
Thread thread =
new Thread(
Thread.currentThread().getThreadGroup(),
() -> {
try {
for (int i = 0; i < 1000; ++i) {
writeMutation(
new PatchMutation(
DocumentKey.fromPathString("some/object/for/test"),
ObjectValue.fromMap(fields),
mask,
Precondition.NONE,
fieldTransforms));
}
} catch (Throwable e) {
error.set(e);
}
},
"test",
1024 * 1024);
try {
thread.start();
thread.join();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
assertThat(error.get()).isNull();
}
}