[MODE] use ARRAYSIZE, don't include windows.h, fix two possible out-of-bounds reads

CID 1206872
CID 1206873

svn path=/trunk/; revision=69079
This commit is contained in:
Christoph von Wittich 2015-09-07 07:05:33 +00:00
parent 81302aadd9
commit 4fdcca4b99

View file

@ -20,14 +20,16 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include <windows.h> #include <windef.h>
#include <winbase.h>
#include <winuser.h>
#include <wincon.h>
#include <stdio.h> #include <stdio.h>
#define MAX_PORTNAME_LEN 20 #define MAX_PORTNAME_LEN 20
#define MAX_COMPORT_NUM 10 #define MAX_COMPORT_NUM 10
#define MAX_COMPARAM_LEN 20 #define MAX_COMPARAM_LEN 20
#define NUM_ELEMENTS(a) (sizeof(a)/sizeof(a[0]))
#define ASSERT(a) #define ASSERT(a)
const WCHAR* const usage_strings[] = const WCHAR* const usage_strings[] =
@ -63,7 +65,7 @@ int Usage()
int i; int i;
wprintf(L"\nConfigures system devices.\n\n"); wprintf(L"\nConfigures system devices.\n\n");
for (i = 0; i < NUM_ELEMENTS(usage_strings); i++) for (i = 0; i < ARRAYSIZE(usage_strings); i++)
{ {
wprintf(L"%s\n", usage_strings[i]); wprintf(L"%s\n", usage_strings[i]);
} }
@ -78,7 +80,7 @@ int QueryDevices()
WCHAR* ptr = buffer; WCHAR* ptr = buffer;
*ptr = L'\0'; *ptr = L'\0';
if (QueryDosDeviceW(NULL, buffer, NUM_ELEMENTS(buffer))) if (QueryDosDeviceW(NULL, buffer, ARRAYSIZE(buffer)))
{ {
while (*ptr != L'\0') while (*ptr != L'\0')
{ {
@ -117,7 +119,7 @@ int ShowParallelStatus(int nPortNum)
swprintf(szPortName, L"LPT%d", nPortNum); swprintf(szPortName, L"LPT%d", nPortNum);
wprintf(L"\nStatus for device LPT%d:\n", nPortNum); wprintf(L"\nStatus for device LPT%d:\n", nPortNum);
wprintf(L"-----------------------\n"); wprintf(L"-----------------------\n");
if (QueryDosDeviceW(szPortName, buffer, NUM_ELEMENTS(buffer))) if (QueryDosDeviceW(szPortName, buffer, ARRAYSIZE(buffer)))
{ {
WCHAR* ptr = wcsrchr(buffer, L'\\'); WCHAR* ptr = wcsrchr(buffer, L'\\');
if (ptr != NULL) if (ptr != NULL)
@ -226,12 +228,12 @@ int ShowSerialStatus(int nPortNum)
{ {
return 1; return 1;
} }
if (dcb.Parity > NUM_ELEMENTS(parity_strings)) if (dcb.Parity >= ARRAYSIZE(parity_strings))
{ {
wprintf(L"ERROR: Invalid value for Parity Bits %d:\n", dcb.Parity); wprintf(L"ERROR: Invalid value for Parity Bits %d:\n", dcb.Parity);
dcb.Parity = 0; dcb.Parity = 0;
} }
if (dcb.StopBits > NUM_ELEMENTS(stopbit_strings)) if (dcb.StopBits >= ARRAYSIZE(stopbit_strings))
{ {
wprintf(L"ERROR: Invalid value for Stop Bits %d:\n", dcb.StopBits); wprintf(L"ERROR: Invalid value for Stop Bits %d:\n", dcb.StopBits);
dcb.StopBits = 0; dcb.StopBits = 0;