Describe the bug
The line
array can either ends with \n\0
or \c\0
where \c
is an arbitrary character; depending on whether or not the length of input characters exceed the MAXLINE
limit or not.
With that, there are 2 bugs that were overlooked.
(1) In reverse function, setting int i_back = length(line) - 2
is incorrect since it's incorrectly assuming that the line ends with \n\0
in every scenario.
(2) In the case where the length of input characters exceed MAXLINE
, the while loop inside get_line
will terminate early by i < max_line_len - 1
check. However, there are input characters still remain in the iostream that did not get flush out, which causes output to be incorrect.
To Reproduce
Set MAXLINE
to 10 temporally so that it's easier to test.
Here is a copy of my terminal:
clalexni :: ~ » cc 1-19.c && ./a.out
0123456789 // input
7654321089 // incorrect output, expecting 876543210
- Notice that the output starts at 7 instead of 8. In this case the input is
012345678\0
; there is no \n
as described in (1).
- Notice that the output ends with 89. This is due to iostream not getting flushed as described in (2).
Another example:
clalexni :: ~ » cc 1-19.c&& ./a.out
012345678 // input
765432108 // incorrect output, expecting 876543210
Expected behavior
clalexni :: ~ » cc 1-19.c && ./a.out
0123456789 // input
876543210 // Expected output
clalexni :: ~ » cc 1-19.c&& ./a.out
012345678 // input
876543210 // Expected output