Skip to content

Commit 0a62055

Browse files
Fix several C6xxx memory warnings in ANCM.
1 parent 2747640 commit 0a62055

File tree

12 files changed

+75
-264
lines changed

12 files changed

+75
-264
lines changed

src/Servers/IIS/AspNetCoreModuleV2/DefaultRules.ruleset

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@
291291
<Rule Id="C28736" Action="Error" />
292292
<Rule Id="C28750" Action="Error" />
293293
<Rule Id="C28751" Action="Error" />
294-
<Rule Id="C6001" Action="None" /> <!-- uninitialized memory -->
295-
<Rule Id="C6011" Action="None" /> <!-- Possible null deref -->
294+
<Rule Id="C6001" Action="Error" />
295+
<Rule Id="C6011" Action="Error" />
296296
<Rule Id="C6014" Action="Error" />
297297
<Rule Id="C6029" Action="Error" />
298298
<Rule Id="C6031" Action="None" /> <!-- Return value ignored: '<func>'. -->
@@ -303,7 +303,7 @@
303303
<Rule Id="C6064" Action="Error" />
304304
<Rule Id="C6066" Action="Error" />
305305
<Rule Id="C6067" Action="Error" />
306-
<Rule Id="C6101" Action="None" /> <!-- Returning uninitialized memory -->
306+
<Rule Id="C6101" Action="Error" />
307307
<Rule Id="C6200" Action="Error" />
308308
<Rule Id="C6201" Action="Error" />
309309
<Rule Id="C6211" Action="Error" />
@@ -347,7 +347,7 @@
347347
<Rule Id="C6280" Action="Error" />
348348
<Rule Id="C6281" Action="Error" />
349349
<Rule Id="C6282" Action="Error" />
350-
<Rule Id="C6283" Action="None" /> <!-- array new with scalar delete -->
350+
<Rule Id="C6283" Action="Error" />
351351
<Rule Id="C6284" Action="Error" />
352352
<Rule Id="C6285" Action="Error" />
353353
<Rule Id="C6286" Action="Error" />
@@ -396,8 +396,8 @@
396396
<Rule Id="C6381" Action="Error" />
397397
<Rule Id="C6383" Action="Error" />
398398
<Rule Id="C6384" Action="Error" />
399-
<Rule Id="C6385" Action="None" /> <!-- Reading invalid data -->
400-
<Rule Id="C6386" Action="None" /> <!-- Overrun -->
399+
<Rule Id="C6385" Action="Error" />
400+
<Rule Id="C6386" Action="Error" />
401401
<Rule Id="C6387" Action="None" /> <!-- '<expr>' could be '<val>': this does not adhere to the specification for the function '<func>' -->
402402
<Rule Id="C6388" Action="None" /> <!-- '<var>' might not be '<val>': this does not adhere to the specification for the function '<func>' -->
403403
<Rule Id="C6400" Action="Error" />

src/Servers/IIS/AspNetCoreModuleV2/IISLib/base64.cpp

Lines changed: 32 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Return Values:
5656
if (cchEncodedStringSize == 0 && pszEncodedString == nullptr) {
5757
return ERROR_SUCCESS;
5858
}
59+
else if (pszEncodedString == nullptr)
60+
{
61+
return ERROR_INVALID_PARAMETER;
62+
}
63+
64+
*pszEncodedString = 0;
5965

