From 20736f7f5884cf1e2827543c92b6e47f8282aeaf Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 17 Aug 2018 16:44:50 +0000 Subject: Introduce safer CJS_Return::Success() and Failure(). Avoid the possibility of ever re-introducing the issue noticed last week. Remove some redundant JSGetStringFromID() calls. Change-Id: I56687c2191bd72e378f747083f34080e50cbe490 Reviewed-on: https://pdfium-review.googlesource.com/40490 Reviewed-by: Lei Zhang Commit-Queue: Tom Sepez --- fxjs/cjs_publicmethods.cpp | 186 ++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 93 deletions(-) (limited to 'fxjs/cjs_publicmethods.cpp') diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp index f2cd3ff7c1..86a2a777b9 100644 --- a/fxjs/cjs_publicmethods.cpp +++ b/fxjs/cjs_publicmethods.cpp @@ -874,17 +874,17 @@ CJS_Return CJS_PublicMethods::AFNumber_Format( const std::vector>& params) { #if _FX_OS_ != _FX_OS_ANDROID_ if (params.size() != 6) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventHandler* pEvent = pRuntime->GetCurrentEventContext()->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(L"No event handler"); + return CJS_Return::Failure(L"No event handler"); WideString& Value = pEvent->Value(); ByteString strValue = StrTrim(Value.ToDefANSI()); if (strValue.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); int iDec = abs(pRuntime->ToInt32(params[0])); int iSepStyle = ValidStyleOrZero(pRuntime->ToInt32(params[1])); @@ -972,7 +972,7 @@ CJS_Return CJS_PublicMethods::AFNumber_Format( } } #endif - return CJS_Return(); + return CJS_Return::Success(); } // function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, @@ -981,12 +981,12 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() < 2) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); CJS_EventHandler* pEvent = pContext->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); WideString& val = pEvent->Value(); WideString& wstrChange = pEvent->Change(); @@ -995,17 +995,17 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( if (pEvent->WillCommit()) { WideString swTemp = StrTrim(wstrValue); if (swTemp.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); NormalizeDecimalMarkW(&swTemp); if (!IsNumber(swTemp)) { pEvent->Rc() = false; WideString sError = JSGetStringFromID(JSMessage::kInvalidInputError); AlertIfPossible(pContext, sError); - return CJS_Return(sError); + return CJS_Return::Failure(sError); } // It happens after the last keystroke and before validating, - return CJS_Return(); + return CJS_Return::Success(); } WideString wstrSelected; @@ -1019,7 +1019,7 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( // can't insert "change" in front of sign position. if (!wstrSelected.IsEmpty() && pEvent->SelStart() == 0) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } } @@ -1031,7 +1031,7 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( if (wstrChange[i] == cSep) { if (bHasSep) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } bHasSep = true; continue; @@ -1039,16 +1039,16 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( if (wstrChange[i] == L'-') { if (bHasSign) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } // sign's position is not correct if (i != 0) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } if (pEvent->SelStart() != 0) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } bHasSign = true; continue; @@ -1056,12 +1056,12 @@ CJS_Return CJS_PublicMethods::AFNumber_Keystroke( if (!std::iswdigit(wstrChange[i])) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } } val = CalcMergedString(pEvent, wstrValue, wstrChange); - return CJS_Return(); + return CJS_Return::Success(); } // function AFPercent_Format(nDec, sepStyle) @@ -1070,17 +1070,17 @@ CJS_Return CJS_PublicMethods::AFPercent_Format( const std::vector>& params) { #if _FX_OS_ != _FX_OS_ANDROID_ if (params.size() != 2) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventHandler* pEvent = pRuntime->GetCurrentEventContext()->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); WideString& Value = pEvent->Value(); ByteString strValue = StrTrim(Value.ToDefANSI()); if (strValue.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); int iDec = abs(pRuntime->ToInt32(params[0])); int iSepStyle = ValidStyleOrZero(pRuntime->ToInt32(params[1])); @@ -1141,7 +1141,7 @@ CJS_Return CJS_PublicMethods::AFPercent_Format( strValue += '%'; Value = WideString::FromLocal(strValue.AsStringView()); #endif - return CJS_Return(); + return CJS_Return::Success(); } // AFPercent_Keystroke(nDec, sepStyle) @@ -1156,17 +1156,17 @@ CJS_Return CJS_PublicMethods::AFDate_FormatEx( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); CJS_EventHandler* pEvent = pContext->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); WideString& val = pEvent->Value(); WideString strValue = val; if (strValue.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); WideString sFormat = pRuntime->ToWideString(params[0]); double dDate; @@ -1182,11 +1182,11 @@ CJS_Return CJS_PublicMethods::AFDate_FormatEx( WideString swMsg = WideString::Format( JSGetStringFromID(JSMessage::kParseDateError).c_str(), sFormat.c_str()); AlertIfPossible(pContext, swMsg); - return CJS_Return(JSMessage::kParseDateError); + return CJS_Return::Failure(JSMessage::kParseDateError); } val = MakeFormatDate(dDate, sFormat); - return CJS_Return(); + return CJS_Return::Success(); } double CJS_PublicMethods::MakeInterDate(const WideString& strValue) { @@ -1230,40 +1230,39 @@ CJS_Return CJS_PublicMethods::AFDate_KeystrokeEx( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) { - return CJS_Return( - WideString(L"AFDate_KeystrokeEx's parameters' size r not correct")); + return CJS_Return::Failure( + WideString(L"AFDate_KeystrokeEx's parameter size not correct")); } CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); CJS_EventHandler* pEvent = pContext->GetEventHandler(); - if (pEvent->WillCommit()) { - if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); - - const WideString& strValue = pEvent->Value(); - if (strValue.IsEmpty()) - return CJS_Return(); - - WideString sFormat = pRuntime->ToWideString(params[0]); - bool bWrongFormat = false; - double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); - if (bWrongFormat || std::isnan(dRet)) { - WideString swMsg = WideString::Format( - JSGetStringFromID(JSMessage::kParseDateError).c_str(), - sFormat.c_str()); - AlertIfPossible(pContext, swMsg); - pEvent->Rc() = false; - return CJS_Return(); - } + if (!pEvent->WillCommit()) + return CJS_Return::Success(); + + if (!pEvent->m_pValue) + return CJS_Return::Failure(JSMessage::kBadObjectError); + + const WideString& strValue = pEvent->Value(); + if (strValue.IsEmpty()) + return CJS_Return::Success(); + + WideString sFormat = pRuntime->ToWideString(params[0]); + bool bWrongFormat = false; + double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); + if (bWrongFormat || std::isnan(dRet)) { + WideString swMsg = WideString::Format( + JSGetStringFromID(JSMessage::kParseDateError).c_str(), sFormat.c_str()); + AlertIfPossible(pContext, swMsg); + pEvent->Rc() = false; } - return CJS_Return(); + return CJS_Return::Success(); } CJS_Return CJS_PublicMethods::AFDate_Format( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); static constexpr const wchar_t* cFormats[] = {L"m/d", L"m/d/yy", @@ -1292,7 +1291,7 @@ CJS_Return CJS_PublicMethods::AFDate_Keystroke( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); static constexpr const wchar_t* cFormats[] = {L"m/d", L"m/d/yy", @@ -1321,7 +1320,7 @@ CJS_Return CJS_PublicMethods::AFTime_Format( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); static constexpr const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"}; @@ -1337,7 +1336,7 @@ CJS_Return CJS_PublicMethods::AFTime_Keystroke( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); static constexpr const wchar_t* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss", L"h:MM:ss tt"}; @@ -1366,12 +1365,12 @@ CJS_Return CJS_PublicMethods::AFSpecial_Format( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventHandler* pEvent = pRuntime->GetCurrentEventContext()->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); const WideString& wsSource = pEvent->Value(); WideString wsFormat; @@ -1394,7 +1393,7 @@ CJS_Return CJS_PublicMethods::AFSpecial_Format( } pEvent->Value() = CJS_Util::printx(wsFormat, wsSource); - return CJS_Return(); + return CJS_Return::Success(); } // function AFSpecial_KeystrokeEx(mask) @@ -1402,40 +1401,39 @@ CJS_Return CJS_PublicMethods::AFSpecial_KeystrokeEx( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() < 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); CJS_EventHandler* pEvent = pContext->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); const WideString& valEvent = pEvent->Value(); WideString wstrMask = pRuntime->ToWideString(params[0]); if (wstrMask.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); if (pEvent->WillCommit()) { if (valEvent.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); size_t iIndexMask = 0; for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { if (!MaskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) break; } - if (iIndexMask != wstrMask.GetLength() || (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { AlertIfPossible(pContext, JSGetStringFromID(JSMessage::kInvalidInputError)); pEvent->Rc() = false; } - return CJS_Return(); + return CJS_Return::Success(); } WideString& wideChange = pEvent->Change(); if (wideChange.IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); WideString wChange = wideChange; size_t iIndexMask = pEvent->SelStart(); @@ -1444,13 +1442,13 @@ CJS_Return CJS_PublicMethods::AFSpecial_KeystrokeEx( if (combined_len > wstrMask.GetLength()) { AlertIfPossible(pContext, JSGetStringFromID(JSMessage::kParamTooLongError)); pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { AlertIfPossible(pContext, JSGetStringFromID(JSMessage::kParamTooLongError)); pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } for (size_t i = 0; i < wChange.GetLength(); ++i) { @@ -1458,7 +1456,7 @@ CJS_Return CJS_PublicMethods::AFSpecial_KeystrokeEx( AlertIfPossible(pContext, JSGetStringFromID(JSMessage::kParamTooLongError)); pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } wchar_t wMask = wstrMask[iIndexMask]; if (!IsReservedMaskChar(wMask)) @@ -1466,12 +1464,12 @@ CJS_Return CJS_PublicMethods::AFSpecial_KeystrokeEx( if (!MaskSatisfied(wChange[i], wMask)) { pEvent->Rc() = false; - return CJS_Return(); + return CJS_Return::Success(); } iIndexMask++; } wideChange = std::move(wChange); - return CJS_Return(); + return CJS_Return::Success(); } // function AFSpecial_Keystroke(psf) @@ -1479,12 +1477,12 @@ CJS_Return CJS_PublicMethods::AFSpecial_Keystroke( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventHandler* pEvent = pRuntime->GetCurrentEventContext()->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); const char* cFormat = ""; switch (pRuntime->ToInt32(params[0])) { @@ -1514,7 +1512,7 @@ CJS_Return CJS_PublicMethods::AFMergeChange( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventHandler* pEventHandler = pRuntime->GetCurrentEventContext()->GetEventHandler(); @@ -1524,18 +1522,18 @@ CJS_Return CJS_PublicMethods::AFMergeChange( swValue = pEventHandler->Value(); if (pEventHandler->WillCommit()) - return CJS_Return(pRuntime->NewString(swValue.AsStringView())); + return CJS_Return::Success(pRuntime->NewString(swValue.AsStringView())); - WideString merged = - CalcMergedString(pEventHandler, swValue, pEventHandler->Change()); - return CJS_Return(pRuntime->NewString(merged.AsStringView())); + return CJS_Return::Success(pRuntime->NewString( + CalcMergedString(pEventHandler, swValue, pEventHandler->Change()) + .AsStringView())); } CJS_Return CJS_PublicMethods::AFParseDateEx( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 2) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); WideString sValue = pRuntime->ToWideString(params[0]); WideString sFormat = pRuntime->ToWideString(params[1]); @@ -1544,18 +1542,18 @@ CJS_Return CJS_PublicMethods::AFParseDateEx( WideString swMsg = WideString::Format( JSGetStringFromID(JSMessage::kParseDateError).c_str(), sFormat.c_str()); AlertIfPossible(pRuntime->GetCurrentEventContext(), swMsg); - return CJS_Return(JSMessage::kParseDateError); + return CJS_Return::Failure(JSMessage::kParseDateError); } - return CJS_Return(pRuntime->NewNumber(dDate)); + return CJS_Return::Success(pRuntime->NewNumber(dDate)); } CJS_Return CJS_PublicMethods::AFSimple( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 3) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); - return CJS_Return(pRuntime->NewNumber(static_cast(AF_Simple( + return CJS_Return::Success(pRuntime->NewNumber(static_cast(AF_Simple( pRuntime->ToWideString(params[0]).c_str(), pRuntime->ToDouble(params[1]), pRuntime->ToDouble(params[2]))))); } @@ -1564,7 +1562,7 @@ CJS_Return CJS_PublicMethods::AFMakeNumber( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); WideString ws = pRuntime->ToWideString(params[0]); NormalizeDecimalMarkW(&ws); @@ -1572,18 +1570,19 @@ CJS_Return CJS_PublicMethods::AFMakeNumber( v8::Local val = pRuntime->MaybeCoerceToNumber(pRuntime->NewString(ws.AsStringView())); if (!val->IsNumber()) - return CJS_Return(pRuntime->NewNumber(0)); - return CJS_Return(val); + return CJS_Return::Success(pRuntime->NewNumber(0)); + + return CJS_Return::Success(val); } CJS_Return CJS_PublicMethods::AFSimple_Calculate( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 2) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); if ((params[1].IsEmpty() || !params[1]->IsArray()) && !params[1]->IsString()) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CPDFSDK_InterForm* pReaderInterForm = pRuntime->GetFormFillEnv()->GetInterForm(); @@ -1664,24 +1663,24 @@ CJS_Return CJS_PublicMethods::AFSimple_Calculate( pRuntime->ToWideString(pRuntime->NewNumber(dValue)); } - return CJS_Return(); + return CJS_Return::Success(); } -/* This function validates the current event to ensure that its value is -** within the specified range. */ +// This function validates the current event to ensure that its value is +// within the specified range. CJS_Return CJS_PublicMethods::AFRange_Validate( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 4) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); CJS_EventContext* pContext = pRuntime->GetCurrentEventContext(); CJS_EventHandler* pEvent = pContext->GetEventHandler(); if (!pEvent->m_pValue) - return CJS_Return(JSMessage::kBadObjectError); + return CJS_Return::Failure(JSMessage::kBadObjectError); if (pEvent->Value().IsEmpty()) - return CJS_Return(); + return CJS_Return::Success(); double dEentValue = atof(pEvent->Value().ToDefANSI().c_str()); bool bGreaterThan = pRuntime->ToBoolean(params[0]); @@ -1712,14 +1711,14 @@ CJS_Return CJS_PublicMethods::AFRange_Validate( AlertIfPossible(pContext, swMsg); pEvent->Rc() = false; } - return CJS_Return(); + return CJS_Return::Success(); } CJS_Return CJS_PublicMethods::AFExtractNums( CJS_Runtime* pRuntime, const std::vector>& params) { if (params.size() != 1) - return CJS_Return(JSMessage::kParamError); + return CJS_Return::Failure(JSMessage::kParamError); WideString str = pRuntime->ToWideString(params[0]); if (str.GetLength() > 0 && IsDigitSeparatorOrDecimalMark(str[0])) @@ -1743,6 +1742,7 @@ CJS_Return CJS_PublicMethods::AFExtractNums( pRuntime->NewString(sPart.AsStringView())); } if (pRuntime->GetArrayLength(nums) > 0) - return CJS_Return(nums); - return CJS_Return(pRuntime->NewUndefined()); + return CJS_Return::Success(nums); + + return CJS_Return::Success(pRuntime->NewUndefined()); } -- cgit v1.2.3