From 4429eaa2d0c5f07118a57418a469ee39461cd4c5 Mon Sep 17 00:00:00 2001
From: Bruce Dawson <brucedawson@google.com>
Date: Mon, 8 Dec 2014 16:19:45 -0800
Subject: Replace manual/error-prone/hard-to-verify arraysize calculations with
 safe FX_ArraySize macro.

pdfium has numerous places where the number of elements in an array is
calculated with expressions like:

    sizeof(cFormats)/sizeof(FX_LPCWSTR)

This is suboptimal because it is verbose, it is easy to get wrong, and
it cannot be determined through casual inspection whether the code is
correct. It will give incorrect results if cFormats is a pointer instead
of an array and it will give incorrect results if FX_LPCWSTR is not the
type of the array elements.

The FX_WSTRC macro in fx_string.h which I fixed was particularly scary
because it would silently misbehave if passed a pointer.

The FX_ArraySize macro which I have added and started using (taken from
arraysize in v8's macros.h) is easier to use and will always give correct
results. If passed a pointer it will fail to compile.

For this change I only fixed instances of sizeof(FX_LPCWSTR). There
appear to be about 150 other places in the pdfium code that could
benefit from using FX_ArraySize.

R=bo_xu@foxitsoftware.com, tsepez@chromium.org

Review URL: https://codereview.chromium.org/729293003
---
 fpdfsdk/src/javascript/PublicMethods.cpp | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

(limited to 'fpdfsdk/src')

diff --git a/fpdfsdk/src/javascript/PublicMethods.cpp b/fpdfsdk/src/javascript/PublicMethods.cpp
index 82bffe67bf..b29a5b4f3c 100644
--- a/fpdfsdk/src/javascript/PublicMethods.cpp
+++ b/fpdfsdk/src/javascript/PublicMethods.cpp
@@ -1679,11 +1679,11 @@ FX_BOOL CJS_PublicMethods::AFDate_Format(OBJ_METHOD_PARAMS)
 		L"yy-mm-dd", L"mmm-yy", L"mmmm-yy", L"mmm d, yyyy", L"mmmm d, yyyy",
 		L"m/d/yy h:MM tt", L"m/d/yy HH:MM" };
 
-	ASSERT(iIndex < sizeof(cFormats)/sizeof(FX_LPCWSTR));
+	ASSERT(iIndex < FX_ArraySize(cFormats));
 
 	if (iIndex < 0)
 		iIndex = 0;
-	if (iIndex >= sizeof(cFormats)/sizeof(FX_LPCWSTR))
+	if (iIndex >= FX_ArraySize(cFormats))
 		iIndex = 0;
 	CJS_Parameters newParams;
 	CJS_Value val(isolate,cFormats[iIndex]);
@@ -1710,11 +1710,11 @@ FX_BOOL CJS_PublicMethods::AFDate_Keystroke(OBJ_METHOD_PARAMS)
 		L"yy-mm-dd", L"mmm-yy", L"mmmm-yy", L"mmm d, yyyy", L"mmmm d, yyyy",
 		L"m/d/yy h:MM tt", L"m/d/yy HH:MM" };
 
-	ASSERT(iIndex<sizeof(cFormats)/sizeof(FX_LPCWSTR));
+	ASSERT(iIndex<FX_ArraySize(cFormats));
 
 	if (iIndex < 0)
 		iIndex = 0;
-	if (iIndex >= sizeof(cFormats)/sizeof(FX_LPCWSTR))
+	if (iIndex >= FX_ArraySize(cFormats))
 		iIndex = 0;
 	CJS_Parameters newParams;
 	CJS_Value val(isolate,cFormats[iIndex]);
@@ -1738,11 +1738,11 @@ FX_BOOL CJS_PublicMethods::AFTime_Format(OBJ_METHOD_PARAMS)
 	int iIndex = params[0];
 	FX_LPCWSTR cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"};
 
-	ASSERT(iIndex<sizeof(cFormats)/sizeof(FX_LPCWSTR));
+	ASSERT(iIndex<FX_ArraySize(cFormats));
 
 	if (iIndex < 0)
 		iIndex = 0;
-	if (iIndex >= sizeof(cFormats)/sizeof(FX_LPCWSTR))
+	if (iIndex >= FX_ArraySize(cFormats))
 		iIndex = 0;
 	CJS_Parameters newParams;
 	CJS_Value val(isolate,cFormats[iIndex]);
@@ -1764,11 +1764,11 @@ FX_BOOL CJS_PublicMethods::AFTime_Keystroke(OBJ_METHOD_PARAMS)
 	int iIndex = params[0];
 	FX_LPCWSTR cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"};
 
-	ASSERT(iIndex<sizeof(cFormats)/sizeof(FX_LPCWSTR));
+	ASSERT(iIndex<FX_ArraySize(cFormats));
 
 	if (iIndex < 0)
 		iIndex = 0;
-	if (iIndex >= sizeof(cFormats)/sizeof(FX_LPCWSTR))
+	if (iIndex >= FX_ArraySize(cFormats))
 		iIndex = 0;
 	CJS_Parameters newParams;
 	CJS_Value val(isolate,cFormats[iIndex]);
-- 
cgit v1.2.3