Fix exception when unparcelling NetworkRequest with reservationId

This change adds a new private constructor that is called directly from
createFromParcel instead of using the public constructor used by CS.

The public constructor throws an exception when the passed
NetworkCapabilities specify a reservationId (to prevent apps from
passing in reservationIds). This means that it cannot be called from
createFromParcel.

Test: atest NetworkReservationTest
Change-Id: I1fe2411401623b0b01cbdbe040049b810fcbb48a
This commit is contained in:
Patrick Rohr 2025-01-07 12:54:33 -08:00
commit 121ab15fdf

View file

@ -282,6 +282,13 @@ public class NetworkRequest implements Parcelable {
this.type = that.type;
}
private NetworkRequest(Parcel in) {
networkCapabilities = NetworkCapabilities.CREATOR.createFromParcel(in);
legacyType = in.readInt();
requestId = in.readInt();
type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
}
/**
* Builder used to create {@link NetworkRequest} objects. Specify the Network features
* needed in terms of {@link NetworkCapabilities} features
@ -678,12 +685,7 @@ public class NetworkRequest implements Parcelable {
public static final @android.annotation.NonNull Creator<NetworkRequest> CREATOR =
new Creator<NetworkRequest>() {
public NetworkRequest createFromParcel(Parcel in) {
NetworkCapabilities nc = NetworkCapabilities.CREATOR.createFromParcel(in);
int legacyType = in.readInt();
int requestId = in.readInt();
Type type = Type.valueOf(in.readString()); // IllegalArgumentException if invalid.
NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
return result;
return new NetworkRequest(in);
}
public NetworkRequest[] newArray(int size) {
return new NetworkRequest[size];