From f5d175116f9f54b5395ed543b995103fa5bed7f7 Mon Sep 17 00:00:00 2001 From: Sarah Chin Date: Wed, 9 Nov 2022 00:37:21 -0800 Subject: [PATCH] Fix NPEs in DataCallResponse Fix NPEs in the equals method for DataCallResponse. Only check the size for QOS bearer sessions and traffic descriptors if they are both not null. Also fix the hash code to return the same hash regardless of list orders. Test: atest DataCallResponseTest Bug: 257975797 Change-Id: I6403f71126e3e5ab300c565cf0c528268cbfe69d --- .../telephony/data/DataCallResponse.java | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/telephony/java/android/telephony/data/DataCallResponse.java b/telephony/java/android/telephony/data/DataCallResponse.java index 73aff4351785d..a834e2bbd0d10 100644 --- a/telephony/java/android/telephony/data/DataCallResponse.java +++ b/telephony/java/android/telephony/data/DataCallResponse.java @@ -468,14 +468,14 @@ public final class DataCallResponse implements Parcelable { final boolean isQosBearerSessionsSame = (mQosBearerSessions == null || other.mQosBearerSessions == null) ? mQosBearerSessions == other.mQosBearerSessions - : mQosBearerSessions.size() == other.mQosBearerSessions.size() - && mQosBearerSessions.containsAll(other.mQosBearerSessions); + : (mQosBearerSessions.size() == other.mQosBearerSessions.size() + && mQosBearerSessions.containsAll(other.mQosBearerSessions)); final boolean isTrafficDescriptorsSame = (mTrafficDescriptors == null || other.mTrafficDescriptors == null) ? mTrafficDescriptors == other.mTrafficDescriptors - : mTrafficDescriptors.size() == other.mTrafficDescriptors.size() - && mTrafficDescriptors.containsAll(other.mTrafficDescriptors); + : (mTrafficDescriptors.size() == other.mTrafficDescriptors.size() + && mTrafficDescriptors.containsAll(other.mTrafficDescriptors)); return mCause == other.mCause && mSuggestedRetryTime == other.mSuggestedRetryTime @@ -504,10 +504,35 @@ public final class DataCallResponse implements Parcelable { @Override public int hashCode() { + // Generate order-independent hashes for lists + int addressesHash = mAddresses.stream() + .map(LinkAddress::hashCode) + .mapToInt(Integer::intValue) + .sum(); + int dnsAddressesHash = mDnsAddresses.stream() + .map(InetAddress::hashCode) + .mapToInt(Integer::intValue) + .sum(); + int gatewayAddressesHash = mGatewayAddresses.stream() + .map(InetAddress::hashCode) + .mapToInt(Integer::intValue) + .sum(); + int pcscfAddressesHash = mPcscfAddresses.stream() + .map(InetAddress::hashCode) + .mapToInt(Integer::intValue) + .sum(); + int qosBearerSessionsHash = mQosBearerSessions.stream() + .map(QosBearerSession::hashCode) + .mapToInt(Integer::intValue) + .sum(); + int trafficDescriptorsHash = mTrafficDescriptors.stream() + .map(TrafficDescriptor::hashCode) + .mapToInt(Integer::intValue) + .sum(); return Objects.hash(mCause, mSuggestedRetryTime, mId, mLinkStatus, mProtocolType, - mInterfaceName, mAddresses, mDnsAddresses, mGatewayAddresses, mPcscfAddresses, - mMtu, mMtuV4, mMtuV6, mHandoverFailureMode, mPduSessionId, mDefaultQos, - mQosBearerSessions, mSliceInfo, mTrafficDescriptors); + mInterfaceName, addressesHash, dnsAddressesHash, gatewayAddressesHash, + pcscfAddressesHash, mMtu, mMtuV4, mMtuV6, mHandoverFailureMode, mPduSessionId, + mDefaultQos, qosBearerSessionsHash, mSliceInfo, trafficDescriptorsHash); } @Override @@ -816,8 +841,8 @@ public final class DataCallResponse implements Parcelable { /** * Set pdu session id. *

- * The id must be between 1 and 15 when linked to a pdu session. If no pdu session - * exists for the current data call, the id must be set to {@link PDU_SESSION_ID_NOT_SET}. + * The id must be between 1 and 15 when linked to a pdu session. If no pdu session + * exists for the current data call, the id must be set to {@link #PDU_SESSION_ID_NOT_SET}. * * @param pduSessionId Pdu Session Id of the data call. * @return The same instance of the builder. @@ -858,6 +883,7 @@ public final class DataCallResponse implements Parcelable { */ public @NonNull Builder setQosBearerSessions( @NonNull List qosBearerSessions) { + Objects.requireNonNull(qosBearerSessions); mQosBearerSessions = qosBearerSessions; return this; } @@ -891,6 +917,7 @@ public final class DataCallResponse implements Parcelable { */ public @NonNull Builder setTrafficDescriptors( @NonNull List trafficDescriptors) { + Objects.requireNonNull(trafficDescriptors); mTrafficDescriptors = trafficDescriptors; return this; }