Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

echo: fix wrapping behavior of octal sequences #5218

Merged
merged 4 commits into from
Oct 3, 2023

Conversation

tertsdiepraam
Copy link
Member

@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.


for _ in 1..max_digits {
match input.peek().and_then(|c| c.to_digit(base as u32)) {
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
Member Author

Choose a reason for hiding this comment

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

Agreed and fixed!

@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
Member Author

Yeah sure, I've added it.

@github-actions
Copy link

GNU testsuite comparison:

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

src/uu/echo/src/echo.rs Outdated Show resolved Hide resolved
src/uu/echo/src/echo.rs Outdated Show resolved Hide resolved
src/uu/echo/src/echo.rs Outdated Show resolved Hide resolved
src/uu/echo/src/echo.rs Outdated Show resolved Hide resolved
src/uu/echo/src/echo.rs Outdated Show resolved Hide resolved
@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
44 of 45 checks passed
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