From 6522d7c9f47aeb8c6093cf8bd27ad8a175c40475 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Tue, 25 Jan 2022 20:45:37 -0600 Subject: [PATCH] Bugfix: Incorrect return result for fwrite_unlocked(). The return result for fwrite_unlocked() is not negative one. Zero or some value less that the length is returned. An error occurs when the return value is smaller than the given string length. I likely confused write() return results with fwrite_unlocked(). --- level_0/f_print/c/print.c | 19 ++++++------ level_0/f_print/c/print_to.h | 2 +- level_0/f_print/c/private-print.c | 64 ++++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/level_0/f_print/c/print.c b/level_0/f_print/c/print.c index e8cc5c6..a015f02 100644 --- a/level_0/f_print/c/print.c +++ b/level_0/f_print/c/print.c @@ -25,7 +25,7 @@ extern "C" { if (!output) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - if (fwrite_unlocked(&character, 1, 1, output) == -1) { + if (!fwrite_unlocked(&character, 1, 1, output)) { return F_status_set_error(F_output); } @@ -40,22 +40,22 @@ extern "C" { #endif // _di_level_0_parameter_checking_ if (character == 0x7f) { - if (fwrite_unlocked(f_print_sequence_delete_s, 1, 3, output) != -1) { + if (fwrite_unlocked(f_print_sequence_delete_s, 1, 3, output) == 3) { return F_none; } } else if (macro_f_utf_character_t_width_is(character) == 1) { - if (fwrite_unlocked(f_print_sequence_unknown_s, 1, 3, output) != -1) { + if (fwrite_unlocked(f_print_sequence_unknown_s, 1, 3, output) == 3) { return F_none; } } else if (macro_f_utf_character_t_width_is(character) > 1 || character > 0x1f) { - if (fwrite_unlocked(&character, 1, 1, output) != -1) { + if (fwrite_unlocked(&character, 1, 1, output)) { return F_utf; } } else { - if (fwrite_unlocked(f_print_sequence_set_control_s[character], 1, 3, output) != -1) { + if (fwrite_unlocked(f_print_sequence_set_control_s[character], 1, 3, output) == 3) { return F_none; } } @@ -66,6 +66,7 @@ extern "C" { #ifndef _di_f_print_character_safely_get_ f_string_t f_print_character_safely_get(const char character) { + return private_f_print_character_safely_get(character); } #endif // _di_f_print_character_safely_get_ @@ -545,14 +546,14 @@ extern "C" { if (safe) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } total = 0; } - if (fwrite_unlocked(safe, 1, 3, output) == -1) { + if (fwrite_unlocked(safe, 1, 3, output) < 3) { return F_status_set_error(F_output); } @@ -563,7 +564,7 @@ extern "C" { if (total + width >= F_print_write_max_d) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -580,7 +581,7 @@ extern "C" { } // for if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } } diff --git a/level_0/f_print/c/print_to.h b/level_0/f_print/c/print_to.h index 658e5f7..01437ca 100644 --- a/level_0/f_print/c/print_to.h +++ b/level_0/f_print/c/print_to.h @@ -1418,7 +1418,7 @@ extern "C" { * * F_output (with error bit) on any other failure. * - * @see fwrite_unlocked() + * @see write() * * @see f_utf_is_valid() */ diff --git a/level_0/f_print/c/private-print.c b/level_0/f_print/c/private-print.c index 5ac1b10..ecd41c1 100644 --- a/level_0/f_print/c/private-print.c +++ b/level_0/f_print/c/private-print.c @@ -19,7 +19,7 @@ extern "C" { total = F_print_write_max_d; } - if (fwrite_unlocked(string + i, 1, total, output) == -1) { + if (fwrite_unlocked(string + i, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -73,7 +73,7 @@ extern "C" { if (j < except.used && except.array[j] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -90,7 +90,7 @@ extern "C" { if (!string[i] || i + 1 == stop || total == F_print_write_max_d) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -125,7 +125,7 @@ extern "C" { if (at < except_at.used && except_at.array[at] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -143,7 +143,7 @@ extern "C" { if (in < except_in.used && except_in.array[in].start <= i && except_in.array[in].stop >= i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -162,7 +162,7 @@ extern "C" { if (!string[i] || i + 1 == stop || total == F_print_write_max_d) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -197,7 +197,7 @@ extern "C" { if (at < except_at.used && except_at.array[at] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -215,7 +215,7 @@ extern "C" { if (in < except_in.used && except_in.array[in].start <= i && except_in.array[in].stop >= i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -232,7 +232,7 @@ extern "C" { if (i + 1 == stop || total == F_print_write_max_d) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -272,7 +272,7 @@ extern "C" { if (at < except_at.used && except_at.array[at] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -290,7 +290,7 @@ extern "C" { if (in < except_in.used && except_in.array[in].start <= i && except_in.array[in].stop >= i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -310,7 +310,7 @@ extern "C" { } else { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -323,14 +323,14 @@ extern "C" { if (safe) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } total = 0; } - if (fwrite_unlocked(safe, 1, 3, output) == -1) { + if (fwrite_unlocked(safe, 1, 3, output) < 3) { return F_status_set_error(F_output); } @@ -340,7 +340,7 @@ extern "C" { } if (total + width >= F_print_write_max_d) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -353,7 +353,7 @@ extern "C" { } // while if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } } @@ -378,7 +378,7 @@ extern "C" { if (j < except.used && except.array[j] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -393,7 +393,7 @@ extern "C" { if (i + 1 == stop || total == F_print_write_max_d) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -432,7 +432,7 @@ extern "C" { if (j < except.used && except.array[j] == i) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -450,7 +450,7 @@ extern "C" { } else { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -465,14 +465,14 @@ extern "C" { if (safe) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } total = 0; } - if (fwrite_unlocked(safe, 1, 3, output) == -1) { + if (fwrite_unlocked(safe, 1, 3, output) < 3) { return F_status_set_error(F_output); } @@ -482,7 +482,7 @@ extern "C" { } if (total + width >= F_print_write_max_d) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -513,7 +513,7 @@ extern "C" { total = F_print_write_max_d; } - if (fwrite_unlocked(string + i, 1, total, output) == -1) { + if (fwrite_unlocked(string + i, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -531,7 +531,7 @@ extern "C" { } while (i < length && !string[i] && total < F_print_write_max_d); - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } } @@ -563,7 +563,7 @@ extern "C" { } else { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -576,14 +576,14 @@ extern "C" { if (safe) { if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } total = 0; } - if (fwrite_unlocked(safe, 1, 3, output) == -1) { + if (!fwrite_unlocked(safe, 1, 3, output) < 3) { return F_status_set_error(F_output); } @@ -593,7 +593,7 @@ extern "C" { } if (total + width >= F_print_write_max_d) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } @@ -606,7 +606,7 @@ extern "C" { } // while if (total) { - if (fwrite_unlocked(string + start, 1, total, output) == -1) { + if (fwrite_unlocked(string + start, 1, total, output) < total) { return F_status_set_error(F_output); } } @@ -641,7 +641,9 @@ extern "C" { #if !defined(_di_f_print_terminated_) || !defined(_di_f_print_raw_terminated_) f_status_t private_f_print_terminated(const f_string_t string, FILE *output) { - if (fwrite_unlocked(string, 1, strlen(string), output) == -1) { + const size_t length = strlen(string); + + if (fwrite_unlocked(string, 1, length, output) < length) { return F_status_set_error(F_output); } -- 1.8.3.1