From e54f33f4a5e037ab4945ecb6a12adba5a75ce63f Mon Sep 17 00:00:00 2001 From: Italo Sampaio Date: Mon, 25 Nov 2024 17:52:27 -0300 Subject: [PATCH] Incidentally, fixing intermittent error on srlp and btctx unit tests This error was happening intermittently in my local, and now has happened in the CI as well. Turns out ferror was not working well with fclose, since it would mistakenly report an error while errno is set to 0. --- firmware/src/powhsm/test/btctx/test_btctx.c | 6 ++++-- firmware/src/powhsm/test/srlp/test_srlp.c | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/firmware/src/powhsm/test/btctx/test_btctx.c b/firmware/src/powhsm/test/btctx/test_btctx.c index 0bea267d..8b372376 100644 --- a/firmware/src/powhsm/test/btctx/test_btctx.c +++ b/firmware/src/powhsm/test/btctx/test_btctx.c @@ -54,12 +54,14 @@ int read_hex_file(const char* file_name, unsigned char** buffer, size_t* len) { fread(tmp, 2, 1, f); read_hex(tmp, 2, *buffer + off); } - fclose(f); - if (ferror(f)) { return -1; } + if (fclose(f)) { + return -1; + } + return 0; } diff --git a/firmware/src/powhsm/test/srlp/test_srlp.c b/firmware/src/powhsm/test/srlp/test_srlp.c index b05a2b96..dba0d1ee 100644 --- a/firmware/src/powhsm/test/srlp/test_srlp.c +++ b/firmware/src/powhsm/test/srlp/test_srlp.c @@ -204,12 +204,15 @@ int read_block_file(const char* file_name, char** buffer, size_t* len) { *buffer = malloc(*len); fread(*buffer, *len, 1, f); - fclose(f); if (ferror(f)) { return -1; } + if (fclose(f)) { + return -1; + } + return 0; }