From ce07550c57172443c10a66957b50085e273d20b3 Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sun, 14 Feb 2016 17:29:49 +0100
Subject: o Fixing off-by-one error when reading device name.

---
 apps/SoilMoisture.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/apps/SoilMoisture.cpp b/apps/SoilMoisture.cpp
index 88f881a..13578f6 100644
--- a/apps/SoilMoisture.cpp
+++ b/apps/SoilMoisture.cpp
@@ -130,7 +130,11 @@ string SoilMoisture::getName(uint8_t sensor) {
     uint8_t bytes[bytesLeft];
     buffer.copy(bytes, bytesLeft);
 
-    return string((char *) bytes, (unsigned long) bytesLeft);
+    if (bytesLeft == 0 || bytesLeft < (bytes[0] + 1)) {
+        throw runtime_error("Bad response from device. buffer size: " + to_string(bytesLeft) + ", buffer[0]=" + to_string((int)bytes[0]));
+    }
+
+    return string((const char *)&bytes[1], bytes[0]);
 }
 
 bool SoilMoisture::hasTemperatureSensor() {
-- 
cgit v1.2.3