Cleanup: Remove more checks for new returning NULL.
authorLei Zhang <thestig@chromium.org>
Mon, 22 Jun 2015 22:13:36 +0000 (15:13 -0700)
committerLei Zhang <thestig@chromium.org>
Mon, 22 Jun 2015 22:13:36 +0000 (15:13 -0700)
R=jam@chromium.org

Review URL: https://codereview.chromium.org/1198903002.

fpdfsdk/include/fsdk_mgr.h
fpdfsdk/src/fpdfformfill.cpp
fpdfsdk/src/fsdk_mgr.cpp
fpdfsdk/src/javascript/app.cpp

index 4f2a87d..167d0d6 100644 (file)
@@ -170,59 +170,9 @@ public:
                }
        }
 
-       CFX_WideString JS_fieldBrowse()
-       {
-               if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Field_browse)
-               {
-                       int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, NULL, 0);
-                       if (nRequiredLen <= 0)
-                               return L"";
-
-                       char* pbuff = new char[nRequiredLen];
-                       if (!pbuff)
-                               return L"";
-
-                       memset(pbuff, 0, nRequiredLen);
-                       int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen);
-                       if (nActualLen <= 0 || nActualLen > nRequiredLen)
-                       {
-                               delete[] pbuff;
-                               return L"";
-                       }
-                       CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
-                       CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
-                       delete[] pbuff;
-                       return wsRet;
-               }
-               return L"";
-       }
+    CFX_WideString JS_fieldBrowse();
 
-       CFX_WideString JS_docGetFilePath()
-       {
-               if (m_pInfo && m_pInfo->m_pJsPlatform && m_pInfo->m_pJsPlatform->Doc_getFilePath)
-               {
-                       int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, NULL, 0);
-                       if (nRequiredLen <= 0)
-                               return L"";
-
-                       char* pbuff = new char[nRequiredLen];
-                       if (!pbuff)
-                               return L"";
-
-                       memset(pbuff, 0, nRequiredLen);
-                       int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(m_pInfo->m_pJsPlatform, pbuff, nRequiredLen);
-                       if (nActualLen <= 0 || nActualLen > nRequiredLen)
-                       {
-                               delete[] pbuff;
-                               return L"";
-                       }
-                       CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
-                       CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
-                       delete[] pbuff;
-                       return wsRet;
-               }
-               return L"";
-       }
+    CFX_WideString JS_docGetFilePath();
 
        void JS_docSubmitForm(void* formData, int length, const FX_WCHAR* URL)
        {
index 9358c9e..021a78a 100644 (file)
@@ -6,59 +6,43 @@
 
 #include "../../public/fpdf_formfill.h"
 #include "../../public/fpdfview.h"
+#include "../../third_party/base/nonstd_unique_ptr.h"
 #include "../include/fsdk_define.h"
 #include "../include/fsdk_mgr.h"
-
-
 #include "../include/javascript/IJavaScript.h"
 
-
-DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, FPDF_PAGE page,double page_x, double page_y)
+DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(
+    FPDF_FORMHANDLE hHandle, FPDF_PAGE page, double page_x, double page_y)
 {
-       if(!page || !hHandle)
-               return -1;
-       CPDF_Page * pPage = (CPDF_Page*) page;
-
-       CPDF_InterForm * pInterForm = NULL;
-       pInterForm = new CPDF_InterForm(pPage->m_pDocument,FALSE);
-       if (!pInterForm)
-               return -1;
-       CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
-       if(!pFormCtrl)
-       {
-               delete pInterForm;
-               return -1;
-       }
-       CPDF_FormField* pFormField = pFormCtrl->GetField();
-       if(!pFormField)
-       {
-               delete pInterForm;
-               return -1;
-       }
-
-       int nType = pFormField->GetFieldType();
-       delete pInterForm;
-       return nType;
+    if (!page || !hHandle)
+        return -1;
+    CPDF_Page* pPage = (CPDF_Page*) page;
+
+    nonstd::unique_ptr<CPDF_InterForm> pInterForm(
+        new CPDF_InterForm(pPage->m_pDocument, FALSE));
+    CPDF_FormControl* pFormCtrl = pInterForm->GetControlAtPoint(
+        pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y);
+    if (!pFormCtrl)
+        return -1;
+
+    CPDF_FormField* pFormField = pFormCtrl->GetField();
+    if(!pFormField)
+        return -1;
+
+    return pFormField->GetFieldType();
 }
 
-DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
+DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(
+    FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo)
 {
-       if(!document || !formInfo || formInfo->version!=1)
-               return NULL;
-       CPDF_Document * pDocument = (CPDF_Document*) document;
-       CPDFDoc_Environment * pEnv = NULL;
-       pEnv = new CPDFDoc_Environment(pDocument);
-       if (!pEnv)
-               return NULL;
-       pEnv->RegAppHandle(formInfo);
-
-       if(pEnv->GetPDFDocument())
-       {
-               CPDFSDK_Document* pSDKDoc = new CPDFSDK_Document(pEnv->GetPDFDocument(), pEnv);
-               if(pSDKDoc)
-                       pEnv->SetCurrentDoc(pSDKDoc);
-       }
-       return pEnv;
+    if (!document || !formInfo || formInfo->version != 1)
+        return nullptr;
+    CPDF_Document * pDocument = (CPDF_Document*) document;
+    CPDFDoc_Environment * pEnv = new CPDFDoc_Environment(pDocument);
+    pEnv->RegAppHandle(formInfo);
+    if (CPDF_Document* pEnvDocument = pEnv->GetPDFDocument())
+        pEnv->SetCurrentDoc(new CPDFSDK_Document(pEnvDocument, pEnv));
+    return pEnv;
 }
 
 DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle)
