Skip to content

Commit a6c6ff5

Browse files
committed
Add bound and pointer checks to memmove calls
1 parent 475b00a commit a6c6ff5

File tree

1 file changed

+63
-21
lines changed

1 file changed

+63
-21
lines changed

src/CLR/CorLib/corlib_native_System_Number.cpp

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,30 +196,45 @@ int Library_corlib_native_System_Number::GetDotIndex(char *buffer, int bufferCon
196196
void Library_corlib_native_System_Number::RoundUpNumStr(char *buffer, int *bufferContentLength)
197197
{
198198
char *c = &buffer[*bufferContentLength - 1];
199+
199200
for (;;)
200201
{
201202
if (*c != '.' && *c != '-')
202203
{
203204
*c += 1;
205+
204206
if (*c <= '9')
207+
{
205208
break;
209+
}
210+
206211
*c = '0';
207212
}
213+
208214
if (c == buffer)
209215
{
210216
if (*c == '-')
211217
{
212-
memmove(&buffer[2], &buffer[1], *bufferContentLength + 1);
213-
buffer[1] = '1';
218+
if (*bufferContentLength > 1)
219+
{
220+
memmove(&buffer[2], &buffer[1], *bufferContentLength + 1);
221+
buffer[1] = '1';
222+
}
214223
}
215224
else
216225
{
217-
memmove(&buffer[1], buffer, *bufferContentLength + 1);
218-
buffer[0] = '1';
226+
if (*bufferContentLength > 0)
227+
{
228+
memmove(&buffer[1], buffer, *bufferContentLength + 1);
229+
buffer[0] = '1';
230+
}
219231
}
232+
220233
(*bufferContentLength)++;
234+
221235
break;
222236
}
237+
223238
c--;
224239
}
225240
}
@@ -232,9 +247,12 @@ int Library_corlib_native_System_Number::ReplaceNegativeSign(char *buffer, int b
232247
{
233248
int negativeSignLength = GetStrLen(negativeSign);
234249

235-
memmove(&buffer[negativeSignLength], &buffer[1], bufferContentLength);
236-
memcpy(buffer, negativeSign, negativeSignLength);
237-
ret += negativeSignLength - 1;
250+
if (bufferContentLength > 1)
251+
{
252+
memmove(&buffer[negativeSignLength], &buffer[1], bufferContentLength);
253+
memcpy(buffer, negativeSign, negativeSignLength);
254+
ret += negativeSignLength - 1;
255+
}
238256
}
239257

240258
return ret;
@@ -248,13 +266,22 @@ int Library_corlib_native_System_Number::ReplaceDecimalSeparator(
248266
int ret = bufferContentLength;
249267

250268
int dotIndex = GetDotIndex(buffer, bufferContentLength);
269+
251270
if (dotIndex != -1)
252271
{
253272
int decimalSeparatorLength = GetStrLen(decimalSeparator);
254273

255-
memmove(&buffer[dotIndex + decimalSeparatorLength], &buffer[dotIndex + 1], bufferContentLength);
256-
memcpy(&buffer[dotIndex], decimalSeparator, decimalSeparatorLength);
257-
ret += decimalSeparatorLength - 1;
274+
if (bufferContentLength > dotIndex + 1)
275+
{
276+
memmove(
277+
&buffer[dotIndex + decimalSeparatorLength],
278+
&buffer[dotIndex + 1],
279+
bufferContentLength - dotIndex - 1);
280+
281+
memcpy(&buffer[dotIndex], decimalSeparator, decimalSeparatorLength);
282+
283+
ret += decimalSeparatorLength - 1;
284+
}
258285
}
259286

260287
return ret;
@@ -271,10 +298,12 @@ int Library_corlib_native_System_Number::InsertGroupSeparators(
271298
int significantDigitsStartAtIndex = 0;
272299
int significantDigitCount = bufferContentLength - 1;
273300
int dotIndex = GetDotIndex(buffer, bufferContentLength);
301+
274302
if (dotIndex != -1)
275303
{
276304
significantDigitCount = dotIndex - 1;
277305
}
306+
278307
if (buffer[0] == '-')
279308
{
280309
significantDigitCount--;
@@ -289,27 +318,40 @@ int Library_corlib_native_System_Number::InsertGroupSeparators(
289318
{
290319
ret = bufferContentLength + plusLength;
291320

292-
int srcIdx = bufferContentLength;
293-
int tgtIdx = ret;
321+
int sourceIdx = bufferContentLength;
322+
int targetIdx = ret;
294323

295324
if (dotIndex != -1)
296325
{
297326
int fractionPostfixWithDotLength = bufferContentLength - dotIndex;
298-
memmove(&buffer[dotIndex + plusLength], &buffer[dotIndex], fractionPostfixWithDotLength);
299-
srcIdx -= fractionPostfixWithDotLength;
300-
tgtIdx -= fractionPostfixWithDotLength;
327+
328+
if (bufferContentLength > dotIndex)
329+
{
330+
memmove(&buffer[dotIndex + plusLength], &buffer[dotIndex], fractionPostfixWithDotLength);
331+
332+
sourceIdx -= fractionPostfixWithDotLength;
333+
targetIdx -= fractionPostfixWithDotLength;
334+
}
301335
}
302336

303337
for (;;)
304338
{
305-
if ((srcIdx - significantDigitsStartAtIndex) <= groupSize)
339+
if ((sourceIdx - significantDigitsStartAtIndex) <= groupSize)
340+
{
306341
break;
342+
}
343+
344+
targetIdx -= groupSize;
345+
sourceIdx -= groupSize;
307346

308-
tgtIdx -= groupSize;
309-
srcIdx -= groupSize;
310-
memmove(&buffer[tgtIdx], &buffer[srcIdx], groupSize);
311-
tgtIdx -= groupSepLength;
312-
memcpy(&buffer[tgtIdx], groupSep, groupSepLength);
347+
if (bufferContentLength > sourceIdx)
348+
{
349+
memmove(&buffer[targetIdx], &buffer[sourceIdx], groupSize);
350+
351+
targetIdx -= groupSepLength;
352+
353+
memcpy(&buffer[targetIdx], groupSep, groupSepLength);
354+
}
313355
}
314356
}
315357

0 commit comments

Comments
 (0)