Ticket #10396: 0004-Remove-variable-length-arrays-of-non-PODs.patch

File 0004-Remove-variable-length-arrays-of-non-PODs.patch, 9.0 KB (added by js, 6 years ago)
  • src/add-ons/media/media-add-ons/mixer/MixerCore.cpp

    From 7554373ada8b8486cf12625d870175dd7dcfd19d Mon Sep 17 00:00:00 2001
    From: Jonathan Schleifer <js@webkeks.org>
    Date: Fri, 10 Jan 2014 21:06:36 +0100
    Subject: [PATCH 04/10] Remove variable length arrays of non-PODs.
    
    Variable length arrays of non-PODs are not part of the C++ standard, but
    a GNU extension that never worked correctly. Instead, BStackOrHeap array
    is used now, which makes sure that it's not too big for the stack, calls
    all constructors and is valid C++.
    ---
     src/add-ons/media/media-add-ons/mixer/MixerCore.cpp |  6 ++++--
     src/apps/fontdemo/FontDemoView.cpp                  |  5 +++--
     src/apps/icon-o-matic/shape/PathManipulator.cpp     |  3 ++-
     src/apps/soundrecorder/VUView.cpp                   |  3 ++-
     src/apps/terminal/BasicTerminalBuffer.cpp           |  3 ++-
     src/kits/app/ServerLink.cpp                         |  5 +++--
     src/kits/support/ArchivingManagers.cpp              |  4 +++-
     src/servers/app/ServerApp.cpp                       | 15 +++++++--------
     src/servers/app/drawing/DrawingEngine.cpp           |  5 ++++-
     9 files changed, 30 insertions(+), 19 deletions(-)
    
    diff --git a/src/add-ons/media/media-add-ons/mixer/MixerCore.cpp b/src/add-ons/media/media-add-ons/mixer/MixerCore.cpp
    index 4931f45..5af27a6 100644
    a b  
    1616#include <BufferProducer.h>
    1717#include <MediaNode.h>
    1818#include <RealtimeAlloc.h>
     19#include <StackOrHeapArray.h>
    1920#include <StopWatch.h>
    2021#include <TimeSource.h>
    2122
    MixerCore::_MixThread()  
    520521    uint64 bufferIndex = 0;
    521522#endif
    522523
    523     RtList<chan_info> inputChanInfos[MAX_CHANNEL_TYPES];
    524     RtList<chan_info> mixChanInfos[fMixBufferChannelCount];
     524    typedef RtList<chan_info> chan_info_list;
     525    chan_info_list inputChanInfos[MAX_CHANNEL_TYPES];
     526    BStackOrHeapArray<chan_info_list, 16> mixChanInfos(fMixBufferChannelCount);
    525527        // TODO: this does not support changing output channel count
    526528
    527529    bigtime_t eventTime = timeBase;
  • src/apps/fontdemo/FontDemoView.cpp

    diff --git a/src/apps/fontdemo/FontDemoView.cpp b/src/apps/fontdemo/FontDemoView.cpp
    index af4205e..ec1ab54 100644
    a b  
    2020#include <Message.h>
    2121#include <Shape.h>
    2222#include <String.h>
     23#include <StackOrHeapArray.h>
    2324
    2425#include "messages.h"
    2526
    FontDemoView::_DrawView(BView* view)  
    99100    view->SetFont(&fFont, B_FONT_ALL);
    100101
    101102    const size_t size = fString.CountChars();
    102     BRect boundBoxes[size];
     103    BStackOrHeapArray<BRect, 64> boundBoxes(size);
    103104
    104105    if (OutLineLevel())
    105106        fFont.GetGlyphShapes(fString, size, fShapes);
    FontDemoView::_NewBitmap(BRect rect)  
    456457        delete fBitmap;
    457458        fBitmap = NULL;
    458459    }
    459 }
    460  No newline at end of file
     460}
  • src/apps/icon-o-matic/shape/PathManipulator.cpp

    diff --git a/src/apps/icon-o-matic/shape/PathManipulator.cpp b/src/apps/icon-o-matic/shape/PathManipulator.cpp
    index fa30575..4e50c11 100644
    a b  
    1414#include <Message.h>
    1515#include <MenuItem.h>
    1616#include <PopUpMenu.h>
     17#include <StackOrHeapArray.h>
    1718#include <Window.h>
    1819
    1920#include "cursors.h"
    PathManipulator::_Nudge(BPoint direction)  
    17031704        int32 count = fromSelection ? fSelection->CountItems()
    17041705                                    : fPath->CountPoints();
    17051706        int32 indices[count];
    1706         control_point points[count];
     1707        BStackOrHeapArray<control_point, 64> points(count);
    17071708
    17081709        // init indices and points
    17091710        for (int32 i = 0; i < count; i++) {
  • src/apps/soundrecorder/VUView.cpp

    diff --git a/src/apps/soundrecorder/VUView.cpp b/src/apps/soundrecorder/VUView.cpp
    index c90afc5..2f6112b 100644
    a b  
    1111
    1212#include <MediaDefs.h>
    1313#include <Screen.h>
     14#include <StackOrHeapArray.h>
    1415#include <Window.h>
    1516
    1617#include "DrawingTidbits.h"
    VUView::_RenderLaunch(void *data)  
    108109void
    109110VUView::_RenderLoop()
    110111{
    111     rgb_color levels[fLevelCount][2];
     112    BStackOrHeapArray<rgb_color[2], 64> levels(fLevelCount);
    112113   
    113114    for (int32 i = 0; i < fLevelCount; i++) {
    114115        levels[i][0] = levels[i][1] = back_color;   
  • src/apps/terminal/BasicTerminalBuffer.cpp

    diff --git a/src/apps/terminal/BasicTerminalBuffer.cpp b/src/apps/terminal/BasicTerminalBuffer.cpp
    index dfbb54e..0006066 100644
    a b  
    1818
    1919#include <algorithm>
    2020
     21#include <StackOrHeapArray.h>
    2122#include <String.h>
    2223
    2324#include "TermConst.h"
    BasicTerminalBuffer::Find(const char* _pattern, const TermPos& start,  
    537538    int32 patternByteLen = strlen(_pattern);
    538539
    539540    // convert pattern to UTF8Char array
    540     UTF8Char pattern[patternByteLen];
     541    BStackOrHeapArray<UTF8Char, 64> pattern(patternByteLen);
    541542    int32 patternLen = 0;
    542543    while (*_pattern != '\0') {
    543544        int32 charLen = UTF8Char::ByteCount(*_pattern);
  • src/kits/app/ServerLink.cpp

    diff --git a/src/kits/app/ServerLink.cpp b/src/kits/app/ServerLink.cpp
    index ea7664a..05ac105 100644
    a b  
    2525#include <GradientConic.h>
    2626#include <Region.h>
    2727#include <Shape.h>
     28#include <StackOrHeapArray.h>
    2829
    2930#include <ServerProtocol.h>
    3031
    ServerLink::ReadShape(BShape* shape)  
    9697    fReceiver->Read(&opCount, sizeof(int32));
    9798    fReceiver->Read(&ptCount, sizeof(int32));
    9899
    99     uint32 opList[opCount];
     100    BStackOrHeapArray<uint32, 64> opList(opCount);
    100101    if (opCount > 0)
    101102        fReceiver->Read(opList, opCount * sizeof(uint32));
    102103
    103     BPoint ptList[ptCount];
     104    BStackOrHeapArray<BPoint, 64> ptList(ptCount);
    104105    if (ptCount > 0)
    105106        fReceiver->Read(ptList, ptCount * sizeof(BPoint));
    106107
  • src/kits/support/ArchivingManagers.cpp

    diff --git a/src/kits/support/ArchivingManagers.cpp b/src/kits/support/ArchivingManagers.cpp
    index 3405b53..069b339 100644
    a b  
    1111#include <syslog.h>
    1212#include <typeinfo>
    1313
     14#include <StackOrHeapArray.h>
     15
    1416
    1517namespace BPrivate {
    1618namespace Archiving {
    BArchiveManager::ArchiverLeaving(const BArchiver* archiver, status_t err)  
    163165    if (archiver == fCreator && fError == B_OK) {
    164166        // first, we must sort the objects into the order they were archived in
    165167        typedef std::pair<BMessage*, const BArchivable*> ArchivePair;
    166         ArchivePair pairs[fTokenMap.size()];
     168        BStackOrHeapArray<ArchivePair, 64> pairs(fTokenMap.size());
    167169
    168170        for(TokenMap::iterator it = fTokenMap.begin(), end = fTokenMap.end();
    169171                it != end; it++) {
  • src/servers/app/ServerApp.cpp

    diff --git a/src/servers/app/ServerApp.cpp b/src/servers/app/ServerApp.cpp
    index 057df07..8c0122a 100644
    a b  
    3434#include <ScrollBar.h>
    3535#include <Shape.h>
    3636#include <String.h>
     37#include <StackOrHeapArray.h>
    3738
    3839#include <FontPrivate.h>
    3940#include <MessengerPrivate.h>
    ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)  
    18561857                size = 0.0f;
    18571858            }
    18581859
    1859             // TODO: don't use the stack for this - numStrings could be large
    1860             float widthArray[numStrings];
    1861             int32 lengthArray[numStrings];
    1862             char *stringArray[numStrings];
     1860            BStackOrHeapArray<float, 64> widthArray(numStrings);
     1861            BStackOrHeapArray<int32, 64> lengthArray(numStrings);
     1862            BStackOrHeapArray<char*, 64> stringArray(numStrings);
    18631863            for (int32 i = 0; i < numStrings; i++) {
    18641864                // This version of ReadString allocates the strings, we free
    18651865                // them below
    ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)  
    18821882                }
    18831883
    18841884                fLink.StartMessage(B_OK);
    1885                 fLink.Attach(widthArray, sizeof(widthArray));
     1885                fLink.Attach(widthArray, numStrings * sizeof(float));
    18861886            } else
    18871887                fLink.StartMessage(B_BAD_VALUE);
    18881888
    ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)  
    24972497                link.Read<escapement_delta>(&deltaArray[i]);
    24982498            }
    24992499
    2500             // TODO: don't do this on the heap! (at least check the size before)
    2501             BRect rectArray[numStrings];
     2500            BStackOrHeapArray<BRect, 64> rectArray(numStrings);
    25022501
    25032502            ServerFont font;
    25042503            bool success = false;
    ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver& link)  
    25132512                if (font.GetBoundingBoxesForStrings(stringArray, lengthArray,
    25142513                    numStrings, rectArray, mode, deltaArray) == B_OK) {
    25152514                    fLink.StartMessage(B_OK);
    2516                     fLink.Attach(rectArray, sizeof(rectArray));
     2515                    fLink.Attach(rectArray, numStrings * sizeof(BRect));
    25172516                    success = true;
    25182517                }
    25192518            }
  • src/servers/app/drawing/DrawingEngine.cpp

    diff --git a/src/servers/app/drawing/DrawingEngine.cpp b/src/servers/app/drawing/DrawingEngine.cpp
    index 4ec70d7..6ef0b85 100644
    a b  
    1010#include "DrawingEngine.h"
    1111
    1212#include <Bitmap.h>
     13#include <StackOrHeapArray.h>
     14
    1315#include <stdio.h>
     16
    1417#include <algorithm>
    1518#include <stack>
    1619
    DrawingEngine::CopyRegion(/*const*/ BRegion* region, int32 xOffset,  
    469472
    470473    // TODO: make this step unnecessary
    471474    // (by using different stack impl inside node)
    472     node nodes[count];
     475    BStackOrHeapArray<node, 64> nodes(count);
    473476    for (int32 i= 0; i < count; i++) {
    474477        nodes[i].init(region->RectAt(i), count);
    475478    }