Skip to content

Commit 079ea83

Browse files
committed
drivers/w5100: Fix recv() with too small buffer
If netdev_driver_t::recv() is called and the provided buffer is smaller than the frame `-ENOBUFS` should be returned, the frame should not be droppend and no data of the frame should be returned. Addresses: #10413
1 parent ae093fd commit 079ea83

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

drivers/w5100/w5100.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* @}
1919
*/
2020

21+
#include <errno.h>
2122
#include <stdio.h>
2223
#include <string.h>
2324

@@ -230,12 +231,12 @@ static int send(netdev_t *netdev, const iolist_t *iolist)
230231
return sum;
231232
}
232233

233-
static int recv(netdev_t *netdev, void *buf, size_t len, void *info)
234+
static int recv(netdev_t *netdev, void *buf, size_t max_len, void *info)
234235
{
235236
(void)info;
236237
w5100_t *dev = (w5100_t *)netdev;
237238
uint8_t *in_buf = (uint8_t *)buf;
238-
unsigned n = 0;
239+
unsigned len = 0;
239240

240241
/* get access to the SPI bus for the duration of this function */
241242
spi_acquire(dev->p.spi, dev->p.cs, SPI_CONF, dev->p.clk);
@@ -247,20 +248,25 @@ static int recv(netdev_t *netdev, void *buf, size_t len, void *info)
247248
uint16_t rp = raddr(dev, S0_RX_RD0, S0_RX_RD1);
248249
uint16_t psize = raddr(dev, (S0_RX_BASE + (rp & S0_MASK)),
249250
(S0_RX_BASE + ((rp + 1) & S0_MASK)));
250-
n = psize - 2;
251+
len = psize - 2;
251252

252-
DEBUG("[w5100] recv: got packet of %i byte (at 0x%04x)\n", n, (int)rp);
253+
DEBUG("[w5100] recv: got packet of %i byte (at 0x%04x)\n", len, (int)rp);
253254

254255
/* read the actual data into the given buffer if wanted */
255256
if (in_buf != NULL) {
257+
/* Is provided buffer big enough? */
258+
if (len > max_len) {
259+
spi_release(dev->p.spi);
260+
return -ENOBUFS;
261+
}
256262
uint16_t pos = rp + 2;
257-
len = (n <= len) ? n : len;
263+
258264
for (unsigned i = 0; i < len; i++) {
259265
in_buf[i] = rreg(dev, (S0_RX_BASE + ((pos++) & S0_MASK)));
260266
}
261267

262268
DEBUG("[w5100] recv: read %i byte from device (at 0x%04x)\n",
263-
n, (int)rp);
269+
len, (int)rp);
264270
}
265271

266272
/* if frame received OR drop requested, remove frame from RX buffer */
@@ -279,7 +285,7 @@ static int recv(netdev_t *netdev, void *buf, size_t len, void *info)
279285
/* release the SPI bus again */
280286
spi_release(dev->p.spi);
281287

282-
return (int)n;
288+
return (int)len;
283289
}
284290

285291
static void isr(netdev_t *netdev)

0 commit comments

Comments
 (0)