Commit bfd8746
Client Encryption: Fixes MemoryTextReader.ReadToEnd returning empty string (#5801)
# Fixes `MemoryTextReader.ReadToEnd` returning an empty string
## The bug
`Microsoft.Azure.Cosmos.Encryption.Custom.Transformation.MemoryTextReader.ReadToEnd()`
set `this.pos = this.length` **before** computing the remaining slice as
`(this.pos, this.length - this.pos)`. `this.length - this.pos` was
therefore always zero, and the method returned an empty string
regardless of the reader's contents or position.
```csharp
// before
this.pos = this.length;
Memory<char> remaining = this.chars.Slice(this.pos, this.length - this.pos); // always empty slice
return new string(remaining.ToArray());
```
## The fix
Swap the ordering so the slice is computed from the current position
first, then advance `pos`. Also replaces `Memory<char>.ToArray() + new
string(char[])` with `ReadOnlySpan<char>.ToString()` to avoid an
intermediate `char[]` allocation on every call.
```csharp
// after
int start = this.pos;
int count = this.length - start;
this.pos = this.length;
if (count == 0)
{
return string.Empty;
}
return this.chars.Span.Slice(start, count).ToString();
```
## Impact on production code: none today
The only in-tree caller of `MemoryTextReader` is
`JObjectSqlSerializer.Deserialize` (line 114), which hands the reader to
`Newtonsoft.Json.JsonTextReader`. `JsonTextReader` reads via
`Read(buffer, index, count)` — not `ReadToEnd` — so this bug has no
observable effect on the encrypt/decrypt paths today.
However, `MemoryTextReader` derives from `TextReader` and any future
consumer reaching for `ReadToEnd` would silently see an empty result.
This fix makes the method behave as its public `TextReader` contract
requires.
## Tests
Adds `MemoryTextReaderTests.cs` with 23 cases covering:
- The bug directly (`ReadToEnd_WhenFreshReader_ReturnsFullContent`,
cross-validated against `StringReader`).
- Edge cases: empty input, fully-consumed reader, double-`ReadToEnd`,
partial read then `ReadToEnd`, Unicode input.
- Argument validation: null buffer, negative index/count, count
exceeding buffer.
- Disposal guards: `Peek`/`Read`/`ReadLine`/`ReadToEnd` after `Close()`
all throw.
- Other previously-uncovered overrides: `Peek`, `Read()`,
`Read(buffer,index,count)`, `ReadLine`.
**Regression guard**: 6 of the new `ReadToEnd` tests fail against the
buggy implementation and pass against the fixed one:
```
Failed ReadToEnd_WhenFreshReader_ReturnsFullContent
Failed ReadToEnd_AfterPartialRead_ReturnsRemainderOnly
Failed ReadToEnd_CalledTwice_SecondCallReturnsEmptyString
Failed ReadToEnd_OnUnicodeContent_PreservesCharacters
Failed ReadToEnd_MatchesStringReaderBehaviour
Failed ReadToEnd_AfterPartialRead_MatchesStringReaderBehaviour
Failed! - Failed: 6, Passed: 17, Skipped: 0, Total: 23
```
After the fix: 23/23 pass; all 214 other encryption-custom tests pass
(`net8.0`, `--filter "TestCategory!=Integration"`).
## Risks
Minimal. The method had no observed callers relying on the broken
behaviour (it was always returning `""` which no consumer is plausibly
relying on). The fix is a 2-line swap plus a cheap allocation
optimisation. No public API surface changes.
## Checklist
- [x] Branch: `users/adamnova/memorytextreader-readtoend`
- [x] PR title matches the required regex (`Client Encryption: Fixes
MemoryTextReader.ReadToEnd returning empty string`)
- [x] `Co-authored-by: Copilot` trailer on the commit
- [x] No changes to `Directory.Build.props` / versioning / packaging
- [x] Both `net8.0` and `netstandard2.0` targets build
- [x] Unit tests added with regression coverage; full suite passes
- [x] Integration tests (require Cosmos emulator) — not run
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Kiran Kumar Kolli <kirankk@microsoft.com>1 parent dd06aae commit bfd8746
2 files changed
Lines changed: 254 additions & 2 deletions
File tree
- Microsoft.Azure.Cosmos.Encryption.Custom
- src/Transformation
- tests/Microsoft.Azure.Cosmos.Encryption.Custom.Tests/Transformation
Lines changed: 8 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
106 | 108 | | |
107 | | - | |
108 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
109 | 115 | | |
110 | 116 | | |
111 | 117 | | |
| |||
Lines changed: 246 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
0 commit comments