Skip to content

Enforce integer bounds in type encoders#773

Merged
josevalim merged 1 commit into
elixir-ecto:masterfrom
Snehil-Shah:int-boundary
Jun 23, 2026
Merged

Enforce integer bounds in type encoders#773
josevalim merged 1 commit into
elixir-ecto:masterfrom
Snehil-Shah:int-boundary

Conversation

@Snehil-Shah

Copy link
Copy Markdown
Contributor

Started from interval and found quite a few types missing integer bound checks before stuffing the binary, silently wrapping the bits and corrupting data, without the user knowing.

I have added bound checks to the following types, such that it now rejects out-of-bound values like PG actually would:

  • inet
  • interval
  • macaddr
  • numeric
  • tid
  • tsvector

Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Comment on lines +41 to +48
# Lexeme positions can range from 1 to 16383 with a silent ceiling at the top.
# Ref: https://www.postgresql.org/docs/current/datatype-textsearch.html#DATATYPE-TSVECTOR
if position < 1 do
raise DBConnection.EncodeError,
Postgrex.Utils.encode_msg(position, 1..@max_position)
end

position = min(position, @max_position)

@Snehil-Shah Snehil-Shah Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

According to postgres docs:

"Position values can range from 1 to 16383; larger numbers are silently set to 16383"

Decided to follow their behavior, although I do feel that explicitly raising for max position can be a more consistent experience. WDYT?

@josevalim josevalim merged commit c482554 into elixir-ecto:master Jun 23, 2026
23 of 26 checks passed
@josevalim

Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

@Snehil-Shah Snehil-Shah deleted the int-boundary branch June 23, 2026 09:05
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.

2 participants