Enforce non-nullness in PrinterId and exploit it.
Change-Id: Ifdf3b2329548a665d609dd66b738baab5c765b54
diff --git a/core/java/android/print/PrinterId.java b/core/java/android/print/PrinterId.java
index 83efe80..ff9c0df 100644
--- a/core/java/android/print/PrinterId.java
+++ b/core/java/android/print/PrinterId.java
@@ -20,16 +20,17 @@
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 @@
*
* @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 @@
*
* @hide
*/
- public ComponentName getServiceName() {
+ public @NonNull ComponentName getServiceName() {
return mServiceName;
}
@@ -93,14 +94,10 @@
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 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;
}
diff --git a/core/java/android/printservice/PrintService.java b/core/java/android/printservice/PrintService.java
index d0037b7..3104492 100644
--- a/core/java/android/printservice/PrintService.java
+++ b/core/java/android/printservice/PrintService.java
@@ -30,6 +30,8 @@
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 final PrinterId generatePrinterId(String localId) {
throwIfNotCalledOnMainThread();
+ localId = Preconditions.checkNotNull(localId, "localId cannot be null");
return new PrinterId(new ComponentName(getPackageName(),
getClass().getName()), localId);
}
diff --git a/services/print/java/com/android/server/print/RemotePrintService.java b/services/print/java/com/android/server/print/RemotePrintService.java
index 0af1525..5ee4066 100644
--- a/services/print/java/com/android/server/print/RemotePrintService.java
+++ b/services/print/java/com/android/server/print/RemotePrintService.java
@@ -863,8 +863,7 @@
}
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);
}
}