Commit 7cacdee
authored
Fix non-thread-safe use of Random in MultipartFormDataBinaryContent (#36)
* Fix non-thread-safe use of Random in MultipartFormDataBinaryContent
MultipartFormDataBinaryContent has a static Random instance that's being used without a lock. This locks around each access (an alternative would be to use and lazily-initialize on each access a [ThreadStatic] Random).
While fixing that, also improved a few things:
- We don't need to create a char[] for the boundary values; the const string can be used directly.
- For netstandard2.0, we don't need to allocate a char[]; a stackalloc'd span can be used directly.
- For .NET, we don't need a stackalloc, either; we can use string.Create and write directly into the new string instance.
- For .NET, we don't need a dedicated Random instance, either; we can use Random.Shared.
- For .NET, Random allows writing into a byte span, so we don't need the byte[] allocation either and can stackalloc it.
- For .NET, just %'ing by the string length produces as good or better code than using a manually-computed mask.
* Address PR feedback1 parent 674e0f7 commit 7cacdee
1 file changed
+36
-20
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
123 | 138 | | |
124 | 139 | | |
125 | | - | |
| 140 | + | |
126 | 141 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
134 | 147 | | |
135 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
136 | 152 | | |
137 | | - | |
| 153 | + | |
138 | 154 | | |
139 | | - | |
| 155 | + | |
140 | 156 | | |
141 | 157 | | |
142 | 158 | | |
143 | 159 | | |
| 160 | + | |
144 | 161 | | |
145 | 162 | | |
146 | 163 | | |
| |||
158 | 175 | | |
159 | 176 | | |
160 | 177 | | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
| 178 | + | |
165 | 179 | | |
166 | 180 | | |
167 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
168 | 184 | | |
169 | 185 | | |
170 | 186 | | |
171 | 187 | | |
172 | 188 | | |
173 | | - | |
| 189 | + | |
174 | 190 | | |
175 | 191 | | |
176 | | - | |
| 192 | + | |
177 | 193 | | |
178 | 194 | | |
179 | 195 | | |
| |||
0 commit comments