From d9204ad9edfa932afb5c31f42371fb702d5952d5 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 6 Dec 2023 07:57:25 +0100 Subject: [PATCH] tftp try to fix a bug Lorenz told me about in fread --- project/Core/Src/tftp.c | 4 ++-- tests/tfpt_tests.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index da67f95..6173a1d 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -78,8 +78,8 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { * @param[in,out] handle Custom file handles */ size_t tftp_custom_fread(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(buf, handle->data + handle->offset, bytes); handle->offset += bytes; diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index 597addf..4a70f51 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -76,7 +76,7 @@ TEST(TFTP, custom_fwrite) { write_file.len = 0; } -TEST(TFTP, custom_fread_fwrite_suggested_by_Lorentz) { +TEST(TFTP, custom_fread_fwrite_suggested_by_Lorenz) { write_file.data = (char*)malloc(21 * sizeof(char)); write_file.len = 21; write_file.offset = 0;