Enforce non-nullness in PrinterId and exploit it.
Change-Id: Ifdf3b2329548a665d609dd66b738baab5c765b54
This commit is contained in:
@@ -20,16 +20,17 @@ import android.annotation.NonNull;
|
||||
import android.content.ComponentName;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.internal.util.Preconditions;
|
||||
|
||||
/**
|
||||
* This class represents the unique id of a printer.
|
||||
*/
|
||||
public final class PrinterId implements Parcelable {
|
||||
|
||||
private final ComponentName mServiceName;
|
||||
private final @NonNull ComponentName mServiceName;
|
||||
|
||||
private final String mLocalId;
|
||||
private final @NonNull String mLocalId;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
@@ -39,14 +40,14 @@ public final class PrinterId implements Parcelable {
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public PrinterId(ComponentName serviceName, String localId) {
|
||||
public PrinterId(@NonNull ComponentName serviceName, @NonNull String localId) {
|
||||
mServiceName = serviceName;
|
||||
mLocalId = localId;
|
||||
}
|
||||
|
||||
private PrinterId(Parcel parcel) {
|
||||
mServiceName = parcel.readParcelable(null);
|
||||
mLocalId = parcel.readString();
|
||||
private PrinterId(@NonNull Parcel parcel) {
|
||||
mServiceName = Preconditions.checkNotNull((ComponentName) parcel.readParcelable(null));
|
||||
mLocalId = Preconditions.checkNotNull(parcel.readString());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +57,7 @@ public final class PrinterId implements Parcelable {
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public ComponentName getServiceName() {
|
||||
public @NonNull ComponentName getServiceName() {
|
||||
return mServiceName;
|
||||
}
|
||||
|
||||
@@ -93,14 +94,10 @@ public final class PrinterId implements Parcelable {
|
||||
return false;
|
||||
}
|
||||
PrinterId other = (PrinterId) object;
|
||||
if (mServiceName == null) {
|
||||
if (other.mServiceName != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!mServiceName.equals(other.mServiceName)) {
|
||||
if (!mServiceName.equals(other.mServiceName)) {
|
||||
return false;
|
||||
}
|
||||
if (!TextUtils.equals(mLocalId, other.mLocalId)) {
|
||||
if (!mLocalId.equals(other.mLocalId)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -110,8 +107,7 @@ public final class PrinterId implements Parcelable {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int hashCode = 1;
|
||||
hashCode = prime * hashCode + ((mServiceName != null)
|
||||
? mServiceName.hashCode() : 1);
|
||||
hashCode = prime * hashCode + mServiceName.hashCode();
|
||||
hashCode = prime * hashCode + mLocalId.hashCode();
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ import android.print.PrintJobInfo;
|
||||
import android.print.PrinterId;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.util.Preconditions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -346,6 +348,7 @@ public abstract class PrintService extends Service {
|
||||
*/
|
||||
public final PrinterId generatePrinterId(String localId) {
|
||||
throwIfNotCalledOnMainThread();
|
||||
localId = Preconditions.checkNotNull(localId, "localId cannot be null");
|
||||
return new PrinterId(new ComponentName(getPackageName(),
|
||||
getClass().getName()), localId);
|
||||
}
|
||||
|
||||
@@ -863,8 +863,7 @@ final class RemotePrintService implements DeathRecipient {
|
||||
}
|
||||
|
||||
private void throwIfPrinterIdTampered(ComponentName serviceName, PrinterId printerId) {
|
||||
if (printerId == null || printerId.getServiceName() == null
|
||||
|| !printerId.getServiceName().equals(serviceName)) {
|
||||
if (printerId == null || !printerId.getServiceName().equals(serviceName)) {
|
||||
throw new IllegalArgumentException("Invalid printer id: " + printerId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user