Ticket #11934: 0002-Deskbar-Sanitize-the-usage-of-BMessage-especially-fo.patch

File 0002-Deskbar-Sanitize-the-usage-of-BMessage-especially-fo.patch, 5.8 KB (added by Barrett, 5 years ago)

Confused Unset with Detach.

  • src/apps/deskbar/BarView.cpp

    From 0fa1d476060521d7d9fe15b48afe17a6085abf95 Mon Sep 17 00:00:00 2001
    From: Dario Casalinuovo <b.vitruvio@gmail.com>
    Date: Thu, 9 Apr 2015 16:22:37 +0200
    Subject: [PATCH 2/2] Deskbar: Sanitize the usage of BMessage especially for
     SendMessage and SendReply. Delete the item's message if AddItem does not
     return successfully.
    
    ---
     src/apps/deskbar/BarView.cpp    |  3 ++-
     src/apps/deskbar/BarWindow.cpp  | 16 ++++++++++------
     src/apps/deskbar/StatusView.cpp | 23 ++++++++++++-----------
     src/apps/deskbar/TimeView.cpp   |  4 ++--
     src/kits/interface/Shelf.cpp    | 17 ++++++++++++-----
     5 files changed, 38 insertions(+), 25 deletions(-)
    
    diff --git a/src/apps/deskbar/BarView.cpp b/src/apps/deskbar/BarView.cpp
    index 3f8fc8d..8634207 100644
    a b TBarView::MessageReceived(BMessage* message)  
    263263            // so that I can follow the common pathway
    264264            // for adding icons to the tray
    265265            int32 id;
    266             AddItem(new BMessage(*message), B_DESKBAR_TRAY, &id);
     266            if (AddItem(message, B_DESKBAR_TRAY, &id) == B_OK)
     267                Looper()->DetachCurrentMessage();
    267268            break;
    268269        }
    269270
  • src/apps/deskbar/BarWindow.cpp

    diff --git a/src/apps/deskbar/BarWindow.cpp b/src/apps/deskbar/BarWindow.cpp
    index 0fd262e..2ca7301 100644
    a b All rights reserved.  
    3939#include <stdio.h>
    4040
    4141#include <Application.h>
     42#include <AutoDeleter.h>
    4243#include <Catalog.h>
    4344#include <Directory.h>
    4445#include <FindDirectory.h>
    TBarWindow::AddItem(BMessage* message)  
    516517    BMessage reply;
    517518    status_t err = B_ERROR;
    518519
    519     BMessage archivedView;
    520     if (message->FindMessage("view", &archivedView) == B_OK) {
     520    BMessage* archivedView = new BMessage();
     521    ObjectDeleter<BMessage> deleter(archivedView);
     522    if (message->FindMessage("view", archivedView) == B_OK) {
    521523#if SHELF_AWARE
    522524        message->FindInt32("shelf", &shelf);
    523525#endif
    524         BMessage* archive = new BMessage(archivedView);
    525         err = fBarView->AddItem(archive, shelf, &id);
    526         if (err < B_OK)
    527             delete archive;
     526        err = fBarView->AddItem(archivedView, shelf, &id);
     527        if (err == B_OK) {
     528            // Detach the deleter since AddReplicant is taking ownership
     529            // on success. This should be changed on server side.
     530            deleter.Detach();
     531        }
    528532    } else if (message->FindRef("addon", &ref) == B_OK) {
    529533        BEntry entry(&ref);
    530534        err = entry.InitCheck();
  • src/apps/deskbar/StatusView.cpp

    diff --git a/src/apps/deskbar/StatusView.cpp b/src/apps/deskbar/StatusView.cpp
    index a0139cf..dde88ed 100644
    a b TReplicantTray::MessageReceived(BMessage* message)  
    337337            bool showDayOfWeek = fTime->ShowDayOfWeek();
    338338            bool showTimeZone = fTime->ShowTimeZone();
    339339
    340             BMessage* reply = new BMessage(kGetClockSettings);
    341             reply->AddBool("showClock", showClock);
    342             reply->AddBool("showSeconds", showSeconds);
    343             reply->AddBool("showDayOfWeek", showDayOfWeek);
    344             reply->AddBool("showTimeZone", showTimeZone);
    345             message->SendReply(reply);
     340            BMessage reply(kGetClockSettings);
     341            reply.AddBool("showClock", showClock);
     342            reply.AddBool("showSeconds", showSeconds);
     343            reply.AddBool("showDayOfWeek", showDayOfWeek);
     344            reply.AddBool("showTimeZone", showTimeZone);
     345            message->SendReply(&reply);
    346346            break;
    347347        }
    348348
    TReplicantTray::ShowHideTime()  
    451451
    452452    // Send a message to Time preferences telling it to update
    453453    BMessenger messenger("application/x-vnd.Haiku-Time");
    454     BMessage* message = new BMessage(kShowHideTime);
    455     message->AddBool("showClock", showClock);
    456     messenger.SendMessage(message);
     454    BMessage message(kShowHideTime);
     455    message.AddBool("showClock", showClock);
     456    messenger.SendMessage(&message);
    457457}
    458458
    459459
    TReplicantTray::LoadAddOn(BEntry* entry, int32* id, bool addToSettings)  
    673673    view->Archive(data);
    674674    delete view;
    675675
    676     AddIcon(data, id, &ref);
    677         // add the rep; adds info to list
     676    // add the rep; adds info to list
     677    if (AddIcon(data, id, &ref) != B_OK)
     678        delete data;
    678679
    679680    if (addToSettings) {
    680681        fAddOnSettings.AddString(kReplicantPathField, path.Path());
  • src/apps/deskbar/TimeView.cpp

    diff --git a/src/apps/deskbar/TimeView.cpp b/src/apps/deskbar/TimeView.cpp
    index 742b391..5a96ed8 100644
    a b TTimeView::MessageReceived(BMessage* message)  
    195195            be_roster->Launch("application/x-vnd.Haiku-Time");
    196196            // tell Time preflet to switch to the clock tab
    197197            BMessenger messenger("application/x-vnd.Haiku-Time");
    198             BMessage* switchToClock = new BMessage('SlCk');
    199             messenger.SendMessage(switchToClock);
     198            BMessage switchToClock('SlCk');
     199            messenger.SendMessage(&switchToClock);
    200200            break;
    201201        }
    202202
  • src/kits/interface/Shelf.cpp

    diff --git a/src/kits/interface/Shelf.cpp b/src/kits/interface/Shelf.cpp
    index 99cd707..7d215dd 100644
    a b  
    1616
    1717#include <pthread.h>
    1818
     19#include <AutoDeleter.h>
    1920#include <AutoLock.h>
    2021#include <Beep.h>
    2122#include <Dragger.h>
    BShelf::_InitData(BEntry *entry, BDataIO *stream, BView *view,  
    11781179                genCount = 1;
    11791180
    11801181            BMessage replicant;
    1181             BMessage *replmsg = NULL;
    1182             for (int32 i = 0; archive.FindMessage("replicant", i, &replicant) == B_OK; i++) {
     1182            for (int32 i = 0; archive.FindMessage("replicant", i, &replicant)
     1183                == B_OK; i++) {
    11831184                BPoint point;
    1184                 replmsg = new BMessage();
     1185                BMessage *replMsg = new BMessage();
     1186                ObjectDeleter<BMessage> deleter(replMsg);
    11851187                replicant.FindPoint("position", &point);
    1186                 replicant.FindMessage("message", replmsg);
    1187                 AddReplicant(replmsg, point);
     1188                if (replicant.FindMessage("message", replMsg) == B_OK)
     1189                    if (AddReplicant(replMsg, point) == B_OK) {
     1190                        // Detach the deleter since AddReplicant is taking
     1191                        // ownership on success. In R2 API this should be
     1192                        // changed to take always ownership on the message.
     1193                        deleter.Detach();
     1194                    }
    11881195            }
    11891196        }
    11901197    }