Skip to content

Commit

Permalink
Check destination buffer size on read()
Browse files Browse the repository at this point in the history
  • Loading branch information
mchack-work committed Jan 22, 2024
1 parent 9300507 commit d24c95d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 1 addition & 2 deletions include/tkey/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ int parseframe(uint8_t b, struct frame_header *hdr);
void writebyte(uint8_t b);
void write(const uint8_t *buf, size_t nbytes);
uint8_t readbyte();
void read(uint8_t *buf, size_t nbytes);

int read(uint8_t *buf, size_t bufsize, size_t nbytes);
#endif
8 changes: 7 additions & 1 deletion libcommon/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ uint8_t readbyte()
}
}

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;
}

0 comments on commit d24c95d

Please sign in to comment.