From 3c9eae47a61e925e5544cb24b0c3eb0445ac9a59 Mon Sep 17 00:00:00 2001 From: Gregor Rosenauer Date: Wed, 14 Aug 2024 17:54:15 +0200 Subject: [PATCH] harden and fix page number handling. --- bepdf/beos/BepdfApplication.cpp | 30 +++++++++++++++++------------- bepdf/beos/PDFWindow.cpp | 6 +++++- bepdf/beos/PDFWindow.h | 2 +- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/bepdf/beos/BepdfApplication.cpp b/bepdf/beos/BepdfApplication.cpp index 39a9676..15a8f1b 100644 --- a/bepdf/beos/BepdfApplication.cpp +++ b/bepdf/beos/BepdfApplication.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -60,7 +61,7 @@ static const char * GPLCopyright = "\n\n" "This program is free software under the GNU GPL v2, or any later version.\n"; -static const char *PAGE_NUM_MSG = "bepdf:page_num"; +static const char *PAGE_NUM_MSG_KEY = "bepdf:page_num"; static const char *settingsFilename = "BePDF"; @@ -528,24 +529,27 @@ bool BepdfApplication::QuitRequested() { /* Opens everything. */ -void BepdfApplication::RefsReceived ( BMessage * msg ) +void BepdfApplication::RefsReceived(BMessage *msg) { uint32 type; int32 count; - int32 i; - entry_ref ref; - mReadyToQuit = false; + status_t result; - msg->GetInfo ( "refs", &type, &count ); - if ( type != B_REF_TYPE ) { - return; - } + msg->GetInfo("refs", &type, &count); + + if (type != B_REF_TYPE) { + BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("Invalid file reference received!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); + error->Go(); + + return; + } BString ownerPassword, userPassword; const char *owner = NULL; const char *user = NULL; int32 pageNum = 0; + entry_ref ref; if (B_OK == msg->FindString("ownerPassword", &ownerPassword)) { owner = ownerPassword.String(); @@ -553,17 +557,17 @@ void BepdfApplication::RefsReceived ( BMessage * msg ) if (B_OK == msg->FindString("userPassword", &userPassword)) { user = userPassword.String(); } - status_t result = msg->FindInt32(PAGE_NUM_MSG, &pageNum); + result = msg->FindInt32(PAGE_NUM_MSG_KEY, &pageNum); if (result != B_OK) { if (result != B_NAME_NOT_FOUND) { - BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("BePDF: Error getting page number!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); + BAlert *error = new BAlert(B_TRANSLATE("Error"), B_TRANSLATE("Error getting page number!"), B_TRANSLATE("Close"), NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT); error->Go(); } } Initialize(); - for ( i = --count ; i >= 0; i-- ) { + for (int32 i = --count ; i >= 0; i-- ) { if ( msg->FindRef("refs", i, &ref ) == B_OK ) { /* Open the document... @@ -688,7 +692,7 @@ BepdfApplication::ArgvReceived (int32 argc, char **argv) // fprintf(errFile, "%s%s\n", pdfViewerCopyright, GPLCopyright); BMessage msg(B_REFS_RECEIVED); - msg.AddInt32 (PAGE_NUM_MSG, pg); + msg.AddInt32 (PAGE_NUM_MSG_KEY, pg); get_ref_for_path (argvCopy[1], &fileToOpen); msg.AddRef ("refs", &fileToOpen); PostMessage (&msg); diff --git a/bepdf/beos/PDFWindow.cpp b/bepdf/beos/PDFWindow.cpp index e12832b..4abbddd 100644 --- a/bepdf/beos/PDFWindow.cpp +++ b/bepdf/beos/PDFWindow.cpp @@ -973,8 +973,12 @@ PDFWindow::SetZoomSize(float w, float h) /////////////////////////////////////////////////////////// // update page list and page number item void -PDFWindow::SetPage(int16 page) { +PDFWindow::SetPage(int32 page) { char pageStr [64]; + if (page <= 0) page = 1; + if (page > mPagesView->CountItems()) { + page = mPagesView->CountItems(); + } snprintf (pageStr, sizeof (pageStr), "%d", page); mPageNumberItem->SetText (pageStr); mPagesView->Select(page-1); diff --git a/bepdf/beos/PDFWindow.h b/bepdf/beos/PDFWindow.h index e9d30d2..49e1737 100644 --- a/bepdf/beos/PDFWindow.h +++ b/bepdf/beos/PDFWindow.h @@ -283,7 +283,7 @@ class PDFWindow void SetZoomSize (float w, float h); void SetZoom(int16 zoom); void SetRotation(float rotation); - void SetPage(int16 page); + void SetPage(int32 page); static void OpenPDF(const char* file); static bool OpenPDFHelp(const char* name);