From 6dd8cd52c5911fb0aef3e19a7a62e845e06eed22 Mon Sep 17 00:00:00 2001 From: MikeTheTux <44850211+MikeTheTux@users.noreply.github.com> Date: Sun, 18 Dec 2022 13:35:08 +0100 Subject: [PATCH] fixed potential StringIndexOutOfBoundsExceptions (#14000) fixed compiler warnings Signed-off-by: Michael Weger --- .../binding/airq/internal/AirqHandler.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.airq/src/main/java/org/openhab/binding/airq/internal/AirqHandler.java b/bundles/org.openhab.binding.airq/src/main/java/org/openhab/binding/airq/internal/AirqHandler.java index 6eca67a8524a7..2ceccd6c99573 100644 --- a/bundles/org.openhab.binding.airq/src/main/java/org/openhab/binding/airq/internal/AirqHandler.java +++ b/bundles/org.openhab.binding.airq/src/main/java/org/openhab/binding/airq/internal/AirqHandler.java @@ -268,7 +268,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { break; case "logLevel": String ll = command.toString(); - if (ll.equals("Error") || ll.equals("Warning") || ll.equals("Info")) { + if ("Error".equals(ll) || "Warning".equals(ll) || "Info".equals(ll)) { newobj.addProperty("logging", ll); changeSettings(newobj); } else { @@ -288,7 +288,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { break; case "powerFreqSuppression": String newFreq = command.toString(); - if (newFreq.equals("50Hz") || newFreq.equals("60Hz") || newFreq.equals("50Hz+60Hz")) { + if ("50Hz".equals(newFreq) || "60Hz".equals(newFreq) || "50Hz+60Hz".equals(newFreq)) { newobj.addProperty("Rejection", newFreq); changeSettings(newobj); } else { @@ -710,7 +710,12 @@ private void processType(JsonObject dec, String airqName, String channelName, St for (JsonElement el : arr) { str.append(el.getAsString() + ", "); } - updateState(channelName, new StringType(str.substring(0, str.length() - 2))); + if (str.length() >= 2) { + updateState(channelName, new StringType(str.substring(0, str.length() - 2))); + } else { + logger.trace("air-Q - airqHandler - processType(): cannot handle this as an array: {}", + jsonarr); + } } else { logger.warn("air-Q - airqHandler - processType(): cannot handle this as an array: {}", jsonarr); } @@ -730,7 +735,13 @@ private void processType(JsonObject dec, String airqName, String channelName, St str = str + attributeName + ": offset=" + attributeValue.get("offset").getAsString() + " [" + timecalibString + "]"; } - updateState(channelName, new StringType(str.substring(0, str.length() - 1))); + if (!str.isEmpty()) { + updateState(channelName, new StringType(str.substring(0, str.length() - 1))); + } else { + logger.trace( + "air-Q - airqHandler - processType(): Cannot extract calibration data from this string: {}", + dec.get(airqName).toString()); + } } else { logger.warn( "air-Q - airqHandler - processType(): Cannot extract calibration data from this string: {}", @@ -749,9 +760,14 @@ private void processType(JsonObject dec, String airqName, String channelName, St for (JsonElement el : arr) { arrstr = arrstr + el.getAsString() + ", "; } - logger.trace("air-Q - airqHandler - processType(): property array {} set to {}", channelName, - arrstr.substring(0, arrstr.length() - 2)); - getThing().setProperty(channelName, arrstr.substring(0, arrstr.length() - 2)); + if (arrstr.length() >= 2) { + logger.trace("air-Q - airqHandler - processType(): property array {} set to {}", + channelName, arrstr.substring(0, arrstr.length() - 2)); + getThing().setProperty(channelName, arrstr.substring(0, arrstr.length() - 2)); + } else { + logger.trace("air-Q - airqHandler - processType(): cannot handle this as an array: {}", + proparr); + } } else { logger.warn("air-Q - airqHandler - processType(): cannot handle this as an array: {}", proparr); }