Skip to content

A char column compared with a number outside 0..65535 is stored as a comparison with another character #168

Closed

Description

@CaffeinatedCoder

Found by the pre-tag review of 948496a..0ee827d (the review for v11.0.0-preview.1).

What goes wrong

LambdaTranslator.CharCode (src/CodoMetis.EFCore.SchemaKit/Translation/Internal/LambdaTranslator.cs:403) turns the number a char column is compared with back into a character with (char)code, without checking that the number is a character code. A number outside 0..65535 wraps around to some other character:

Lambda C# over 'A' and 'ᅱ' Stored template
x => x.Grade == 65601 never true (0 rows) {Grade} = 'A'
x => x.Grade < 70000 always true (2 rows) {Grade} < 'ᅰ'
x => x.Grade > -1 always true (2 rows) {Grade} > '�'
x => x.Grade == captured (captured = 65601) never true {Grade} = 'A'

In a where: the constraint then holds over different rows than the model declares. Nothing reports it: the declaration is accepted, the migration scaffolds and applies.

The check #103 added asks only about conversions of what the lambda reads from the entity. Here the narrowing is done to the constant, so the check does not see it. The parity sweep leaves out rows that compare a char with a number (ParityCatalogue.cs:356-361), so it does not see it either.

This is older than the range: it came with the 5.x port and is in 948496a. #103 and #126 did not change it (#126 only dropped !IsConvertedChar(node) from the same line, which does not affect this).

Reproduction

var docs = new[] { new Doc { Grade = 'A' }, new Doc { Grade = 'ᅱ' } };
Expression<Func<Doc, bool>> eq = x => x.Grade == 65601;
docs.Count(eq.Compile());              // 0
LambdaTranslator.Predicate(eq);        // "{Grade} = 'A'"

Run in test/CodoMetis.EFCore.SchemaKit.Tests/Translation/ on 0ee827d: C#: eq=0 lt=2 gt=2 against SQL: eq='{Grade} = 'A'' lt='{Grade} < 'ᅰ'' gt='{Grade} > '�''.

Fix

Refuse a code outside char.MinValue..char.MaxValue where the lambda is written, with a message that says it is not a character. A trial of that change in a scratch copy passed every translator and parity test. x.Grade == 65 still gives 'A', and the out-of-range rows are refused. Add rows for a char against an int to the sweep, or say in the catalogue why they stay out.

Does it block preview.1?

No. It is silent, but only a lambda that compares a char with a number no character has reaches it, which nobody writes on purpose. Fix it for preview.2.

2026-11-17: the maintainer chose to fix it before the tag, in PR 173.

Activity

  1. added this to the 11.0.0 milestone on Nov 17, 2026
  2. added
    bug
    silent
    package/core
    severity/medium
    area/expression-indexes
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/expression-indexesbugpackage/coreseverity/mediumsilent

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions