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

"\xff" is parsed as weird bytes #856

Closed
Hecate2 opened this issue Jan 9, 2024 · 1 comment · Fixed by #1219
Closed

"\xff" is parsed as weird bytes #856

Hecate2 opened this issue Jan 9, 2024 · 1 comment · Fixed by #1219

Comments

@Hecate2
Copy link
Contributor

Hecate2 commented Jan 9, 2024

nccs 3.6.0

        public static ByteString EncodeIntegerFixedLength(BigInteger i, BigInteger fixedLength)
        {
            ByteString data = (ByteString)i;
            BigInteger lengthToComplement = fixedLength - data.Length;
            if (lengthToComplement < 0)
                throw new ArgumentException("Too long value " + data);
            if (lengthToComplement > 0)
            {
                ByteString suffix = "";
                for (; lengthToComplement > 0; --lengthToComplement)
                    suffix += i >= 0 ? "\x00" : "\xff";  // watch this
                data += suffix;
            }
            return data;
        }
# Code RelationalDB.cs line 332: "suffix += i >= 0 ? "\x00" : "\xff";"
1187 LDLOC2
1188 LDARG0
1189 PUSH0
1190 GE
1191 JMPIFNOT 07 # pos: 1198 (offset: 7)
1193 PUSHDATA1 00 # as text: "\x00"
1196 JMP 06 # pos: 1202 (offset: 6)
1198 PUSHDATA1 C3-BF # as text: "?"     # IT PUSHES "?" instead of "\xff"
1202 CAT
1203 CONVERT 28 # ByteString type
1205 DUP
1206 STLOC2
1207 DROP
invokefunction('encodeIntegerFixedLength', [-1, 4])
b'\xff\xc3\xbf\xc3\xbf\xc3\xbf'
# expected b'\xff\xff\xff\xff'

And it is not the ternary operator [condition]:[expr1]?[expr2] that lead to the problem. I used if else, and the result is still b'\xff\xc3\xbf\xc3\xbf\xc3\xbf':

        public static ByteString EncodeIntegerFixedLength(BigInteger i, BigInteger fixedLength)
        {
            ByteString data = (ByteString)i;
            BigInteger lengthToComplement = fixedLength - data.Length;
            if (lengthToComplement < 0)
                throw new ArgumentException("Too long value " + data);
            if (lengthToComplement > 0)
            {
                ByteString suffix = "";
                for (; lengthToComplement > 0; --lengthToComplement)
                {
                    if (i >= 0)
                        suffix += "\x00";
                    else
                        suffix += "\xff";
                }
                data += suffix;
            }
            return data;
        }
# Code RelationalDB.cs line 336: "suffix += "\xff";"
1204 LDLOC2
1205 PUSHDATA1 C3-BF # as text: "?"
1209 CAT
1210 CONVERT 28 # ByteString type
1212 DUP
1213 STLOC2
1214 DROP
@Hecate2
Copy link
Contributor Author

Hecate2 commented Jan 9, 2024

It is a duplicate of #794 , because at

Optional<object?> constant = model.GetConstantValue(syntax);

for syntax == StringLiteralExpression "\xff", the resulting constant.Value is "ÿ". And at
return Push(Utility.StrictUTF8.GetBytes(s));

Utility.StrictUTF8.GetBytes(s) returns "?" ("\xc2\xb6").
The workaround is

suffix += i >= 0 ? new byte[] { 0x00 } : new byte[] { 0xff };

@Hecate2 Hecate2 closed this as completed Jan 9, 2024
@Hecate2 Hecate2 changed the title "\xff" is parsed as question mark ? "\xff" is parsed as weird bytes Jan 9, 2024
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 a pull request may close this issue.

1 participant