From a99dca7e95de7d12f4d7b37e6b82e2bf9f6eed30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ho=C3=A0ng=20Gia=20B=E1=BA=A3o?= <70064328+YT-Advanced@users.noreply.github.com> Date: Sun, 11 Feb 2024 13:28:17 +0700 Subject: [PATCH] fix(YouTube - Litho Filter): Ignore null buffers --- .../components/LayoutComponentsFilter.java | 2 +- .../patches/components/LithoFilterPatch.java | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/components/LayoutComponentsFilter.java b/app/src/main/java/app/revanced/integrations/youtube/patches/components/LayoutComponentsFilter.java index e1938f7059..06dcb85f9c 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/components/LayoutComponentsFilter.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/components/LayoutComponentsFilter.java @@ -177,7 +177,7 @@ boolean isFiltered(String path, @Nullable String identifier, String allValue, by if (matchedGroup == communityPosts) { if (BrowseIdPatch.isHomeFeed()) return SettingsEnum.HIDE_COMMUNITY_POSTS_HOME.getBoolean(); - return HIDE_COMMUNITY_POSTS_SUBSCRIPTIONS.getBoolean(); + return SettingsEnum.HIDE_COMMUNITY_POSTS_SUBSCRIPTIONS.getBoolean(); } return super.isFiltered(path, identifier, allValue, protobufBufferArray, matchedList, matchedGroup, matchedIndex); diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/components/LithoFilterPatch.java b/app/src/main/java/app/revanced/integrations/youtube/patches/components/LithoFilterPatch.java index 4288e11fc0..13f4522c89 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/components/LithoFilterPatch.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/components/LithoFilterPatch.java @@ -354,6 +354,8 @@ public final class LithoFilterPatch { private static final StringTrieSearch identifierSearchTree = new StringTrieSearch(); private static final StringTrieSearch allValueSearchTree = new StringTrieSearch(); + private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; + /** * Because litho filtering is multi-threaded and the buffer is passed in from a different injection point, * the buffer is saved to a ThreadLocal so each calling thread does not interfere with other threads. @@ -396,12 +398,18 @@ private static void filterGroupLists(TrieSearch pathSearchTree, * Injection point. Called off the main thread. */ @SuppressWarnings("unused") - public static void setProtoBuffer(@NonNull ByteBuffer protobufBuffer) { + public static void setProtoBuffer(@Nullable ByteBuffer protobufBuffer) { // Set the buffer to a thread local. The buffer will remain in memory, even after the call to #filter completes. // This is intentional, as it appears the buffer can be set once and then filtered multiple times. // The buffer will be cleared from memory after a new buffer is set by the same thread, // or when the calling thread eventually dies. - bufferThreadLocal.set(protobufBuffer); + if (protobufBuffer == null) { + // It appears the buffer can be cleared out just before the call to #filter() + // Ignore this null value and retain the last buffer that was set. + LogHelper.printDebug(() -> "Ignoring null protobuffer"); + } else { + bufferThreadLocal.set(protobufBuffer); + } } /** @@ -414,14 +422,17 @@ public static boolean filter(@NonNull StringBuilder pathBuilder, @Nullable Strin return false; ByteBuffer protobufBuffer = bufferThreadLocal.get(); + final byte[] bufferArray; + // Potentially the buffer may have been null or never set up until now. + // Use an empty buffer so the litho id/path filters still work correctly. if (protobufBuffer == null) { - LogHelper.printException(() -> "Proto buffer is null"); // Should never happen. - return false; - } - - if (!protobufBuffer.hasArray()) { - LogHelper.printDebug(() -> "Proto buffer does not have an array"); - return false; + LogHelper.printDebug(() -> "Proto buffer is null, using an empty buffer array"); + bufferArray = EMPTY_BYTE_ARRAY; + } else if (!protobufBuffer.hasArray()) { + LogHelper.printDebug(() -> "Proto buffer does not have an array, using an empty buffer array"); + bufferArray = EMPTY_BYTE_ARRAY; + } else { + bufferArray = protobufBuffer.array(); } LithoFilterParameters parameter = new LithoFilterParameters(pathBuilder.toString(), identifier, object.toString(), protobufBuffer.array()); @@ -488,7 +499,7 @@ private static void findAsciiStrings(StringBuilder builder, byte[] buffer) { @Override public String toString() { // Estimate the percentage of the buffer that are Strings. - StringBuilder builder = new StringBuilder(protoBuffer.length / 2); + StringBuilder builder = new StringBuilder(Math.max(100, protoBuffer.length / 2)); builder.append("\nID: "); builder.append(identifier); builder.append("\nPath: "); @@ -502,4 +513,4 @@ public String toString() { return builder.toString(); } } -} \ No newline at end of file +}