Page MenuHomeFreeBSD

col(1): fix incorrect output and segfault
ClosedPublic

Authored by yzhong_freebsdfoundation.org on Sep 23 2020, 7:00 PM.
Tags
None
Referenced Files
Unknown Object (File)
Feb 12 2024, 2:33 AM
Unknown Object (File)
Dec 20 2023, 6:37 AM
Unknown Object (File)
Dec 1 2023, 3:48 AM
Unknown Object (File)
Dec 1 2023, 3:48 AM
Unknown Object (File)
Nov 19 2023, 7:19 AM
Unknown Object (File)
Nov 19 2023, 5:50 AM
Unknown Object (File)
Nov 19 2023, 4:46 AM
Unknown Object (File)
Nov 19 2023, 3:46 AM
Subscribers

Details

Summary

If col(1) is called with -f in a simple test case, the last line of the input gets cut off:

$ printf 'hello\nworld' | col -f 
hello

as opposed to:

$ printf 'hello\nworld' | col 
hello
world

This is caused by the code not calculating the correct number of half-line feeds to add to the end of the file. This patch fixes this issue, and also changes an incorrect comment regarding the calculations.

Additionally fixes https://bugs.freebsd.org/249308:

col(1) segfaults with this simple test case:

$ printf 'hello\vworld\n' | col
     world
Segmentation fault

This issue is caused near the end of main() (line 344), when flush_lines() is called without setting the local variable pointing at the allocated lines to a valid value afterwards. The fix is quite simple, and its reasoning is described in the comment.

I understand that revisions are intended to contain only one change, but I thought that these two issues are small enough, and close enough together, that it would be more reasonable to put them into one revision.

Test Plan

Run the new tests in the tests directory.

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

Would you be willing to add some simple regression tests for these fixes based on the examples in the code review description? There are some tests already in usr.bin/col/tests/col_test.sh. The atf-sh-api man page has info on the shell functions you can use for writing tests, and there are lots of examples in the tree.

usr.bin/col/col.c
371

There should be a blank line here.

391

You can drop the cast of free()'s return value - it has none.

yzhong_freebsdfoundation.org edited the test plan for this revision. (Show Details)

Added tests for the issues fixed directly by this patch.
Also added some tests for half line feeds, as I found that it was very easy to affect how they behave when changing the whitespace calculation code.

Thanks for writing tests.

I think this is ok. It took me some time to wrap my head around this code.

usr.bin/col/col.c
112

Please fix the whitespace in the alloc_line() declaration as well.

This revision is now accepted and ready to land.Sep 25 2020, 5:32 PM

Fixed the whitespace problem. Can't believe I didn't see it, considering I saw the other one

This revision now requires review to proceed.Sep 25 2020, 6:02 PM
This revision was not accepted when it landed; it landed in state Needs Review.Oct 9 2020, 3:27 PM
Closed by commit rS366577: col(1): Fix a couple of bugs (authored by markj). · Explain Why
This revision was automatically updated to reflect the committed changes.