6066
if (cchEncodedStringSize < cchEncoded) {
6167
// Given buffer is too small to hold encoded string.
@@ -66,8 +72,16 @@ Return Values:
6672
ib = ich = 0;
6773
while (ib < cbDecodedBufferSize) {
6874
b0 = pbDecodedBuffer[ib++];
69-
b1 = (ib < cbDecodedBufferSize) ? pbDecodedBuffer[ib++] : 0;
70-
b2 = (ib < cbDecodedBufferSize) ? pbDecodedBuffer[ib++] : 0;
75+
b1 = 0;
76+
b2 = 0;
77+
if (ib < cbDecodedBufferSize)
78+
{
79+
b1 = pbDecodedBuffer[ib++];
80+
}
81+
if (ib < cbDecodedBufferSize)
82+
{
83+
b2 = pbDecodedBuffer[ib++];
84+
}
7185

7286
//
7387
// The checks below for buffer overflow seems redundant to me.
@@ -126,122 +140,6 @@ Return Values:
126140
}
127141

128142

129-
DWORD
130-
Base64Decode(
131-
__in PCWSTR pszEncodedString,
132-
__out_opt VOID * pDecodeBuffer,
133-
__in DWORD cbDecodeBufferSize,
134-
__out_opt DWORD * pcbDecoded
135-
)
136-
/*++
137-
138-
Routine Description:
139-
140-
Decode a base64-encoded string.
141-
142-
Arguments:
143-
144-
pszEncodedString (IN) - base64-encoded string to decode.
145-
cbDecodeBufferSize (IN) - size in bytes of the decode buffer.
146-
pbDecodeBuffer (OUT) - holds the decoded data.
147-
pcbDecoded (OUT) - number of data bytes in the decoded data (if success or
148-
STATUS_BUFFER_TOO_SMALL).
149-
150-
Return Values:
151-
152-
0 - success.
153-
E_OUTOFMEMORY
154-
E_INVALIDARG
155-
156-
--*/
157-
{
158-
constexpr auto NA = (255);
159-
#define DECODE(x) (((ULONG)(x) < sizeof(rgbDecodeTable)) ? rgbDecodeTable[x] : NA)
160-
161-
static BYTE rgbDecodeTable[128] = {
162-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, // 0-15
163-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, // 16-31
164-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 62, NA, NA, NA, 63, // 32-47
165-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NA, NA, NA, 0, NA, NA, // 48-63
166-
NA, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
167-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, NA, NA, NA, NA, NA, // 80-95
168-
NA, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
169-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, NA, NA, NA, NA, NA, // 112-127
170-
};
171-
172-
DWORD cbDecoded;
173-
DWORD cchEncodedSize;
174-
DWORD ich;
175-
DWORD ib;
176-
BYTE b0, b1, b2, b3;
177-
BYTE * pbDecodeBuffer = (BYTE *) pDecodeBuffer;
178-
179-
cchEncodedSize = (DWORD)wcslen(pszEncodedString);
180-
if (nullptr != pcbDecoded) {
181-
*pcbDecoded = 0;
182-
}
183-
184-
if ((0 == cchEncodedSize) || (0 != (cchEncodedSize % 4))) {
185-
// Input string is not sized correctly to be base64.
186-
return ERROR_INVALID_PARAMETER;
187-
}
188-
189-
// Calculate decoded buffer size.
190-
cbDecoded = (cchEncodedSize + 3) / 4 * 3;
191-
if (pszEncodedString[cchEncodedSize-1] == '=') {
192-
if (pszEncodedString[cchEncodedSize-2] == '=') {
193-
// Only one data byte is encoded in the last cluster.
194-
cbDecoded -= 2;
195-
}
196-
else {
197-
// Only two data bytes are encoded in the last cluster.
198-
cbDecoded -= 1;
199-
}
200-
}
201-
202-
if (nullptr != pcbDecoded) {
203-
*pcbDecoded = cbDecoded;
204-
}
205-
206-
if (cbDecodeBufferSize == 0 && pDecodeBuffer == nullptr) {
207-
return ERROR_SUCCESS;
208-
}
209-
210-
if (cbDecoded > cbDecodeBufferSize) {
211-
// Supplied buffer is too small.
212-
return ERROR_INSUFFICIENT_BUFFER;
213-
}
214-
215-
// Decode each four-byte cluster into the corresponding three data bytes.
216-
ich = ib = 0;
217-
while (ich < cchEncodedSize) {
218-
b0 = DECODE(pszEncodedString[ich]); ich++;
219-
b1 = DECODE(pszEncodedString[ich]); ich++;
220-
b2 = DECODE(pszEncodedString[ich]); ich++;
221-
b3 = DECODE(pszEncodedString[ich]); ich++;
222-
223-
if ((NA == b0) || (NA == b1) || (NA == b2) || (NA == b3)) {
224-
// Contents of input string are not base64.
225-
return ERROR_INVALID_PARAMETER;
226-
}
227-
228-
pbDecodeBuffer[ib++] = (b0 << 2) | (b1 >> 4);
229-
230-
if (ib < cbDecoded) {
231-
pbDecodeBuffer[ib++] = (b1 << 4) | (b2 >> 2);
232-
233-
if (ib < cbDecoded) {
234-
pbDecodeBuffer[ib++] = (b2 << 6) | b3;
235-
}
236-
}
237-
}
238-
239-
DBG_ASSERT(ib == cbDecoded);
240-
241-
return ERROR_SUCCESS;
242-
}
243-
244-
245143
DWORD
246144
Base64Encode(
247145
__in_bcount(cbDecodedBufferSize) VOID * pDecodedBuffer,
@@ -295,6 +193,12 @@ Return Values:
295193
if (cchEncodedStringSize == 0 && pszEncodedString == nullptr) {
296194
return ERROR_SUCCESS;
297195
}
196+
else if (pszEncodedString == nullptr)
197+
{
198+
return ERROR_INVALID_PARAMETER;
199+
}
200+
201+
*pszEncodedString = 0;
298202

299203
if (cchEncodedStringSize < cchEncoded) {
300204
// Given buffer is too small to hold encoded string.
@@ -305,8 +209,16 @@ Return Values:
305209
ib = ich = 0;
306210
while (ib < cbDecodedBufferSize) {
307211
b0 = pbDecodedBuffer[ib++];
308-
b1 = (ib < cbDecodedBufferSize) ? pbDecodedBuffer[ib++] : 0;
309-
b2 = (ib < cbDecodedBufferSize) ? pbDecodedBuffer[ib++] : 0;
212+
b1 = 0;
213+
b2 = 0;
214+
if (ib < cbDecodedBufferSize)
215+
{
216+
b1 = pbDecodedBuffer[ib++];
217+
}
218+
if (ib < cbDecodedBufferSize)
219+
{
220+
b2 = pbDecodedBuffer[ib++];
221+
}
310222

311223
//
312224
// The checks below for buffer overflow seems redundant to me.
@@ -363,120 +275,3 @@ Return Values:
363275

364276
return ERROR_SUCCESS;
365277
}
366-
367-
368-
DWORD
369-
Base64Decode(
370-
__in PCSTR pszEncodedString,
371-
__out_opt VOID * pDecodeBuffer,
372-
__in DWORD cbDecodeBufferSize,
373-
__out_opt DWORD * pcbDecoded
374-
)
375-
/*++
376-
377-
Routine Description:
378-
379-
Decode a base64-encoded string.
380-
381-
Arguments:
382-
383-
pszEncodedString (IN) - base64-encoded string to decode.
384-
cbDecodeBufferSize (IN) - size in bytes of the decode buffer.
385-
pbDecodeBuffer (OUT) - holds the decoded data.
386-
pcbDecoded (OUT) - number of data bytes in the decoded data (if success or
387-
STATUS_BUFFER_TOO_SMALL).
388-
389-
Return Values:
390-
391-
0 - success.
392-
E_OUTOFMEMORY
393-
E_INVALIDARG
394-
395-
--*/
396-
{
397-
#define NA (255)
398-
#define DECODE(x) (((ULONG)(x) < sizeof(rgbDecodeTable)) ? rgbDecodeTable[x] : NA)
399-
400-
static BYTE rgbDecodeTable[128] = {
401-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, // 0-15
402-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, // 16-31
403-
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 62, NA, NA, NA, 63, // 32-47
404-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, NA, NA, NA, 0, NA, NA, // 48-63
405-
NA, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
406-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, NA, NA, NA, NA, NA, // 80-95
407-
NA, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
408-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, NA, NA, NA, NA, NA, // 112-127
409-
};
410-
411-
DWORD cbDecoded;
412-
DWORD cchEncodedSize;
413-
DWORD ich;
414-
DWORD ib;
415-
BYTE b0, b1, b2, b3;
416-
BYTE * pbDecodeBuffer = (BYTE *) pDecodeBuffer;
417-
418-
cchEncodedSize = (DWORD)strlen(pszEncodedString);
419-
if (nullptr != pcbDecoded) {
420-
*pcbDecoded = 0;
421-
}
422-
423-
if ((0 == cchEncodedSize) || (0 != (cchEncodedSize % 4))) {
424-
// Input string is not sized correctly to be base64.
425-
return ERROR_INVALID_PARAMETER;
426-
}
427-
428-
// Calculate decoded buffer size.
429-
cbDecoded = (cchEncodedSize + 3) / 4 * 3;
430-
if (pszEncodedString[cchEncodedSize-1] == '=') {
431-
if (pszEncodedString[cchEncodedSize-2] == '=') {
432-
// Only one data byte is encoded in the last cluster.
433-
cbDecoded -= 2;
434-
}
435-
else {
436-
// Only two data bytes are encoded in the last cluster.
437-
cbDecoded -= 1;
438-
}
439-
}
440-
441-
if (nullptr != pcbDecoded) {
442-
*pcbDecoded = cbDecoded;
443-
}
444-
445-
if (cbDecodeBufferSize == 0 && pDecodeBuffer == nullptr) {
446-
return ERROR_SUCCESS;
447-
}
448-
449-
if (cbDecoded > cbDecodeBufferSize) {
450-
// Supplied buffer is too small.
451-
return ERROR_INSUFFICIENT_BUFFER;
452-
}
453-
454-
// Decode each four-byte cluster into the corresponding three data bytes.
455-
ich = ib = 0;
456-
while (ich < cchEncodedSize) {
457-
b0 = DECODE(pszEncodedString[ich]); ich++;
458-
b1 = DECODE(pszEncodedString[ich]); ich++;
459-
b2 = DECODE(pszEncodedString[ich]); ich++;
460-
b3 = DECODE(pszEncodedString[ich]); ich++;
461-
462-
if ((NA == b0) || (NA == b1) || (NA == b2) || (NA == b3)) {
463-
// Contents of input string are not base64.
464-
return ERROR_INVALID_PARAMETER;
465-
}
466-
467-
pbDecodeBuffer[ib++] = (b0 << 2) | (b1 >> 4);
468-
469-
if (ib < cbDecoded) {
470-
pbDecodeBuffer[ib++] = (b1 << 4) | (b2 >> 2);
471-
472-
if (ib < cbDecoded) {
473-
pbDecodeBuffer[ib++] = (b2 << 6) | b3;
474-
}
475-
}
476-
}
477-
478-
DBG_ASSERT(ib == cbDecoded);
479-
480-
return ERROR_SUCCESS;
481-
}
482-

src/Servers/IIS/AspNetCoreModuleV2/IISLib/base64.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ Base64Encode(
1515
__out_opt DWORD * pcchEncoded
1616
);
1717

18-
DWORD
19-
Base64Decode(
20-
__in PCWSTR pszEncodedString,
21-
__out_opt VOID * pDecodeBuffer,
22-
__in DWORD cbDecodeBufferSize,
23-
__out_opt DWORD * pcbDecoded
24-
);
25-
2618
DWORD
2719
Base64Encode(
2820
__in_bcount( cbDecodedBufferSize ) VOID * pDecodedBuffer,
@@ -32,13 +24,5 @@ Base64Encode(
3224
__out_opt DWORD * pcchEncoded
3325
);
3426

35-
DWORD
36-
Base64Decode(
37-
__in PCSTR pszEncodedString,
38-
__out_opt VOID * pDecodeBuffer,
39-
__in DWORD cbDecodeBufferSize,
40-
__out_opt DWORD * pcbDecoded
41-
);
42-
4327
#endif // _BASE64_HXX_
4428

src/Servers/IIS/AspNetCoreModuleV2/IISLib/buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ C_ASSERT( sizeof(VOID*) <= sizeof(ULONGLONG) );
259259
// bytes. If the buffer overflows then a heap buffer will be allocated.
260260
//
261261
#define STACK_BUFFER( _name, _size ) \
262-
ULONGLONG __aqw##_name[ ( ( (_size) + sizeof(ULONGLONG) - 1 ) / sizeof(ULONGLONG) ) ]; \
262+
ULONGLONG __aqw##_name[ ( ( (_size) + sizeof(ULONGLONG) - 1 ) / sizeof(ULONGLONG) ) ]{}; \
263263
BUFFER _name( (BYTE*)__aqw##_name, sizeof(__aqw##_name) )
264264

265265
//

0 commit comments

Comments
 (0)