From 00236595527e0a695a2e547d2fcb8c5310367735 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 6 Dec 2023 14:58:41 +0100 Subject: [PATCH] tftp - Remove the suggestion of null terimination of the custom_fread custom_fwrite functions - Update the tests --- project/Core/Src/tftp.c | 17 +++++++++++------ tests/tfpt_tests.cpp | 15 +++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 6173a1d..c674a42 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -73,17 +73,17 @@ void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) { * auto rolling over the offset * if the bytes to read is bigger than the remaining bytes * it will read the remaining bytes and set the bytes to 0 + * this function does not handle null termination * @param[out] buf The buffer to write the data to * @param[in] bytes The number of bytes to read * @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 - 1; + if (handle->offset + bytes > handle->len) { + bytes = handle->len - handle->offset; } memcpy(buf, handle->data + handle->offset, bytes); handle->offset += bytes; - ((char*)buf)[bytes] = '\0'; if (handle->offset > handle->len) { bytes = 0; } @@ -92,7 +92,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { /** * @brief tftp custom file functions to write the data - * auto rolling over the offset + * auto rolling over the offset + * if the bytes to write is bigger than the remaining bytes + * it will write the remaining bytes and set the bytes to 0 + * this function does not handle null termination + * if you want to write a null termination you have to add it to the data + * and set the len to the new length before calling this function * * @param buf The buffer to write the data to * @param bytes The number of bytes to write @@ -100,8 +105,8 @@ 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 - 1; + if (handle->offset + bytes > handle->len) { + bytes = handle->len - handle->offset; } memcpy(handle->data + handle->offset, buf, bytes); handle->offset += bytes; diff --git a/tests/tfpt_tests.cpp b/tests/tfpt_tests.cpp index aba9a79..f58a197 100644 --- a/tests/tfpt_tests.cpp +++ b/tests/tfpt_tests.cpp @@ -77,20 +77,27 @@ TEST(TFTP, custom_fwrite) { } TEST(TFTP, custom_fread_fwrite_suggested_by_Lorenz) { + char buf[21]; 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); + tftp_custom_fwrite("0987654321", 11, &write_file); + // the above function does not write the null terminator + EXPECT_EQ(write_file.offset, 21); EXPECT_EQ(write_file.len, 21); - EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 21), 0); + EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 20), 0); + + tftp_custom_fseek(&write_file, 0, SEEK_SET); + tftp_custom_fread(buf, 20, &write_file); + // the above function does not write the null terminator + buf[20] = '\0'; + EXPECT_EQ(memcmp(buf, "12345678901234567890", 21), 0); free(write_file.data); write_file.data = NULL; write_file.len = 0; - char buf[21]; file.offset = 9; tftp_custom_fread(buf, 20, &file); EXPECT_EQ(memcmp(buf, "0", 2), 0);