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

Negative number into uint32 encodes/decodes incorrectly #93

Closed
eaguilera23 opened this issue Jul 4, 2018 · 2 comments
Closed

Negative number into uint32 encodes/decodes incorrectly #93

eaguilera23 opened this issue Jul 4, 2018 · 2 comments

Comments

@eaguilera23
Copy link

Set up for replication

defmodule Unsigned do
  use Protobuf, """
    message Negative {
      required uint32 issue = 1;
    }
end
  """

Replicate issue

iex> Unsigned.Negative.new(issue: -420) |> Unsigned.Negative.encode |> Unsigned.Negative.decode
%Unsigned.Negative{issue: 92}

iex> Unsigned.Negative.new(issue: -2) |> Unsigned.Negative.encode |> Unsigned.Negative.decode
** (FunctionClauseError) no function clause matching in :gpb.de_vi/4

    The following arguments were given to :gpb.de_vi/4:

        # 1
        ""

        # 2
        7

        # 3
        126

        # 4
        32

    (gpb) src/gpb.erl:856: :gpb.de_vi/4
    (gpb) src/gpb.erl:357: :gpb.decode_type/3
    (gpb) src/gpb.erl:244: :gpb.decode_field/4
    (exprotobuf) lib/exprotobuf/decoder.ex:22: Protobuf.Decoder.decode/2

Related issues (maybe)

mafintosh/protocol-buffers#62

@sneako
Copy link
Collaborator

sneako commented Jul 4, 2018

An unsigned integer is only supposed to hold positive values. You should be using int32 instead if you need to encode negative values.

@eaguilera23
Copy link
Author

Oh ok. I thought it would turn a -40 into 40 at encoding time. My bad then.

@sneako sneako closed this as completed Jul 4, 2018
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

No branches or pull requests

2 participants