From 078f6dd69fbadc011f5f26c88ff21f1803ad32ae Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Sat, 20 Aug 2022 01:12:05 +0000 Subject: [PATCH] Fix FastDataOutput.writeInternedUTF() when over 65534 strings. The existing code casts mStringRefs.size() to short before comparing it with 65535, so that the comparison will always evaluate to be smaller even when the size is actually bigger. Instead of trying to play around with short and int conversion, it's more reliable and future-proof to simply represent unsigned short as int - writeShort() is already accepting int instead of short anyway. Bug: 243194720 Test: presubmit Change-Id: Ia2738db57773fa21059b24cc875c07f8facec3c5 --- core/java/com/android/internal/util/FastDataOutput.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/java/com/android/internal/util/FastDataOutput.java b/core/java/com/android/internal/util/FastDataOutput.java index c9e8f8f082290..5b6075ec54ceb 100644 --- a/core/java/com/android/internal/util/FastDataOutput.java +++ b/core/java/com/android/internal/util/FastDataOutput.java @@ -59,7 +59,7 @@ public class FastDataOutput implements DataOutput, Flushable, Closeable { /** * Values that have been "interned" by {@link #writeInternedUTF(String)}. */ - private final HashMap mStringRefs = new HashMap<>(); + private final HashMap mStringRefs = new HashMap<>(); /** * @deprecated callers must specify {@code use4ByteSequence} so they make a @@ -256,7 +256,7 @@ public class FastDataOutput implements DataOutput, Flushable, Closeable { * @see FastDataInput#readInternedUTF() */ public void writeInternedUTF(@NonNull String s) throws IOException { - Short ref = mStringRefs.get(s); + Integer ref = mStringRefs.get(s); if (ref != null) { writeShort(ref); } else { @@ -265,7 +265,7 @@ public class FastDataOutput implements DataOutput, Flushable, Closeable { // We can only safely intern when we have remaining values; if we're // full we at least sent the string value above - ref = (short) mStringRefs.size(); + ref = mStringRefs.size(); if (ref < MAX_UNSIGNED_SHORT) { mStringRefs.put(s, ref); }