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

Incorrect error message from binary_to_term/2 with safe #5171

Closed
nathanl opened this issue Aug 30, 2021 · 5 comments · Fixed by #5172
Closed

Incorrect error message from binary_to_term/2 with safe #5171

nathanl opened this issue Aug 30, 2021 · 5 comments · Fixed by #5172
Assignees
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM

Comments

@nathanl
Copy link
Contributor

nathanl commented Aug 30, 2021

Describe the bug
If binary_to_term/2 is called with the safe option and the binary contains a non-existent atom, the error message indicates that the safe option itself is invalid.

To Reproduce

binary_to_term(<<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, [safe]).

The bad argument error says argument 2: invalid option in list. This is not correct; safe is a valid option.

If we then call the function without safe, it will allocate the atom a_new_atom.

binary_to_term(<<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, []).
a_new_atom

Now that the atom has been allocated, the safe option works just fine.

binary_to_term(<<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, [safe]).
a_new_atom

Expected behavior

The error message should indicate that the binary is unsafe.

Affected versions

This bug is present on master as of the latest commit, b2f0302 on 2021-07-23.

@nathanl nathanl added the bug Issue is reported as a bug label Aug 30, 2021
@vkatsuba
Copy link
Contributor

Not sure but looks like this is expected behavior, please see #binary_to_term-2:

1 > binary_to_term(<<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, [safe]). 
** exception error: bad argument
...
1> a_new_atom.
a_new_atom
2> binary_to_term(<<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, [safe]). 
a_new_atom

(C):

Use this option when receiving binaries from an untrusted source.

When enabled, it prevents decoding data that can be used to attack the Erlang runtime. In the event of receiving unsafe data, decoding fails with a badarg error.
....

@nathanl
Copy link
Contributor Author

nathanl commented Aug 30, 2021

I agree that failing with a badarg error is expected. That's not the bug.

The bug is that the badarg error indicates the wrong argument as being bad. It says that [safe] is an invalid option, which is not true.

What it should say is that <<131,100,0,10,97,95,110,101,119,95,97,116,111,109>>, which would cause a new atom to be allocated, is a bad argument.

@vkatsuba
Copy link
Contributor

Then this sounds like improvement, not a bug. If yes, maybe make sense to change the label.

@nathanl
Copy link
Contributor Author

nathanl commented Aug 30, 2021

If the error message were unclear, like "one of the arguments is bad", clarifying it would be an improvement. But it clearly indicates the wrong information - it says the second argument is bad when in fact the first one is bad. I would call that a bug.

But since this does not actually cause incorrect execution, but only makes debugging difficult, maybe you wouldn't call it a bug. It's definitely a small bug, at most.

I'm happy to leave classification to the maintainers.

@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Aug 30, 2021
@bjorng bjorng self-assigned this Aug 31, 2021
@bjorng
Copy link
Contributor

bjorng commented Aug 31, 2021

I consider it a bug.

bjorng added a commit that referenced this issue Sep 1, 2021
…OTP-17591

Correct extended error for binary_to_term/2
@bjorng bjorng closed this as completed in cb2b510 Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants