Skip to content

Commit 1799002

Browse files
committed
dcd_stm32_fsdev : Fix index out of bound in dcd_write_packet_memory()
If src is odd then src[wNBytes] is accessed.
1 parent 1d37f5e commit 1799002

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,7 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr)
10641064
*/
10651065
static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, size_t wNBytes)
10661066
{
1067-
uint32_t n = ((uint32_t)wNBytes + 1U) >> 1U;
1068-
uint32_t i;
1067+
uint32_t n = (uint32_t)wNBytes >> 1U;
10691068
uint16_t temp1, temp2;
10701069
const uint8_t * srcVal;
10711070

@@ -1076,15 +1075,22 @@ static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, si
10761075
srcVal = src;
10771076
pdwVal = &pma[PMA_STRIDE*(dst>>1)];
10781077

1079-
for (i = n; i != 0; i--)
1078+
while (n--)
10801079
{
1081-
temp1 = (uint16_t) *srcVal;
1080+
temp1 = (uint16_t)*srcVal;
10821081
srcVal++;
1083-
temp2 = temp1 | ((uint16_t)((uint16_t) ((*srcVal) << 8U))) ;
1082+
temp2 = temp1 | ((uint16_t)(((uint16_t)(*srcVal)) << 8U)) ;
10841083
*pdwVal = temp2;
10851084
pdwVal += PMA_STRIDE;
10861085
srcVal++;
10871086
}
1087+
1088+
if (wNBytes & 0x01)
1089+
{
1090+
temp1 = *srcVal;
1091+
*pdwVal = temp2;
1092+
}
1093+
10881094
return true;
10891095
}
10901096

@@ -1141,7 +1147,6 @@ static bool dcd_write_packet_memory_ff(tu_fifo_t * ff, uint16_t dst, uint16_t wN
11411147
static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wNBytes)
11421148
{
11431149
uint32_t n = (uint32_t)wNBytes >> 1U;
1144-
uint32_t i;
11451150
// The GCC optimizer will combine access to 32-bit sizes if we let it. Force
11461151
// it volatile so that it won't do that.
11471152
__IO const uint16_t *pdwVal;
@@ -1150,7 +1155,7 @@ static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wN
11501155
pdwVal = &pma[PMA_STRIDE*(src>>1)];
11511156
uint8_t *dstVal = (uint8_t*)dst;
11521157

1153-
for (i = n; i != 0U; i--)
1158+
while (n--)
11541159
{
11551160
temp = *pdwVal;
11561161
pdwVal += PMA_STRIDE;

0 commit comments

Comments
 (0)