Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android tv-casting-app: Adding defensive null checks in DiscoveredNodeData constructor #108

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.chip.casting;

import android.net.nsd.NsdServiceInfo;

import android.util.Log;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -27,6 +27,8 @@
import java.util.Objects;

public class DiscoveredNodeData {
private static final String TAG = DiscoveredNodeData.class.getSimpleName();

private static final int MAX_IP_ADDRESSES = 5;
private static final int MAX_ROTATING_ID_LEN = 50;
private static final String KEY_DEVICE_NAME = "DN";
Expand All @@ -53,26 +55,44 @@ public class DiscoveredNodeData {

public DiscoveredNodeData(NsdServiceInfo serviceInfo) {
Map<String, byte[]> attributes = serviceInfo.getAttributes();
this.deviceName = new String(attributes.get(KEY_DEVICE_NAME), StandardCharsets.UTF_8);
if (serviceInfo.getHost() != null) {
this.hostName = serviceInfo.getHost().getHostName();
}
this.deviceType =
Long.parseLong(new String(attributes.get(KEY_DEVICE_TYPE), StandardCharsets.UTF_8));

String vp = new String(attributes.get(KEY_VENDOR_PRODUCT), StandardCharsets.UTF_8);
if (vp != null) {
String[] vpArray = vp.split("\\+");
if (vpArray.length > 0) {
this.vendorId = Long.parseLong(vpArray[0]);
if (vpArray.length == 2) {
this.productId = Long.parseLong(vpArray[1]);

if (attributes != null) {
if (attributes.get(KEY_DEVICE_NAME) != null) {
this.deviceName = new String(attributes.get(KEY_DEVICE_NAME), StandardCharsets.UTF_8);
}

if (attributes.get(KEY_DEVICE_TYPE) != null) {
this.deviceType =
Long.parseLong(new String(attributes.get(KEY_DEVICE_TYPE), StandardCharsets.UTF_8));
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved
}

if (attributes.get(KEY_VENDOR_PRODUCT) != null) {
String vp = new String(attributes.get(KEY_VENDOR_PRODUCT), StandardCharsets.UTF_8);
if (vp != null) {
String[] vpArray = vp.split("\\+");
try {
if (vpArray.length > 0) {
this.vendorId = Long.parseLong(vpArray[0]);
if (vpArray.length == 2) {
this.productId = Long.parseLong(vpArray[1]);
}
}
} catch (NumberFormatException e) {
Log.e(TAG, "Could not parse TXT record for VP: " + e.getMessage());
}
} else {
Log.e(TAG, "TXT Record for VP was null");
}
}
}
sharadb-amazon marked this conversation as resolved.
Show resolved Hide resolved

if (serviceInfo.getHost() != null) {
this.hostName = serviceInfo.getHost().getHostName();
this.ipAddresses = Arrays.asList(serviceInfo.getHost());
} else {
Log.e(TAG, "Host name was null");
}
this.port = serviceInfo.getPort();
this.ipAddresses = Arrays.asList(serviceInfo.getHost());
this.numIPs = 1;
}

Expand Down