From be84cecfeb560a013857fa9a5326eca7b785cb5d Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 6 Jun 2016 20:28:19 -0500 Subject: [PATCH] I hate overflows as much as the next person --- avr/Makefile | 2 +- avr/printer.c | 18 ++++++++++++++++++ avr/recv.c | 7 ++++--- avr/send.c | 7 +++++++ include/tabby/avr/printer.h | 2 ++ 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/avr/Makefile b/avr/Makefile index 46b910a..f85ac71 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -29,7 +29,7 @@ OBJS = send.o recv.o printer.o link.o uart.o RECV_NAME = recv RECV_BIN = $(RECV_NAME).bin RECV_ELF = $(RECV_NAME).elf -RECV_OBJS = recv.o link.o uart.o +RECV_OBJS = recv.o printer.o link.o uart.o SEND_NAME = send SEND_BIN = $(SEND_NAME).bin diff --git a/avr/printer.c b/avr/printer.c index 3fed9c1..17000d6 100644 --- a/avr/printer.c +++ b/avr/printer.c @@ -3,6 +3,24 @@ #include #include +int tabby_avr_printer_packet_toolarge(uint8_t type, uint16_t size) { + switch (type) { + case TABBY_PRINTER_PACKET_JOB: { + if (size > sizeof(tabby_printer_job)) { + return 1; + } + } + + case TABBY_PRINTER_PACKET_DATA: { + if (size > TABBY_PRINTER_PACKET_MAX_SIZE) { + return 1; + } + } + } + + return 0; +} + static uint16_t checksum(tabby_printer_packet *header, uint8_t *body) { uint16_t sum = 0; diff --git a/avr/recv.c b/avr/recv.c index 27392b7..6d5046a 100644 --- a/avr/recv.c +++ b/avr/recv.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include @@ -66,12 +66,13 @@ ISR(SPI_STC_vect) { case 5: { header.size |= in << 8; - i++; - if (header.size > TABBY_PRINTER_PACKET_MAX_SIZE) { + if (tabby_avr_printer_packet_toolarge(header.type, header.size)) { i = 0; } + i++; + break; } diff --git a/avr/send.c b/avr/send.c index 59bcb16..91476ab 100644 --- a/avr/send.c +++ b/avr/send.c @@ -115,6 +115,13 @@ int main() { header.size |= c << 8; i++; + if (tabby_avr_printer_packet_toolarge(header.type, + header.size)) { + i = 0; + b = 0; + sheet_offset = 0; + } + break; } diff --git a/include/tabby/avr/printer.h b/include/tabby/avr/printer.h index d12074e..390423e 100644 --- a/include/tabby/avr/printer.h +++ b/include/tabby/avr/printer.h @@ -5,6 +5,8 @@ #include +int tabby_avr_printer_packet_toolarge(uint8_t type, uint16_t size); + void tabby_avr_printer_send_packet(uint8_t type, uint8_t *body, uint16_t size,