Skip to content

Commit d24c95d

Browse files
committed
Check destination buffer size on read()
1 parent 9300507 commit d24c95d

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/tkey/proto.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,5 @@ int parseframe(uint8_t b, struct frame_header *hdr);
3939
void writebyte(uint8_t b);
4040
void write(const uint8_t *buf, size_t nbytes);
4141
uint8_t readbyte();
42-
void read(uint8_t *buf, size_t nbytes);
43-
42+
int read(uint8_t *buf, size_t bufsize, size_t nbytes);
4443
#endif

libcommon/proto.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ uint8_t readbyte()
8282
}
8383
}
8484

85-
void read(uint8_t *buf, size_t nbytes)
85+
int read(uint8_t *buf, size_t bufsize, size_t nbytes)
8686
{
87+
if (nbytes > bufsize) {
88+
return -1;
89+
}
90+
8791
for (int n = 0; n < nbytes; n++) {
8892
buf[n] = readbyte();
8993
}
94+
95+
return 0;
9096
}

0 commit comments

Comments
 (0)