Kuvée / COBS

Dependents:   Nucleo_cobs_test

Revision:
2:64fdac5efaa1
Parent:
1:2b84a7482df6
Child:
3:41c9f2bbeeb2
diff -r 2b84a7482df6 -r 64fdac5efaa1 COBS.cpp
--- a/COBS.cpp	Tue Feb 10 03:12:12 2015 +0000
+++ b/COBS.cpp	Tue Jul 21 19:31:41 2015 -0400
@@ -51,13 +51,16 @@
 * writing the output to the location pointed
 * to by “dst”.
 */
-void COBS::UnStuffData(unsigned char *ptr, unsigned long length, unsigned char *dst)
+int COBS::UnStuffData(unsigned char *ptr, unsigned long length, unsigned char *dst)
 {
     const unsigned char *end = ptr + length;
     while (ptr < end)
     {
         int i, code = *ptr++;
-        for (i=1; i<code; i++) *dst++ = *ptr++;
+				if (ptr+code > end) return 1;  //if we will overun the end of the buffer exit
+																		 //this is most likely to happen when decoding a malformed message
+        for (i=1; i<code; i++) 	*dst++ = *ptr++;
         if (code < 0xFF) *dst++ = 0;
     }
-}
\ No newline at end of file
+		return 0;
+}