Commit 1bd011c27764548c0fba153cc9625998f12951b2

ckolivas 2014-02-26T09:26:12

Read in hfa stream until we get a HF_PREAMBLE

diff --git a/driver-hashfast.c b/driver-hashfast.c
index b7dc348..01472bb 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -221,11 +221,13 @@ static bool hfa_send_packet(struct cgpu_info *hashfast, struct hf_header *h, int
 	return true;
 }
 
+#define HFA_GET_HEADER_BUFSIZE 512
+
 static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc)
 {
 	int amount, ret, orig_len, len, ofs = 0;
 	cgtimer_t ts_start;
-	char buf[512];
+	char buf[HFA_GET_HEADER_BUFSIZE];
 	char *header;
 
 	if (unlikely(hashfast->usbinfo.nodev))
@@ -247,6 +249,11 @@ static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint
 
 		if (unlikely(hashfast->usbinfo.nodev))
 			return false;
+		if(ofs + len > HFA_GET_HEADER_BUFSIZE) {
+			// Not expected to happen.
+			applog(LOG_WARNING, "hfa_get_header() tried to overflow buf[].");
+			return false;
+		}
 		ret = usb_read(hashfast, buf + ofs, len, &amount, C_HF_GETHEADER);
 
 		if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
@@ -261,6 +268,11 @@ static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint
 			}
 			len -= ofs;
 		}
+		else {
+			/* HF_PREAMBLE not found, toss all the useless leading data. */
+			ofs = 0;
+			len = sizeof(*h);
+		}
 	} while (len > 0);
 
 	memcpy(h, header, orig_len);