From 159cb22bb16f90f0e1370b343959ac506470a007 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 5 Dec 2022 18:33:44 -0600 Subject: [PATCH] Bugfix: Invalid print of character when a placeholder should be printed in byte_dump. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The following is happening: # clear ; echo -n "xa" | byte_dump -wt 2 && echo -n "∩" | byte_dump -wt 2 && echo -n "∩xa" | byte_dump -wt 2 Piped Byte Dump: (in Hexidecimal) 0000000000000000 78 61 | x a | Piped Byte Dump: (in Hexidecimal) 0000000000000000 e2 88 | ∩ | 0000000000000001 a9 | ∩ | Piped Byte Dump: (in Hexidecimal) 0000000000000000 e2 88 | ∩ | 0000000000000001 a9 78 | x | 0000000000000002 61 | a | In the second case the line 0000000000000001 should not print the string '| ∩ |'. This is happening because the character is not being properly reset in the situation where the overflow happens at the end of input. With this change the results should now be: # clear ; echo -n "xa" | byte_dump -wt 2 && echo -n "∩" | byte_dump -wt 2 && echo -n "∩xa" | byte_dump -wt 2 Piped Byte Dump: (in Hexidecimal) 0000000000000000 78 61 | x a | Piped Byte Dump: (in Hexidecimal) 0000000000000000 e2 88 | ∩ | 0000000000000001 a9 | | Piped Byte Dump: (in Hexidecimal) 0000000000000000 e2 88 | ∩ | 0000000000000001 a9 78 | x | 0000000000000002 61 | a | Perform a trivial cleanup/optimization where inverse of bytes is being checked for. This is pointless when there is an else block. Reverse the order and remove the "not" operator. --- level_3/byte_dump/c/private-byte_dump.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/level_3/byte_dump/c/private-byte_dump.c b/level_3/byte_dump/c/private-byte_dump.c index 156b206..c85cc99 100644 --- a/level_3/byte_dump/c/private-byte_dump.c +++ b/level_3/byte_dump/c/private-byte_dump.c @@ -70,7 +70,15 @@ extern "C" { byte_get = getc(file.stream); - if (byte_get < 0) break; + if (byte_get < 0) { + if (width_utf == -1 && character_reset) { + sequence.used = 0; + character_reset = F_false; + memset(&invalid, 0, sizeof(f_char_t) * data->width); + } + + break; + } byte = (f_char_t) byte_get; @@ -560,14 +568,14 @@ extern "C" { cell->column = 0; ++cell->row; - if (!bytes) { - previous->bytes = 0; - previous->invalid = 0; - } - else { + if (bytes) { previous->bytes = bytes; previous->invalid = invalid[current]; } + else { + previous->bytes = 0; + previous->invalid = 0; + } } else { if (data->main->parameters.array[byte_dump_parameter_unicode_e].result == f_console_result_found_e) { -- 1.8.3.1