From 8d779aa8df90e02f0d5f0b497d1cb6d947ceaa0d Mon Sep 17 00:00:00 2001 From: Philippe Saint-Pierre Date: Wed, 23 Feb 2011 01:08:25 +0000 Subject: [PATCH] DiskUsage : * add two missing break; (CID 3301, 3302) * replace strcpy, strcat and sprintf by strlcpy, strlcat and snprintf (CID 6804, 6805, 6806, 6807, 6808, 8962, 8963) * remove a PrintToStream (left by accident for debugging purposes) git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40631 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/diskusage/DiskUsage.cpp | 8 +++++--- src/apps/diskusage/DiskUsage.h | 2 +- src/apps/diskusage/InfoWindow.cpp | 7 ++++--- src/apps/diskusage/PieView.cpp | 3 ++- src/apps/diskusage/Scanner.cpp | 2 +- src/apps/diskusage/Snapshot.cpp | 6 +++--- src/apps/diskusage/StatusView.cpp | 15 +++++++++------ src/apps/diskusage/VolumeView.cpp | 1 - 8 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/apps/diskusage/DiskUsage.cpp b/src/apps/diskusage/DiskUsage.cpp index 28d37e86a7..45a3c3d87c 100644 --- a/src/apps/diskusage/DiskUsage.cpp +++ b/src/apps/diskusage/DiskUsage.cpp @@ -19,7 +19,7 @@ entry_ref helpFileRef; bool helpFileWasFound = false; void -size_to_string(off_t byteCount, char* name) +size_to_string(off_t byteCount, char* name, int maxLength) { struct { off_t limit; @@ -37,13 +37,15 @@ size_to_string(off_t byteCount, char* name) }; if (byteCount < 1024) { - sprintf(name, B_TRANSLATE("%lld bytes"), byteCount); + snprintf(name, maxLength, B_TRANSLATE("%lld bytes"), + byteCount); } else { int i = 0; while (byteCount >= scale[i].limit) i++; - sprintf(name, scale[i].format, byteCount / scale[i].divisor); + snprintf(name, maxLength, scale[i].format, + byteCount / scale[i].divisor); } } diff --git a/src/apps/diskusage/DiskUsage.h b/src/apps/diskusage/DiskUsage.h index 729d9604f1..cdd222ee47 100644 --- a/src/apps/diskusage/DiskUsage.h +++ b/src/apps/diskusage/DiskUsage.h @@ -70,7 +70,7 @@ extern bool helpFileWasFound; #define deg2rad(x) (2.0 * M_PI * (x) / 360.0) #define rad2deg(x) (360.0 * (x) / (2.0 * M_PI)) -void size_to_string(off_t byteCount, char* name); +void size_to_string(off_t byteCount, char* name, int maxLength); #endif // DISKUSAGE_H diff --git a/src/apps/diskusage/InfoWindow.cpp b/src/apps/diskusage/InfoWindow.cpp index 619c206476..9c46410a8a 100644 --- a/src/apps/diskusage/InfoWindow.cpp +++ b/src/apps/diskusage/InfoWindow.cpp @@ -77,12 +77,13 @@ InfoWin::InfoWin(BPoint p, FileInfo *f, BWindow* parent) // Size char name[B_PATH_NAME_LENGTH] = { 0 }; - size_to_string(f->size, name); + size_to_string(f->size, name, sizeof(name)); if (f->count > 0) { // This is a directory. char str[64]; - sprintf(str, B_TRANSLATE(" in %d files"), f->count); - strcat(name, str); + snprintf(str, sizeof(str), B_TRANSLATE(" in %d files"), + f->count); + strlcat(name, str, sizeof(name)); } info.push_back(Item(B_TRANSLATE_MARK("Size"), name)); diff --git a/src/apps/diskusage/PieView.cpp b/src/apps/diskusage/PieView.cpp index 9052420d65..6d88918170 100644 --- a/src/apps/diskusage/PieView.cpp +++ b/src/apps/diskusage/PieView.cpp @@ -205,6 +205,7 @@ PieView::MessageReceived(BMessage* message) fOutdated = true; Invalidate(); } + break; } default: @@ -494,7 +495,7 @@ PieView::_DrawDirectory(BRect b, FileInfo* info, float parentSpan, // Show total volume capacity. char label[B_PATH_NAME_LENGTH]; - size_to_string(volCapacity, label); + size_to_string(volCapacity, label, sizeof(label)); SetHighColor(kPieBGColor); SetDrawingMode(B_OP_OVER); DrawString(label, BPoint(cx - StringWidth(label) / 2.0, diff --git a/src/apps/diskusage/Scanner.cpp b/src/apps/diskusage/Scanner.cpp index c1e15e0520..b2e6e1b3ca 100644 --- a/src/apps/diskusage/Scanner.cpp +++ b/src/apps/diskusage/Scanner.cpp @@ -102,7 +102,7 @@ Scanner::MessageReceived(BMessage* message) BMessage msg(kOutdatedMsg); fListener.SendMessage(&msg); } - + break; } default: diff --git a/src/apps/diskusage/Snapshot.cpp b/src/apps/diskusage/Snapshot.cpp index 419b012e1d..bef48b9b79 100644 --- a/src/apps/diskusage/Snapshot.cpp +++ b/src/apps/diskusage/Snapshot.cpp @@ -79,7 +79,7 @@ FileInfo::Type() const char mimeStr[B_MIME_TYPE_LENGTH] = { '\0' }; if (parent == NULL) { // This is the volume's root directory; treat it as a volume type. - strcpy(mimeStr, kVolumeType); + strlcpy(mimeStr, kVolumeType, sizeof(mimeStr)); } else { // Get the MIME type from the registrar. BNode node(&ref); @@ -93,14 +93,14 @@ FileInfo::Type() const // BFS volumes (e.g., CDFS volumes return B_BAD_VALUE). //nodeInfo.SetType(kDirType); } - strcpy(mimeStr, kDirType); + strlcpy(mimeStr, kDirType, sizeof(mimeStr)); } } } } if (strlen(mimeStr) == 0) - strcpy(mimeStr, kFileType); + strlcpy(mimeStr, kFileType, sizeof(mimeStr)); return new BMimeType(mimeStr); } diff --git a/src/apps/diskusage/StatusView.cpp b/src/apps/diskusage/StatusView.cpp index 421bf4bcc7..9276695d79 100644 --- a/src/apps/diskusage/StatusView.cpp +++ b/src/apps/diskusage/StatusView.cpp @@ -44,16 +44,19 @@ StatusView::StatusView() B_SIZE_UNSET)); char testLabel[256]; - sprintf(testLabel, B_TRANSLATE("%d files"), 999999); + snprintf(testLabel, sizeof(testLabel), B_TRANSLATE("%d files"), + 999999); fCountView = new BStringView(NULL, kEmptyStr); - fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel), B_SIZE_UNSET)); + fCountView->SetExplicitMinSize(BSize(StringWidth(testLabel), + B_SIZE_UNSET)); fCountView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); fPathView = new BStringView(NULL, kEmptyStr); fPathView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); - fRefreshBtn = new BButton(NULL, B_TRANSLATE("Scan"), new BMessage(kBtnRescan)); + fRefreshBtn = new BButton(NULL, B_TRANSLATE("Scan"), + new BMessage(kBtnRescan)); fRefreshBtn->SetExplicitMaxSize(BSize(B_SIZE_UNSET, B_SIZE_UNLIMITED)); @@ -137,13 +140,13 @@ StatusView::ShowInfo(const FileInfo* info) fPathView->SetText(pathLabel.String()); char label[B_PATH_NAME_LENGTH]; - size_to_string(info->size, label); + size_to_string(info->size, label, sizeof(label)); fSizeView->SetText(label); if (info->count > 0) { char label[256]; - sprintf(label, (info->count == 1) ? B_TRANSLATE("%d file") : - B_TRANSLATE("%d files"), info->count); + snprintf(label, sizeof(label), (info->count == 1) ? + B_TRANSLATE("%d file") : B_TRANSLATE("%d files"), info->count); fCountView->SetText(label); } else { fCountView->SetText(kEmptyStr); diff --git a/src/apps/diskusage/VolumeView.cpp b/src/apps/diskusage/VolumeView.cpp index eb504b1e78..2bd3838a91 100644 --- a/src/apps/diskusage/VolumeView.cpp +++ b/src/apps/diskusage/VolumeView.cpp @@ -69,7 +69,6 @@ VolumeView::SetPath(BPath path) void VolumeView::MessageReceived(BMessage* msg) { - msg->PrintToStream(); switch(msg->what) { case kBtnRescan: fPieView->MessageReceived(msg);