From c0a84367dec88719bbe7d969cc75acd41b8fc64e Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Mon, 10 Jul 2017 19:20:47 -0700 Subject: [PATCH] null terminate usb string before parse --- Inc/main.h | 3 +++ Src/usbd_cdc_if.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Inc/main.h b/Inc/main.h index 811dbfb4..c8d127cb 100644 --- a/Inc/main.h +++ b/Inc/main.h @@ -146,6 +146,9 @@ #define CURRENT_MEAS_PERIOD ((float)(2*TIM_1_8_PERIOD_CLOCKS)/(float)TIM_1_8_CLOCK_HZ) #define CURRENT_MEAS_HZ (TIM_1_8_CLOCK_HZ/(2*TIM_1_8_PERIOD_CLOCKS)) +#define MACRO_MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MACRO_MIN(x, y) (((x) < (y)) ? (x) : (y)) + /* USER CODE END Private defines */ /** diff --git a/Src/usbd_cdc_if.c b/Src/usbd_cdc_if.c index 6b0d57c4..4f06cc58 100644 --- a/Src/usbd_cdc_if.c +++ b/Src/usbd_cdc_if.c @@ -265,6 +265,10 @@ static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len) USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); + //Append null termination at end of string + int null_idx = MACRO_MIN(*Len, APP_RX_DATA_SIZE-1); + Buf[null_idx] = 0; + motor_parse_cmd(Buf, *Len); return (USBD_OK);