From a8537ba6d465b00f1ddc3406fcceda45d1759d95 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Tue, 5 Dec 2023 22:29:15 +0100 Subject: [PATCH] tftp try to fix a bug Lorenz told me about in fwrite --- project/Core/Src/tftp.c | 8 ++++---- tests/mocs.c | 2 +- tests/mocs.h | 2 +- tests/tfpt_tests.cpp | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index b19a12e..da67f95 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -100,12 +100,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { * @return The number of bytes written */ size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) { - if (handle->offset + bytes > handle->len) { - bytes = handle->len - handle->offset; + if (handle->offset + bytes >= handle->len) { + bytes = handle->len - handle->offset - 1; } memcpy(handle->data + handle->offset, buf, bytes); handle->offset += bytes; - if (handle->offset > handle->len) { + if (handle->offset >= handle->len) { bytes = 0; } return bytes; @@ -171,7 +171,7 @@ void tftp_close(void* handle) { if (handle == &virt_file[VIRT_TEXT_TXT]) { lcd_clear_images(); lcd_clear_text(); - lcd_display_text((const char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16); + lcd_display_text((char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16); } if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP] diff --git a/tests/mocs.c b/tests/mocs.c index bceea4e..19db235 100644 --- a/tests/mocs.c +++ b/tests/mocs.c @@ -12,7 +12,7 @@ int tftp_init(struct tftp_context* context) { return 0; } -void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT* font) { +void lcd_display_text(const char* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT* font) { UNUSED(color); UNUSED(bg_color); UNUSED(font); diff --git a/tests/mocs.h b/tests/mocs.h index d978179..b67fab7 100644 --- a/tests/mocs.h +++ b/tests/mocs.h @@ -93,7 +93,7 @@ typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err); uint32_t logger_get_timestamp(void); -void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font); +void lcd_display_text(const char* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font); void lcd_draw_img_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos); void lcd_draw_gif_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos); void lcd_draw_bmp_img(uint8_t* bmp_buff, uint32_t x_pos, uint32_t y_pos); diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index 1fa2d95..597addf 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -74,4 +74,22 @@ TEST(TFTP, custom_fwrite) { free(write_file.data); write_file.data = NULL; write_file.len = 0; +} + +TEST(TFTP, custom_fread_fwrite_suggested_by_Lorentz) { + write_file.data = (char*)malloc(21 * sizeof(char)); + write_file.len = 21; + write_file.offset = 0; + tftp_custom_fwrite("1234567890123456789", 19, &write_file); + + tftp_custom_fwrite("0987654321", 10, &write_file); + EXPECT_EQ(write_file.offset, 20); + EXPECT_EQ(write_file.len, 21); + EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 20), 0); + free(write_file.data); + + char buf[21]; + file.offset = 9; + tftp_custom_fread(buf, 20, &file); + EXPECT_EQ(memcmp(buf, "0", 1), 0); } \ No newline at end of file