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

[somfytahoma] Avoid potential NPE #10453

Merged
merged 1 commit into from
Apr 4, 2021
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 @@ -229,7 +229,10 @@ public synchronized void login() {

SomfyTahomaLoginResponse data = gson.fromJson(response.getContentAsString(),
SomfyTahomaLoginResponse.class);
if (data.isSuccess()) {
if (data == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Received invalid data (login)");
} else if (data.isSuccess()) {
logger.debug("SomfyTahoma version: {}", data.getVersion());
String id = registerEvents();
if (id != null && !id.equals(UNAUTHORIZED)) {
Expand All @@ -247,11 +250,11 @@ public synchronized void login() {
}
}
} catch (JsonSyntaxException e) {
logger.debug("Received invalid data", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Received invalid data");
logger.debug("Received invalid data (login)", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Received invalid data (login)");
} catch (InterruptedException | ExecutionException | TimeoutException e) {
if (e instanceof ExecutionException) {
if (e.getMessage().contains(AUTHENTICATION_CHALLENGE)) {
if (isAuthenticationChallenge(e)) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Authentication challenge");
setTooManyRequests();
Expand Down Expand Up @@ -377,9 +380,11 @@ public List<SomfyTahomaDevice> getDevices() {

public synchronized @Nullable SomfyTahomaDevice getCachedDevice(String url) {
List<SomfyTahomaDevice> devices = cachedDevices.getValue();
for (SomfyTahomaDevice device : devices) {
if (url.equals(device.getDeviceURL())) {
return device;
if (devices != null) {
for (SomfyTahomaDevice device : devices) {
if (url.equals(device.getDeviceURL())) {
return device;
}
}
}
return null;
Expand Down Expand Up @@ -451,15 +456,32 @@ private synchronized void updateThings() {
}

private void processExecutionRegisteredEvent(SomfyTahomaEvent event) {
JsonElement el = event.getAction();
if (el.isJsonArray()) {
SomfyTahomaAction[] actions = gson.fromJson(el, SomfyTahomaAction[].class);
for (SomfyTahomaAction action : actions) {
registerExecution(action.getDeviceURL(), event.getExecId());
boolean invalidData = false;
try {
JsonElement el = event.getAction();
if (el.isJsonArray()) {
SomfyTahomaAction[] actions = gson.fromJson(el, SomfyTahomaAction[].class);
if (actions == null) {
invalidData = true;
} else {
for (SomfyTahomaAction action : actions) {
registerExecution(action.getDeviceURL(), event.getExecId());
}
}
} else {
SomfyTahomaAction action = gson.fromJson(el, SomfyTahomaAction.class);
if (action == null) {
invalidData = true;
} else {
registerExecution(action.getDeviceURL(), event.getExecId());
}
}
} else {
SomfyTahomaAction action = gson.fromJson(el, SomfyTahomaAction.class);
registerExecution(action.getDeviceURL(), event.getExecId());
} catch (JsonSyntaxException e) {
invalidData = true;
}
if (invalidData) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Received invalid data (execution registered)");
}
}

Expand Down Expand Up @@ -771,7 +793,8 @@ public SomfyTahomaStatus getTahomaStatus(String gatewayId) {
}

private boolean isAuthenticationChallenge(Exception ex) {
return ex.getMessage().contains(AUTHENTICATION_CHALLENGE);
String msg = ex.getMessage();
return msg != null && msg.contains(AUTHENTICATION_CHALLENGE);
}

@Override
Expand Down