Read in hfa stream until we get a HF_PREAMBLE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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);