From c0d358ad234843a524a1bcc2a439f08ad0c64aa1 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 16 May 2017 12:17:50 -0400 Subject: Update formcalc return types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update return types to be bools instead of ints. Make some methods private. Change-Id: I5d55c48a861f90e9bd116ef835c11cdb515002a6 Reviewed-on: https://pdfium-review.googlesource.com/5553 Commit-Queue: dsinclair Reviewed-by: Nicolás Peña --- testing/libfuzzer/pdf_fm2js_fuzzer.cc | 2 +- xfa/fxfa/fm2js/xfa_fm2jscontext.cpp | 19 ++++++++----------- xfa/fxfa/fm2js/xfa_fm2jscontext.h | 4 ++-- xfa/fxfa/fm2js/xfa_fmparse.cpp | 4 ++-- xfa/fxfa/fm2js/xfa_fmparse.h | 8 ++++---- xfa/fxfa/fm2js/xfa_lexer.h | 20 +++++++++++--------- xfa/fxfa/fm2js/xfa_program.cpp | 14 +++++++------- xfa/fxfa/fm2js/xfa_program.h | 4 ++-- xfa/fxfa/parser/cxfa_scriptcontext.cpp | 2 +- 9 files changed, 38 insertions(+), 39 deletions(-) diff --git a/testing/libfuzzer/pdf_fm2js_fuzzer.cc b/testing/libfuzzer/pdf_fm2js_fuzzer.cc index 9afaa915ec..2f5b3ea7e4 100644 --- a/testing/libfuzzer/pdf_fm2js_fuzzer.cc +++ b/testing/libfuzzer/pdf_fm2js_fuzzer.cc @@ -18,7 +18,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { CFX_WideString input = CFX_WideString::FromUTF8(CFX_ByteStringC(data, safe_size.ValueOrDie())); CXFA_FMProgram program(input.AsStringC()); - if (program.ParseProgram()) + if (!program.ParseProgram()) return 0; CFX_WideTextBuf js; diff --git a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp index 716e378ed7..d0a111fa0d 100644 --- a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp +++ b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp @@ -2778,7 +2778,7 @@ void CXFA_FM2JSContext::Eval(CFXJSE_Value* pThis, } CFX_WideTextBuf wsJavaScriptBuf; - if (CXFA_FM2JSContext::Translate( + if (!CXFA_FM2JSContext::Translate( CFX_WideString::FromUTF8(utf8ScriptString.AsStringC()).AsStringC(), &wsJavaScriptBuf)) { pContext->ThrowCompilerErrorException(); @@ -5433,8 +5433,8 @@ void CXFA_FM2JSContext::eval_translation(CFXJSE_Value* pThis, CFX_WideString scriptString = CFX_WideString::FromUTF8(argString.AsStringC()); CFX_WideTextBuf wsJavaScriptBuf; - if (CXFA_FM2JSContext::Translate(scriptString.AsStringC(), - &wsJavaScriptBuf)) { + if (!CXFA_FM2JSContext::Translate(scriptString.AsStringC(), + &wsJavaScriptBuf)) { pContext->ThrowCompilerErrorException(); return; } @@ -6112,18 +6112,15 @@ CFX_ByteString CXFA_FM2JSContext::ValueToUTF8String(CFXJSE_Value* arg) { } // static. -int32_t CXFA_FM2JSContext::Translate(const CFX_WideStringC& wsFormcalc, - CFX_WideTextBuf* wsJavascript) { +bool CXFA_FM2JSContext::Translate(const CFX_WideStringC& wsFormcalc, + CFX_WideTextBuf* wsJavascript) { if (wsFormcalc.IsEmpty()) { wsJavascript->Clear(); - return 0; + return true; } - CXFA_FMProgram program(wsFormcalc); - int32_t status = program.ParseProgram(); - if (status) - return status; - return program.TranslateProgram(*wsJavascript); + CXFA_FMProgram program(wsFormcalc); + return program.ParseProgram() && program.TranslateProgram(*wsJavascript); } CXFA_FM2JSContext::CXFA_FM2JSContext(v8::Isolate* pScriptIsolate, diff --git a/xfa/fxfa/fm2js/xfa_fm2jscontext.h b/xfa/fxfa/fm2js/xfa_fm2jscontext.h index bde185d28c..6508852ba2 100644 --- a/xfa/fxfa/fm2js/xfa_fm2jscontext.h +++ b/xfa/fxfa/fm2js/xfa_fm2jscontext.h @@ -427,8 +427,8 @@ class CXFA_FM2JSContext : public CFXJSE_HostObject { CFXJSE_Value* src, bool* ret); - static int32_t Translate(const CFX_WideStringC& wsFormcalc, - CFX_WideTextBuf* wsJavascript); + static bool Translate(const CFX_WideStringC& wsFormcalc, + CFX_WideTextBuf* wsJavascript); void GlobalPropertyGetter(CFXJSE_Value* pValue); diff --git a/xfa/fxfa/fm2js/xfa_fmparse.cpp b/xfa/fxfa/fm2js/xfa_fmparse.cpp index a7ba901c89..ad3a92abc3 100644 --- a/xfa/fxfa/fm2js/xfa_fmparse.cpp +++ b/xfa/fxfa/fm2js/xfa_fmparse.cpp @@ -833,7 +833,7 @@ std::unique_ptr CXFA_FMParse::ParseBlockExpression() { std::unique_ptr CXFA_FMParse::ParseIfExpression() { uint32_t line = m_pToken->m_uLinenum; - const wchar_t* pStartPos = m_lexer->SavePos(); + const wchar_t* pStartPos = m_lexer->GetPos(); NextToken(); Check(TOKlparen); std::unique_ptr pExpression; @@ -851,7 +851,7 @@ std::unique_ptr CXFA_FMParse::ParseIfExpression() { m_pToken->m_type = TOKidentifier; m_pToken->m_wstring = L"if"; m_lexer->SetToken(std::move(pNewToken)); - m_lexer->RestorePos(pStartPos); + m_lexer->SetPos(pStartPos); return ParseExpExpression(); } Check(TOKthen); diff --git a/xfa/fxfa/fm2js/xfa_fmparse.h b/xfa/fxfa/fm2js/xfa_fmparse.h index 23b01b877c..8cf6b955ed 100644 --- a/xfa/fxfa/fm2js/xfa_fmparse.h +++ b/xfa/fxfa/fm2js/xfa_fmparse.h @@ -19,9 +19,12 @@ class CXFA_FMParse { ~CXFA_FMParse(); void NextToken(); + std::vector> ParseTopExpression(); + + private: void Check(XFA_FM_TOKEN op); void Error(uint32_t lineNum, const wchar_t* msg, ...); - std::vector> ParseTopExpression(); + bool HasError() const; std::unique_ptr ParseFunction(); std::unique_ptr ParseExpression(); std::unique_ptr ParseVarExpression(); @@ -47,9 +50,6 @@ class CXFA_FMParse { std::unique_ptr e); std::unique_ptr ParseIndexExpression(); - private: - bool HasError() const; - std::unique_ptr m_lexer; CXFA_FMToken* m_pToken; CXFA_FMErrorInfo* const m_pErrorInfo; diff --git a/xfa/fxfa/fm2js/xfa_lexer.h b/xfa/fxfa/fm2js/xfa_lexer.h index 0db9de58a9..7de76d7a62 100644 --- a/xfa/fxfa/fm2js/xfa_lexer.h +++ b/xfa/fxfa/fm2js/xfa_lexer.h @@ -108,21 +108,23 @@ class CXFA_FMLexer { ~CXFA_FMLexer(); CXFA_FMToken* NextToken(); - const wchar_t* Number(CXFA_FMToken* t, const wchar_t* p); - const wchar_t* String(CXFA_FMToken* t, const wchar_t* p); - const wchar_t* Identifiers(CXFA_FMToken* t, const wchar_t* p); - const wchar_t* Comment(const wchar_t* p); - XFA_FM_TOKEN IsKeyword(const CFX_WideStringC& p); + bool HasError() const; + void SetCurrentLine(uint32_t line) { m_uCurrentLine = line; } void SetToken(std::unique_ptr pToken) { m_pToken = std::move(pToken); } - const wchar_t* SavePos() { return m_ptr; } - void RestorePos(const wchar_t* pPos) { m_ptr = pPos; } - void Error(const wchar_t* msg, ...); - bool HasError() const; + + const wchar_t* GetPos() { return m_ptr; } + void SetPos(const wchar_t* pPos) { m_ptr = pPos; } private: + const wchar_t* Number(CXFA_FMToken* t, const wchar_t* p); + const wchar_t* String(CXFA_FMToken* t, const wchar_t* p); + const wchar_t* Identifiers(CXFA_FMToken* t, const wchar_t* p); + const wchar_t* Comment(const wchar_t* p); + XFA_FM_TOKEN IsKeyword(const CFX_WideStringC& p); + void Error(const wchar_t* msg, ...); std::unique_ptr Scan(); const wchar_t* m_ptr; diff --git a/xfa/fxfa/fm2js/xfa_program.cpp b/xfa/fxfa/fm2js/xfa_program.cpp index 6afe332629..8e5aa5ec3b 100644 --- a/xfa/fxfa/fm2js/xfa_program.cpp +++ b/xfa/fxfa/fm2js/xfa_program.cpp @@ -16,25 +16,25 @@ CXFA_FMProgram::CXFA_FMProgram(const CFX_WideStringC& wsFormcalc) CXFA_FMProgram::~CXFA_FMProgram() {} -int32_t CXFA_FMProgram::ParseProgram() { +bool CXFA_FMProgram::ParseProgram() { m_parse.NextToken(); if (!m_pErrorInfo.message.IsEmpty()) - return -1; + return false; std::vector> expressions = m_parse.ParseTopExpression(); if (!m_pErrorInfo.message.IsEmpty()) - return -1; + return false; std::vector arguments; m_globalFunction = pdfium::MakeUnique( 1, true, L"", std::move(arguments), std::move(expressions)); - return 0; + return true; } -int32_t CXFA_FMProgram::TranslateProgram(CFX_WideTextBuf& wsJavaScript) { +bool CXFA_FMProgram::TranslateProgram(CFX_WideTextBuf& wsJavaScript) { if (!m_globalFunction->ToJavaScript(wsJavaScript)) - return -1; + return false; wsJavaScript.AppendChar(0); - return 0; + return true; } diff --git a/xfa/fxfa/fm2js/xfa_program.h b/xfa/fxfa/fm2js/xfa_program.h index a00a5037da..641c72f4a2 100644 --- a/xfa/fxfa/fm2js/xfa_program.h +++ b/xfa/fxfa/fm2js/xfa_program.h @@ -17,8 +17,8 @@ class CXFA_FMProgram { explicit CXFA_FMProgram(const CFX_WideStringC& wsFormcalc); ~CXFA_FMProgram(); - int32_t ParseProgram(); - int32_t TranslateProgram(CFX_WideTextBuf& wsJavaScript); + bool ParseProgram(); + bool TranslateProgram(CFX_WideTextBuf& wsJavaScript); private: CXFA_FMErrorInfo m_pErrorInfo; diff --git a/xfa/fxfa/parser/cxfa_scriptcontext.cpp b/xfa/fxfa/parser/cxfa_scriptcontext.cpp index a3aed388be..cf6cb14fb9 100644 --- a/xfa/fxfa/parser/cxfa_scriptcontext.cpp +++ b/xfa/fxfa/parser/cxfa_scriptcontext.cpp @@ -153,7 +153,7 @@ bool CXFA_ScriptContext::RunScript(XFA_SCRIPTLANGTYPE eScriptType, m_pIsolate, m_JsContext.get(), m_pDocument); } CFX_WideTextBuf wsJavaScript; - if (CXFA_FM2JSContext::Translate(wsScript, &wsJavaScript)) { + if (!CXFA_FM2JSContext::Translate(wsScript, &wsJavaScript)) { hRetValue->SetUndefined(); return false; } -- cgit v1.2.3