Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb15e7f1 authored by Ian Baker's avatar Ian Baker Committed by Android Build Coastguard Worker
Browse files

MediaSession: Enforce max name length on broadcast receivers too

The previous change in ag/35590474 only checked the length of component names set via MediaSession.setMediaButtonReceiver(PendingIntent) [1] and
not via
MediaSession.setMediaButtonBroadcastReceiver(android.content.ComponentName) [2] as highlighted in b/433250316#comment72.

[1] https://developer.android.com/reference/android/media/session/MediaSession#setMediaButtonBroadcastReceiver(android.content.ComponentName)

[2] https://developer.android.com/reference/android/media/session/MediaSession#setMediaButtonBroadcastReceiver(android.content.ComponentName)

Bug: 433250316
Test: Presubmit
Flag: EXEMPT BUGFIX
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:d09400d72573c2e6124ae28dd9bb68139c9598a5
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:daa9c35a13a854fc2a3a997f2cc4b486c5773373
Merged-In: I7ef6bb0015f2501559793129ca62881911e7b9eb
Change-Id: I7ef6bb0015f2501559793129ca62881911e7b9eb
parent 014dea27
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -135,6 +135,9 @@ final class MediaButtonReceiverHolder {
    }

    public static MediaButtonReceiverHolder create(int userId, ComponentName broadcastReceiver) {
        if (componentNameTooLong(broadcastReceiver)) {
            throw new IllegalArgumentException("receiver name too long");
        }
        return new MediaButtonReceiverHolder(userId, null, broadcastReceiver,
                COMPONENT_TYPE_BROADCAST);
    }
@@ -370,20 +373,27 @@ final class MediaButtonReceiverHolder {
            if (componentInfo != null && TextUtils.equals(componentInfo.packageName,
                    pendingIntent.getCreatorPackage())
                    && componentInfo.packageName != null && componentInfo.name != null) {
                int componentNameLength =
                        componentInfo.packageName.length() + componentInfo.name.length() + 1;
                if (componentNameLength > MAX_COMPONENT_NAME_LENGTH) {
                ComponentName componentName =
                    new ComponentName(componentInfo.packageName, componentInfo.name);
                if (componentNameTooLong(componentName)) {
                    Log.w(TAG, "detected and ignored component name with overly long package"
                            + " or name, pi=" + pendingIntent);
                    continue;
                }
                return new ComponentName(componentInfo.packageName, componentInfo.name);
                return componentName;
            }
        }

        return null;
    }

    private static boolean componentNameTooLong(ComponentName componentName) {
        return componentName.getPackageName().length()
                + componentName.getClassName().length()
                + 1
            > MAX_COMPONENT_NAME_LENGTH;
    }

    /**
     * Retrieves the {@link ComponentInfo} from a {@link ResolveInfo} instance. Similar to {@link
     * ResolveInfo#getComponentInfo()}, but returns {@code null} if this {@link ResolveInfo} points