fw: Add destination buffer size to read()

Signed-off-by: Daniel Lublin <daniel@lublin.se>
This commit is contained in:
Michael Cardell Widerkrantz 2023-02-27 16:41:31 +01:00 committed by Daniel Lublin
parent b292c72534
commit f386cec1ed
No known key found for this signature in database
GPG Key ID: 75BD0FEB8D3E7830
3 changed files with 13 additions and 3 deletions

View File

@ -244,7 +244,11 @@ int main()
memset(cmd, 0, CMDLEN_MAXBYTES);
// Now we know the size of the cmd frame, read it all
read(cmd, hdr.len);
if (read(cmd, CMDLEN_MAXBYTES, hdr.len) != 0) {
htif_puts("read: buffer overrun\n");
forever_redflash();
// Not reached
}
// Is it for us?
if (hdr.endpoint != DST_FW) {

View File

@ -148,9 +148,15 @@ uint8_t readbyte_ledflash(int ledvalue, int loopcount)
}
}
void read(uint8_t *buf, size_t nbytes)
int read(uint8_t *buf, size_t bufsize, size_t nbytes)
{
if (nbytes > bufsize) {
return -1;
}
for (int n = 0; n < nbytes; n++) {
buf[n] = readbyte();
}
return 0;
}

View File

@ -56,6 +56,6 @@ void writebyte(uint8_t b);
void write(uint8_t *buf, size_t nbytes);
uint8_t readbyte();
uint8_t readbyte_ledflash(int ledvalue, int loopcount);
void read(uint8_t *buf, size_t nbytes);
int read(uint8_t *buf, size_t bufsize, size_t nbytes);
#endif