index ad27aeb..527538c 100644 (file)
@@ -5,6 +5,7 @@
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
 #include "../../public/fpdf_ext.h"
+#include "../../third_party/base/nonstd_unique_ptr.h"
 #include "../include/fsdk_define.h"
 #include "../include/fsdk_mgr.h"
 #include "../include/formfiller/FFL_FormFiller.h"
@@ -254,6 +255,55 @@ CPDFDoc_Environment::~CPDFDoc_Environment()
     m_pActionHandler = NULL;
 }
 
+CFX_WideString CPDFDoc_Environment::JS_fieldBrowse()
+{
+    if (!m_pInfo ||
+        !m_pInfo->m_pJsPlatform ||
+        !m_pInfo->m_pJsPlatform->Field_browse) {
+        return L"";
+    }
+
+    const int nRequiredLen = m_pInfo->m_pJsPlatform->Field_browse(
+            m_pInfo->m_pJsPlatform, nullptr, 0);
+    if (nRequiredLen <= 0)
+        return L"";
+
+    nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
+    memset(pBuff.get(), 0, nRequiredLen);
+    const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
+            m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
+    if (nActualLen <= 0 || nActualLen > nRequiredLen)
+        return L"";
+
+    CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
+    CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
+    return wsRet;
+}
+
+CFX_WideString CPDFDoc_Environment::JS_docGetFilePath()
+{
+    if (!m_pInfo ||
+        !m_pInfo->m_pJsPlatform ||
+        !m_pInfo->m_pJsPlatform->Doc_getFilePath) {
+        return L"";
+    }
+
+    const int nRequiredLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
+            m_pInfo->m_pJsPlatform, nullptr, 0);
+    if (nRequiredLen <= 0)
+        return L"";
+
+    nonstd::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
+    memset(pBuff.get(), 0, nRequiredLen);
+    const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
+            m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
+    if (nActualLen <= 0 || nActualLen > nRequiredLen)
+        return L"";
+
+    CFX_ByteString bsRet = CFX_ByteString(pBuff.get(), nActualLen);
+    CFX_WideString wsRet = CFX_WideString::FromLocal(bsRet);
+    return wsRet;
+}
 
 IFXJS_Runtime* CPDFDoc_Environment::GetJSRuntime()
 {
index 9d992d0..31e3131 100644 (file)
@@ -4,6 +4,7 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include "../../../third_party/base/nonstd_unique_ptr.h"
 #include "../../include/javascript/JavaScript.h"
 #include "../../include/javascript/IJavaScript.h"
 #include "../../include/javascript/JS_Define.h"
@@ -894,26 +895,27 @@ FX_BOOL app::response(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value
        CPDFDoc_Environment* pApp = pContext->GetReaderApp();
        ASSERT(pApp != NULL);
 
-       const int MAX_INPUT_BYTES = 2048;
-       char* pBuff = new char[MAX_INPUT_BYTES + 2];
-       if (!pBuff)
-               return FALSE;
-
-       memset(pBuff, 0, MAX_INPUT_BYTES + 2);
-       int nLengthBytes = pApp->JS_appResponse(swQuestion.c_str(), swTitle.c_str(), swDefault.c_str(),
-                                            swLabel.c_str(), bPassWord, pBuff, MAX_INPUT_BYTES);
-       if (nLengthBytes <= 0)
-       {
-               vRet.SetNull();
-               delete[] pBuff;
-               return FALSE;
-       }
-       if (nLengthBytes > MAX_INPUT_BYTES)
-               nLengthBytes = MAX_INPUT_BYTES;
-
-       vRet = CFX_WideString::FromUTF16LE((unsigned short*)pBuff, nLengthBytes / sizeof(unsigned short)).c_str();
-       delete[] pBuff;
-       return TRUE;
+    const int MAX_INPUT_BYTES = 2048;
+    nonstd::unique_ptr<char[]> pBuff(new char[MAX_INPUT_BYTES + 2]);
+    memset(pBuff.get(), 0, MAX_INPUT_BYTES + 2);
+    int nLengthBytes = pApp->JS_appResponse(swQuestion.c_str(),
+                                            swTitle.c_str(),
+                                            swDefault.c_str(),
+                                            swLabel.c_str(),
+                                            bPassWord,
+                                            pBuff.get(),
+                                            MAX_INPUT_BYTES);
+    if (nLengthBytes <= 0) {
+        vRet.SetNull();
+        return FALSE;
+    }
+    nLengthBytes = std::min(nLengthBytes, MAX_INPUT_BYTES);
+
+    CFX_WideString ret_string =
+        CFX_WideString::FromUTF16LE((unsigned short*)pBuff.get(),
+                                    nLengthBytes / sizeof(unsigned short));
+    vRet = ret_string.c_str();
+    return TRUE;
 }
 
 FX_BOOL app::media(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError)