[PATCH 0/1] unexpand: fix -f converting non-leading blanks
Diogo Santos
dsan.dev.71 at gmail.com
Tue Jan 14 17:35:41 UTC 2025
Hi,
This patch addresses bug report 16180 [1], which illustrates that
unexpand, when used with the --first-only (-f) option, may convert
non-leading blanks.
To clarify the behavior and reasoning behind the patch, here is
an outline of how unexpand processes its options and determines
default behavior.
BusyBox uses 'getopt32long' to parse command-line options, where
an 'unsigned int' and bit flags represent the -a and -f options.
The state of these options can be summarized as follows:
| Options | -a | -f |
| ------- | --- | --- |
| | 0 | 0 |
| a | 1 | 0 |
| f | 0 | 1 |
| t | 1 | 0 |
| ft | 0 | 1 |
| at | 1 | 0 |
| aft | 0 | 1 |
According to POSIX 2024 [2] and the GNU [3] documentation,
the default behavior is to convert only leading blanks.
However, as shown in the table, the first row doesn't contain a "1".
A similar behavior exists in GNU's unexpand, though it uses two
booleans instead of bit flags.
Despite this, both implementations correctly infer the default behavior.
This is because, when reading line by line, checking the state '-a' is
sufficient to determine its opposite, that is, whether unexpand must
"convert only leading blanks".
Knowing this, and assuming the program reaches line 163 with the
goal of converting only leading blanks (`!(opt & OPT_ALL)`):
if `ptr == line`, then no spaces or tabs were found, i.e.,
there are no leading blanks.
In this case, the line should be printed as-is, and
processing should continue to the next line.
Which is what the if body does here.
Thus, the second condition in the if statement can be dropped.
Best regards,
Diogo
[1]: https://bugs.busybox.net/show_bug.cgi?id=16180
[2]: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/unexpand.html#tag_20_136_16
[3]: https://www.gnu.org/software/coreutils/manual/html_node/unexpand-invocation.html
Diogo Santos (1):
unexpand: fix -f converting non-leading blanks
coreutils/expand.c | 4 ++--
testsuite/unexpand.tests | 12 ++++++++++--
2 files changed, 12 insertions(+), 4 deletions(-)
--
2.39.5
More information about the busybox
mailing list