SBK1002: Binary prefix encoded as UTF-8 text
| Analyzer | SnowBank.Core |
| Severity | Warning (correctness) |
| Code fix | Yes |
Message
'{0}' starts with a binary prefix, and UTF-8 encodes that character as two bytes. Use Slice.FromByteString to write one byte per character.
Why
A key such as "\xFF/metadataVersion" is meant to start with the single byte 0xFF. Slice.FromString and Slice.FromStringUtf8 encode the string as UTF-8, and UTF-8 writes the character U+00FF as the two bytes C3 BF. The key then lands in the user keyspace instead of the system keyspace, and a comparison with the real key never matches.
The rule reports a Slice.FromString or Slice.FromStringUtf8 call on a string literal, or on an interpolated string whose literal head is known, when the first character is between U+0080 and U+00FF and the source writes it as an escape sequence ("\xFF/...", "ÿ..."). Text that starts with a Latin-1 letter written as the letter itself ("Über", "écrit") is valid UTF-8 text and is not reported. A literal with such a character later in the text, or text known only at run time, is not reported either.
Bad
Slice key = Slice.FromString("\xFF/metadataVersion");
Good
Slice key = Slice.FromByteString("\xFF/metadataVersion");
Slice.FromByteString writes the low byte of each character. It truncates a character above 0xFF, so use it only for text made of ASCII and binary prefixes.
Code fix
The fix renames FromString or FromStringUtf8 to FromByteString.
Suppress
#pragma warning disable SBK1002 // reason
or, for a whole project, in .editorconfig:
dotnet_diagnostic.SBK1002.severity = none