Skip to content

Do not leak Decimal.Context changes into the caller process - #198

Open
ARAYN7011 wants to merge 1 commit into
elixir-ecto:masterfrom
ARAYN7011:fix-decimal-context-leak
Open

ARAYN7011 wants to merge 1 commit into
elixir-ecto:masterfrom
ARAYN7011:fix-decimal-context-leak

Conversation

@ARAYN7011

Copy link
Copy Markdown

Fixes #191.

Tds.Types called Decimal.Context.set/1 with the column precision (decode) or 38 (encode) and never restored it, so after any query the caller's Decimal.Context precision stayed changed.

  • decode_decimal/3: the call had no effect (Decimal.new/3 ignores the context), so it is removed.
  • Encoders: they only needed it for Decimal.abs/1, which rounds to the context precision. They now use a private abs_decimal/1 that flips the sign, so large values are not rounded and the context is never touched.

Test plan:

  • New test/types/decimal_context_test.exs (no SQL Server needed) sets precision 7, runs decode and all four encode paths (including a 22-digit value), and asserts the context is unchanged. Both tests fail before the change and pass after.
  • mix format --check-formatted clean.
  • No SQL Server here, so I did not run the DB-backed suite. CI will.

AI disclosure: prepared with AI assistance (Claude) and reviewed before submitting.

🤖 Generated with Claude Code

…imals

decode_decimal/3 and the decimal encoders set the process Decimal.Context
precision and never restored it. Decimal.new/3 does not read the context,
so decoding needs no precision; the encoders only used Decimal.abs/1,
which is now a plain sign flip.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.

Decoding a decimal column changes the caller's Decimal.Context and never restores it

1 participant