Skip to content

Decimal-char in String can be > 255 and is then wrapped to be inside 0..255 #15869

@Booksbaum

Description

@Booksbaum

Decimal char notation:

\DDD (where D indicates a decimal digit; range of 000 - 255; for example, \231 = "ç")

BUT: a value > 255 is accepted too -- but gets wrapped into range (% 256):

> int 'Ω';;
val it: int = 937

> "\937";;
val it: string = "©"

> int ("\937"[0]);;
val it: int = 169
> 937 % 256;;
val it: int = 169

(fable repl)

Expected behavior

Should be compiler error, like in char:

> '\937';;
  ^^^^^^
stdin(85,1): error FS1158: This is not a valid character literal

Actual behavior

Decimal > 255 is accepted and wrapped into byte range.



I think issue is here:

fsharp/src/Compiler/lex.fsl

Lines 1185 to 1192 in 443b3ab

| trigraph
{ let (buf, _fin, m, kind, args) = sargs
let s = lexeme lexbuf
addByteChar buf (trigraph s.[1] s.[2] s.[3])
if not skip then
STRING_TEXT (LexCont.String(args.ifdefStack, args.stringNest, LexerStringStyle.SingleQuote, kind, args.interpolationDelimiterLength, m))
else
singleQuoteString sargs skip lexbuf }

There's no range check and addByteChar then wraps the value to byte range



I'm going to fix this together with the other char notations issues.




Related information

  • Operating system: Windows 11 x64
  • dotnet --version: 7.0.307, 8.0.100-preview.7.23376.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Diagnosticsmistakes and possible improvements to diagnosticsBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions