ch347_spi: Fix segfault with older libusb
Passing NULL for libusb_bulk_transfer()'s transferred argument is only
supported since libusb 1.0.21. Older versions unconditionally dereference
the pointer after a transfer, causing CH347 users to segfault.
Always provide storage for the transferred byte count, even where the
value itself is not consumed.
Signed-off-by: Arthur Heymans <arthur@aheymans.xyz>
Change-Id: I30986a847f953c0390421389a81fd4bb6a6a6964
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/563
Reviewed-by: Nico Huber <nico.h@gmx.de>
Tested-by: Nico Huber <nico.h@gmx.de>
diff --git a/ch347_spi.c b/ch347_spi.c
index cd334da..6cc72ae 100644
--- a/ch347_spi.c
+++ b/ch347_spi.c
@@ -78,7 +78,8 @@
[8] = cs2
};
- int32_t ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, cmd, sizeof(cmd), NULL, 1000);
+ int transferred;
+ int32_t ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, cmd, sizeof(cmd), &transferred, 1000);
if (ret < 0) {
msg_perr("Could not change CS!\n");
return -1;
@@ -112,7 +113,7 @@
return -1;
}
- ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, resp_buf, sizeof(resp_buf), NULL, 1000);
+ ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, resp_buf, sizeof(resp_buf), &transferred, 1000);
if (ret < 0) {
msg_perr("Could not receive write command response\n");
return -1;
@@ -205,6 +206,7 @@
static int32_t ch347_spi_config(struct ch347_spi_data *ch347_data, uint8_t divisor, uint8_t mode)
{
int32_t ret;
+ int transferred;
uint8_t buff[29] = {
[0] = CH347_CMD_SPI_SET_CFG,
[1] = (sizeof(buff) - 3) & 0xFF,
@@ -230,7 +232,7 @@
[24] = 0
};
- ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buff, sizeof(buff), NULL, 1000);
+ ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buff, sizeof(buff), &transferred, 1000);
if (ret < 0) {
msg_perr("Could not configure SPI interface\n");
}
@@ -238,7 +240,7 @@
/* FIXME: Not sure if the CH347 sends error responses for
* invalid config data, if so the code should check
*/
- ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buff, sizeof(buff), NULL, 1000);
+ ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buff, sizeof(buff), &transferred, 1000);
if (ret < 0) {
msg_perr("Could not receive configure SPI command response\n");
}