Skip to content

echo: fix wrapping behavior of octal sequences#5218

Merged
cakebaker merged 4 commits intouutils:mainfrom
tertsdiepraam:echo-fix-octal-wrapping
Oct 3, 2023
Merged

echo: fix wrapping behavior of octal sequences#5218
cakebaker merged 4 commits intouutils:mainfrom
tertsdiepraam:echo-fix-octal-wrapping

Conversation

@tertsdiepraam
Copy link
Collaborator

@tertsdiepraam tertsdiepraam commented Aug 28, 2023

Funny behaviour in GNU:

$ echo -e "\0501"
A

Even though the octal code for A is \0101. The reason that happens is because 3 octal digits is equivalent to a 9 bit integer.

This is possibly a bug in GNU that nobody has fixed in 31 years or they just chose to ignore it since it is fairly minor. I was investigating this with @jdonszelmann and we found that GNU and the built-in echo for zsh and fish both also have this wrapping behaviour. Though it's not really a problem because it is all done using unsigned integers. In this PR, I've made sure to use the wrapping_* methods for arithmetic to further avoid any problems.

Some other utilities might need to be checked too. For instance printf:

$ env printf "\501"
A

I noticed this while I was refactoring print_escaped, which is why that function is changed too.

@tertsdiepraam tertsdiepraam force-pushed the echo-fix-octal-wrapping branch from dc4f5a2 to 9d4f325 Compare August 28, 2023 21:02
Some(n) => ret = ret.wrapping_mul(base).wrapping_add(n as u8),
None => break,
}
input.next();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though Option isn't marked #[must_use], it might be nice to do let _ = input.next()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though maybe clippy complains because it's technically not necessary, I always like it cause it's clear we don't care (instead of the unwrap above, which I guess could be the same let _ =

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least make 'em the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed and fixed!

@tertsdiepraam tertsdiepraam force-pushed the echo-fix-octal-wrapping branch from 9d4f325 to 4623575 Compare August 28, 2023 21:21
@cakebaker
Copy link
Contributor

I'm not sure whether this is outside the scope of this PR: GNU echo allows octal numbers without leading 0 and so the following works with GNU echo:

$ env echo -e "\0501" "\501"
A A

With your solution I get:

$ env cargo run echo -e "\0501" "\501"
A \501

@tertsdiepraam
Copy link
Collaborator Author

Yeah sure, I've added it.

@github-actions
Copy link

GNU testsuite comparison:

Skipping an intermittent issue tests/tail-2/inotify-dir-recreate

@tertsdiepraam tertsdiepraam force-pushed the echo-fix-octal-wrapping branch from 518e7c8 to 45487d4 Compare September 25, 2023 10:02
@github-actions
Copy link

github-actions bot commented Oct 3, 2023

GNU testsuite comparison:

Congrats! The gnu test tests/cp/cp-i is no longer failing!
Congrats! The gnu test tests/mv/mv-n is no longer failing!

@cakebaker cakebaker merged commit 139f205 into uutils:main Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants