Attachment #818052: Use CheckedInt to check for overflow for bug #922600

View | Details | Raw Unified | Return to bug 922600
Collapse All | Expand All

(-)a/gfx/thebes/gfxXlibSurface.cpp (-2 / +15 lines)
Line     Link Here 
 Lines 12-27    Link Here 
12
#undef max // Xlibint.h defines this and it breaks std::max
12
#undef max // Xlibint.h defines this and it breaks std::max
13
#undef min // Xlibint.h defines this and it breaks std::min
13
#undef min // Xlibint.h defines this and it breaks std::min
14
14
15
#include "nsAutoPtr.h"
15
#include "nsAutoPtr.h"
16
#include "nsTArray.h"
16
#include "nsTArray.h"
17
#include "nsAlgorithm.h"
17
#include "nsAlgorithm.h"
18
#include "mozilla/Preferences.h"
18
#include "mozilla/Preferences.h"
19
#include <algorithm>
19
#include <algorithm>
20
#include "mozilla/CheckedInt.h"
20
21
21
using namespace mozilla;
22
using namespace mozilla;
22
23
23
// Although the dimension parameters in the xCreatePixmapReq wire protocol are
24
// Although the dimension parameters in the xCreatePixmapReq wire protocol are
24
// 16-bit unsigned integers, the server's CreatePixmap returns BadAlloc if
25
// 16-bit unsigned integers, the server's CreatePixmap returns BadAlloc if
25
// either dimension cannot be represented by a 16-bit *signed* integer.
26
// either dimension cannot be represented by a 16-bit *signed* integer.
26
#define XLIB_IMAGE_SIDE_SIZE_LIMIT 0x7fff
27
#define XLIB_IMAGE_SIDE_SIZE_LIMIT 0x7fff
27
28
 Lines 116-135   CreatePixmap(Screen *screen, const gfxIn Link Here 
116
}
117
}
117
118
118
void
119
void
119
gfxXlibSurface::TakePixmap()
120
gfxXlibSurface::TakePixmap()
120
{
121
{
121
    NS_ASSERTION(!mPixmapTaken, "I already own the Pixmap!");
122
    NS_ASSERTION(!mPixmapTaken, "I already own the Pixmap!");
122
    mPixmapTaken = true;
123
    mPixmapTaken = true;
123
124
125
    // The bit depth returned from Cairo is technically int, but this is
126
    // the last place we'd be worried about that scenario.
127
    unsigned int bitDepth = cairo_xlib_surface_get_depth(CairoSurface());
128
    MOZ_ASSERT((bitDepth % 8) == 0, "Memory used not recorded correctly");
129
124
    // Divide by 8 because surface_get_depth gives us the number of *bits* per
130
    // Divide by 8 because surface_get_depth gives us the number of *bits* per
125
    // pixel.
131
    // pixel.
126
    RecordMemoryUsed(mSize.width * mSize.height *
132
    CheckedInt32 totalBytes = CheckedInt32(mSize.width) * CheckedInt32(mSize.height) * (bitDepth/8);
127
        cairo_xlib_surface_get_depth(CairoSurface()) / 8);
133
134
    // Don't do anything in the "else" case.  We could add INT32_MAX, but that
135
    // would overflow the memory used counter.  It would also mean we tried for
136
    // a 2G image.  For now, we'll just assert,
137
    MOZ_ASSERT(totalBytes.isValid(),"Did not expect to exceed 2Gb image");
138
    if (totalBytes.isValid()) {
139
        RecordMemoryUsed(totalBytes.value());
140
    }
128
}
141
}
129
142
130
Drawable
143
Drawable
131
gfxXlibSurface::ReleasePixmap() {
144
gfxXlibSurface::ReleasePixmap() {
132
    NS_ASSERTION(mPixmapTaken, "I don't own the Pixmap!");
145
    NS_ASSERTION(mPixmapTaken, "I don't own the Pixmap!");
133
    mPixmapTaken = false;
146
    mPixmapTaken = false;
134
    RecordMemoryFreed();
147
    RecordMemoryFreed();
135
    return mDrawable;
148
    return mDrawable;

Return to bug 922600