Fix buffer overrun in Linux/x86_64 (Pixel is a 64 bit long, passing

a 32 bit int pointer to XtVaGetValues will cause trouble). Using a long
should suffice. A configure test would be better.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41640 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Mattia Barbon
2006-10-05 19:34:25 +00:00
parent 07a84e7bbb
commit 3e0071d949
15 changed files with 55 additions and 54 deletions

View File

@@ -2767,6 +2767,7 @@ typedef void* WXFontType; /* either a XmFontList or XmRenderTable */
typedef void* WXString; typedef void* WXString;
typedef unsigned long Atom; /* this might fail on a few architectures */ typedef unsigned long Atom; /* this might fail on a few architectures */
typedef long WXPixel; /* safety catch in src/motif/colour.cpp */
#endif /* Motif */ #endif /* Motif */

View File

@@ -41,8 +41,8 @@ public:
unsigned char Green() const { return m_green; } unsigned char Green() const { return m_green; }
unsigned char Blue() const { return m_blue; } unsigned char Blue() const { return m_blue; }
int GetPixel() const { return m_pixel; }; WXPixel GetPixel() const { return m_pixel; };
void SetPixel(int pixel) { m_pixel = pixel; m_isInit = true; }; void SetPixel(WXPixel pixel) { m_pixel = pixel; m_isInit = true; };
inline bool operator == (const wxColour& colour) const { return (m_red == colour.m_red && m_green == colour.m_green && m_blue == colour.m_blue); } inline bool operator == (const wxColour& colour) const { return (m_red == colour.m_red && m_green == colour.m_green && m_blue == colour.m_blue); }
@@ -56,7 +56,7 @@ public:
// TODO: can this handle mono displays? If not, we should have an extra // TODO: can this handle mono displays? If not, we should have an extra
// flag to specify whether this should be black or white by default. // flag to specify whether this should be black or white by default.
int AllocColour(WXDisplay* display, bool realloc = false); WXPixel AllocColour(WXDisplay* display, bool realloc = false);
protected: protected:
// Helper function // Helper function
@@ -72,7 +72,7 @@ private:
unsigned char m_green; unsigned char m_green;
public: public:
int m_pixel; WXPixel m_pixel;
}; };
#endif #endif

View File

@@ -92,11 +92,11 @@ protected:
// if roundToWhite == true then the colour will be set to white unless // if roundToWhite == true then the colour will be set to white unless
// it is RGB 0x000000;if roundToWhite == true the colour wull be set to // it is RGB 0x000000;if roundToWhite == true the colour wull be set to
// black unless it id RGB 0xffffff // black unless it id RGB 0xffffff
int CalculatePixel(wxColour& colour, wxColour& curCol, WXPixel CalculatePixel(wxColour& colour, wxColour& curCol,
bool roundToWhite) const; bool roundToWhite) const;
// sets the foreground pixel taking into account the // sets the foreground pixel taking into account the
// currently selected logical operation // currently selected logical operation
void SetForegroundPixelWithLogicalFunction(int pixel); void SetForegroundPixelWithLogicalFunction(WXPixel pixel);
virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
int style = wxFLOOD_SURFACE); int style = wxFLOOD_SURFACE);
@@ -153,7 +153,7 @@ protected:
WXRegion m_clipRegion; WXRegion m_clipRegion;
// Not sure if we'll need all of these // Not sure if we'll need all of these
int m_backgroundPixel; WXPixel m_backgroundPixel;
wxColour m_currentColour; wxColour m_currentColour;
int m_currentPenWidth ; int m_currentPenWidth ;
int m_currentPenJoin ; int m_currentPenJoin ;

View File

@@ -38,7 +38,7 @@ class WXDLLIMPEXP_CORE wxRegion;
extern int wxCharCodeXToWX(KeySym keySym); extern int wxCharCodeXToWX(KeySym keySym);
extern KeySym wxCharCodeWXToX(int id); extern KeySym wxCharCodeWXToX(int id);
int wxGetBestMatchingPixel(Display *display, XColor *desiredColor, Colormap cmap); WXPixel wxGetBestMatchingPixel(Display *display, XColor *desiredColor, Colormap cmap);
Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap ); Pixmap XCreateInsensitivePixmap( Display *display, Pixmap pixmap );
extern XColor g_itemColors[]; extern XColor g_itemColors[];

View File

@@ -152,7 +152,7 @@ void wxBitmapButton::DoSetBitmap()
// in the current widget background colour. // in the current widget background colour.
if (m_bmpNormalOriginal.GetMask()) if (m_bmpNormalOriginal.GetMask())
{ {
int backgroundPixel; WXPixel backgroundPixel;
XtVaGetValues((Widget) m_mainWidget, XtVaGetValues((Widget) m_mainWidget,
XmNbackground, &backgroundPixel, XmNbackground, &backgroundPixel,
NULL); NULL);
@@ -177,7 +177,7 @@ void wxBitmapButton::DoSetBitmap()
{ {
if (m_bmpDisabledOriginal.GetMask()) if (m_bmpDisabledOriginal.GetMask())
{ {
int backgroundPixel; WXPixel backgroundPixel;
XtVaGetValues((Widget) m_mainWidget, XtVaGetValues((Widget) m_mainWidget,
XmNbackground, &backgroundPixel, XmNbackground, &backgroundPixel,
NULL); NULL);
@@ -202,7 +202,7 @@ void wxBitmapButton::DoSetBitmap()
{ {
if (m_bmpSelectedOriginal.GetMask()) if (m_bmpSelectedOriginal.GetMask())
{ {
int backgroundPixel; WXPixel backgroundPixel;
XtVaGetValues((Widget) m_mainWidget, XtVaGetValues((Widget) m_mainWidget,
XmNarmColor, &backgroundPixel, XmNarmColor, &backgroundPixel,
NULL); NULL);

View File

@@ -125,7 +125,7 @@ WXPixmap wxBitmapCache::GetPixmapFromCache(WXWidget w)
while( XmIsGadget( widget ) ) while( XmIsGadget( widget ) )
widget = XtParent( widget ); widget = XtParent( widget );
Pixel fg, bg; WXPixel fg, bg;
XtVaGetValues( widget, XtVaGetValues( widget,
XmNbackground, &bg, XmNbackground, &bg,
XmNforeground, &fg, XmNforeground, &fg,

View File

@@ -155,7 +155,7 @@ void wxCheckBox::ChangeBackgroundColour()
NULL); NULL);
wxColour colour = *wxBLACK; wxColour colour = *wxBLACK;
int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
// Better to have the checkbox selection in black, or it's // Better to have the checkbox selection in black, or it's
// hard to determine what state it is in. // hard to determine what state it is in.

View File

@@ -34,14 +34,14 @@
IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject) IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject)
wxCOMPILE_TIME_ASSERT( sizeof(WXPixel) == sizeof(Pixel), PixelSizeIsOk );
// Colour // Colour
void wxColour::Init() void wxColour::Init()
{ {
m_isInit = false; m_isInit = false;
m_red = m_red = m_blue = m_green = 0;
m_blue =
m_green = 0;
m_pixel = -1; m_pixel = -1;
} }
@@ -82,7 +82,7 @@ void wxColour::InitRGBA(unsigned char r, unsigned char g, unsigned char b,
// TODO: can this handle mono displays? If not, we should have an extra // TODO: can this handle mono displays? If not, we should have an extra
// flag to specify whether this should be black or white by default. // flag to specify whether this should be black or white by default.
int wxColour::AllocColour(WXDisplay* display, bool realloc) WXPixel wxColour::AllocColour(WXDisplay* display, bool realloc)
{ {
if ((m_pixel != -1) && !realloc) if ((m_pixel != -1) && !realloc)
return m_pixel; return m_pixel;
@@ -106,7 +106,7 @@ int wxColour::AllocColour(WXDisplay* display, bool realloc)
} }
else else
{ {
m_pixel = (int) color.pixel; m_pixel = (WXPixel) color.pixel;
return m_pixel; return m_pixel;
} }
} }
@@ -141,14 +141,14 @@ A read-only colour will not change.
may give better matching. may give better matching.
-------------------------------------------*/ -------------------------------------------*/
int wxGetBestMatchingPixel(Display *display, XColor *desiredColor, Colormap cmap) WXPixel wxGetBestMatchingPixel(Display *display, XColor *desiredColor, Colormap cmap)
{ {
if (cmap == (Colormap) NULL) if (cmap == (Colormap) NULL)
cmap = (Colormap) wxTheApp->GetMainColormap(display); cmap = (Colormap) wxTheApp->GetMainColormap(display);
int numPixVals = XDisplayCells(display, DefaultScreen (display)); int numPixVals = XDisplayCells(display, DefaultScreen (display));
int mindist = 256 * 256 * 3; int mindist = 256 * 256 * 3;
int bestpixel = (int) BlackPixel (display, DefaultScreen (display)); Pixel bestpixel = BlackPixel (display, DefaultScreen (display));
int red = desiredColor->red >> 8; int red = desiredColor->red >> 8;
int green = desiredColor->green >> 8; int green = desiredColor->green >> 8;
int blue = desiredColor->blue >> 8; int blue = desiredColor->blue >> 8;

View File

@@ -195,7 +195,7 @@ wxWindowDC::wxWindowDC( wxWindow *window )
&gcvalues); &gcvalues);
} }
m_backgroundPixel = (int) gcvalues.background; m_backgroundPixel = gcvalues.background;
SetBackground(wxBrush(m_window->GetBackgroundColour(), wxSOLID)); SetBackground(wxBrush(m_window->GetBackgroundColour(), wxSOLID));
} }
@@ -797,13 +797,13 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest,
// foreground colour. [m_textForegroundColour] Background pixels (0) // foreground colour. [m_textForegroundColour] Background pixels (0)
// will be painted with backgound colour (m_textBackgroundColour) // will be painted with backgound colour (m_textBackgroundColour)
// Using ::SetPen is horribly slow, so avoid doing it // Using ::SetPen is horribly slow, so avoid doing it
int oldBackgroundPixel = -1; WXPixel oldBackgroundPixel = -1;
int oldForegroundPixel = -1; WXPixel oldForegroundPixel = -1;
if (m_textBackgroundColour.Ok()) if (m_textBackgroundColour.Ok())
{ {
oldBackgroundPixel = m_backgroundPixel; oldBackgroundPixel = m_backgroundPixel;
int pixel = m_textBackgroundColour.AllocColour(m_display); WXPixel pixel = m_textBackgroundColour.AllocColour(m_display);
XSetBackground ((Display*) m_display, (GC) m_gc, pixel); XSetBackground ((Display*) m_display, (GC) m_gc, pixel);
if (m_window && m_window->GetBackingPixmap()) if (m_window && m_window->GetBackingPixmap())
@@ -818,7 +818,7 @@ bool wxWindowDC::DoBlit( wxCoord xdest, wxCoord ydest,
CalculatePixel( m_textForegroundColour, CalculatePixel( m_textForegroundColour,
m_textForegroundColour, true); m_textForegroundColour, true);
int pixel = m_textForegroundColour.GetPixel(); WXPixel pixel = m_textForegroundColour.GetPixel();
if (pixel > -1) if (pixel > -1)
SetForegroundPixelWithLogicalFunction(pixel); SetForegroundPixelWithLogicalFunction(pixel);
} }
@@ -1056,7 +1056,7 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
if (!sameColour || !GET_OPTIMIZATION) if (!sameColour || !GET_OPTIMIZATION)
{ {
int pixel = m_textBackgroundColour.AllocColour(m_display); WXPixel pixel = m_textBackgroundColour.AllocColour(m_display);
m_currentColour = m_textBackgroundColour; m_currentColour = m_textBackgroundColour;
// Set the GC to the required colour // Set the GC to the required colour
@@ -1089,7 +1089,7 @@ void wxWindowDC::DoDrawText( const wxString &text, wxCoord x, wxCoord y )
if (!sameColour || !GET_OPTIMIZATION) if (!sameColour || !GET_OPTIMIZATION)
{ {
int pixel = CalculatePixel(m_textForegroundColour, WXPixel pixel = CalculatePixel(m_textForegroundColour,
m_currentColour, false); m_currentColour, false);
// Set the GC to the required colour // Set the GC to the required colour
@@ -1161,10 +1161,10 @@ void wxWindowDC::DoDrawRotatedText( const wxString &text, wxCoord x, wxCoord y,
wxCHECK_RET( Ok(), "invalid dc" ); wxCHECK_RET( Ok(), "invalid dc" );
int oldBackgroundPixel = -1; WXPixel oldBackgroundPixel = -1;
int oldForegroundPixel = -1; WXPixel oldForegroundPixel = -1;
int foregroundPixel = -1; WXPixel foregroundPixel = -1;
int backgroundPixel = -1; WXPixel backgroundPixel = -1;
if (m_textBackgroundColour.Ok()) if (m_textBackgroundColour.Ok())
{ {
@@ -1414,7 +1414,7 @@ void wxWindowDC::SetFont( const wxFont &font )
#endif #endif
} }
void wxWindowDC::SetForegroundPixelWithLogicalFunction(int pixel) void wxWindowDC::SetForegroundPixelWithLogicalFunction(WXPixel pixel)
{ {
if (m_logicalFunction == wxXOR) if (m_logicalFunction == wxXOR)
{ {
@@ -1434,12 +1434,12 @@ void wxWindowDC::SetForegroundPixelWithLogicalFunction(int pixel)
} }
} }
int wxWindowDC::CalculatePixel(wxColour& colour, wxColour& curCol, WXPixel wxWindowDC::CalculatePixel(wxColour& colour, wxColour& curCol,
bool roundToWhite) const bool roundToWhite) const
{ {
const unsigned char wp = (unsigned char)255; const unsigned char wp = (unsigned char)255;
int pixel = -1; WXPixel pixel = -1;
if(!m_colour) // Mono display if(!m_colour) // Mono display
{ {
unsigned char red = colour.Red (); unsigned char red = colour.Red ();
@@ -1451,7 +1451,7 @@ int wxWindowDC::CalculatePixel(wxColour& colour, wxColour& curCol,
((red != 0 || blue != 0 || green != 0) && roundToWhite)) ((red != 0 || blue != 0 || green != 0) && roundToWhite))
{ {
curCol = *wxWHITE; curCol = *wxWHITE;
pixel = (int)WhitePixel((Display*) m_display, pixel = WhitePixel((Display*) m_display,
DefaultScreen((Display*) m_display)); DefaultScreen((Display*) m_display));
curCol.SetPixel(pixel); curCol.SetPixel(pixel);
colour.SetPixel(pixel); colour.SetPixel(pixel);
@@ -1459,7 +1459,7 @@ int wxWindowDC::CalculatePixel(wxColour& colour, wxColour& curCol,
else else
{ {
curCol = *wxBLACK; curCol = *wxBLACK;
pixel = (int)BlackPixel((Display*) m_display, pixel = BlackPixel((Display*) m_display,
DefaultScreen((Display*) m_display)); DefaultScreen((Display*) m_display));
curCol.SetPixel(pixel); curCol.SetPixel(pixel);
colour.SetPixel(pixel); colour.SetPixel(pixel);
@@ -1717,7 +1717,7 @@ void wxWindowDC::SetPen( const wxPen &pen )
if (!sameColour || !GET_OPTIMIZATION if (!sameColour || !GET_OPTIMIZATION
|| ((m_logicalFunction == wxXOR) || (m_autoSetting & 0x2))) || ((m_logicalFunction == wxXOR) || (m_autoSetting & 0x2)))
{ {
int pixel = -1; WXPixel pixel = -1;
if (m_pen.GetStyle () == wxTRANSPARENT) if (m_pen.GetStyle () == wxTRANSPARENT)
pixel = m_backgroundPixel; pixel = m_backgroundPixel;
else else
@@ -1884,7 +1884,7 @@ void wxWindowDC::SetBrush( const wxBrush &brush )
// must test m_logicalFunction, because it involves background! // must test m_logicalFunction, because it involves background!
if (!sameColour || !GET_OPTIMIZATION || m_logicalFunction == wxXOR) if (!sameColour || !GET_OPTIMIZATION || m_logicalFunction == wxXOR)
{ {
int pixel = CalculatePixel(m_brush.GetColour(), m_currentColour, true); WXPixel pixel = CalculatePixel(m_brush.GetColour(), m_currentColour, true);
if (pixel > -1) if (pixel > -1)
SetForegroundPixelWithLogicalFunction(pixel); SetForegroundPixelWithLogicalFunction(pixel);
@@ -2172,7 +2172,7 @@ static void XCopyRemote(Display *src_display, Display *dest_display,
static const int CACHE_SIZE = 256; static const int CACHE_SIZE = 256;
unsigned int i, j; unsigned int i, j;
unsigned long cachesrc[CACHE_SIZE], cachedest[CACHE_SIZE]; Pixel cachesrc[CACHE_SIZE], cachedest[CACHE_SIZE];
int k, cache_pos, all_cache; int k, cache_pos, all_cache;
if (!cache || !*cache) if (!cache || !*cache)
@@ -2190,7 +2190,7 @@ static void XCopyRemote(Display *src_display, Display *dest_display,
for (i = 0; i < w; i++) for (i = 0; i < w; i++)
for (j = 0; j < h; j++) { for (j = 0; j < h; j++) {
unsigned long pixel; Pixel pixel;
XColor xcol; XColor xcol;
pixel = XGetPixel(image, i, j); pixel = XGetPixel(image, i, j);

View File

@@ -52,7 +52,7 @@ wxMemoryDC::wxMemoryDC(void)
GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode, GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
&gcvalues); &gcvalues);
m_backgroundPixel = (int) gcvalues.background; m_backgroundPixel = gcvalues.background;
SetBrush (* wxWHITE_BRUSH); SetBrush (* wxWHITE_BRUSH);
SetPen (* wxBLACK_PEN); SetPen (* wxBLACK_PEN);
@@ -79,7 +79,7 @@ wxMemoryDC::wxMemoryDC( wxDC* dc )
GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode, GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
&gcvalues); &gcvalues);
m_backgroundPixel = (int) gcvalues.background; m_backgroundPixel = gcvalues.background;
SetBrush (* wxWHITE_BRUSH); SetBrush (* wxWHITE_BRUSH);
SetPen (* wxBLACK_PEN); SetPen (* wxBLACK_PEN);
@@ -112,7 +112,7 @@ void wxMemoryDC::SelectObject( const wxBitmap& bitmap )
GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode, GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
&gcvalues); &gcvalues);
m_backgroundPixel = (int) gcvalues.background; m_backgroundPixel = gcvalues.background;
m_ok = true; m_ok = true;
SetBrush (* wxWHITE_BRUSH); SetBrush (* wxWHITE_BRUSH);

View File

@@ -61,7 +61,7 @@ wxScreenDC::wxScreenDC()
GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode, GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth | GCSubwindowMode,
&gcvalues); &gcvalues);
m_backgroundPixel = (int) gcvalues.background; m_backgroundPixel = gcvalues.background;
m_ok = true; m_ok = true;
} }

View File

@@ -351,7 +351,7 @@ void wxRadioBox::ChangeBackgroundColour()
wxWindow::ChangeBackgroundColour(); wxWindow::ChangeBackgroundColour();
wxColour colour = *wxBLACK; wxColour colour = *wxBLACK;
int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
for (unsigned int i = 0; i < m_noItems; i++) for (unsigned int i = 0; i < m_noItems; i++)
{ {

View File

@@ -142,7 +142,7 @@ void wxRadioButton::ChangeBackgroundColour()
// What colour should this be? // What colour should this be?
wxColour colour = *wxBLACK; wxColour colour = *wxBLACK;
int selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget)); WXPixel selectPixel = colour.AllocColour(XtDisplay((Widget)m_mainWidget));
XtVaSetValues ((Widget) GetMainWidget(), XtVaSetValues ((Widget) GetMainWidget(),
XmNselectColor, selectPixel, XmNselectColor, selectPixel,

View File

@@ -96,7 +96,7 @@ void wxStaticBitmap::DoSetBitmap()
// in the current widget background colour. // in the current widget background colour.
if (m_messageBitmapOriginal.GetMask()) if (m_messageBitmapOriginal.GetMask())
{ {
int backgroundPixel; WXPixel backgroundPixel;
XtVaGetValues( widget, XmNbackground, &backgroundPixel, XtVaGetValues( widget, XmNbackground, &backgroundPixel,
NULL); NULL);

View File

@@ -379,7 +379,7 @@ bool wxToolBar::Realize()
insensBmp = tool->GetDisabledBitmap(); insensBmp = tool->GetDisabledBitmap();
if ( bmp.GetMask() || insensBmp.GetMask() ) if ( bmp.GetMask() || insensBmp.GetMask() )
{ {
int backgroundPixel; WXPixel backgroundPixel;
XtVaGetValues(button, XmNbackground, &backgroundPixel, XtVaGetValues(button, XmNbackground, &backgroundPixel,
NULL); NULL);
@@ -402,7 +402,7 @@ bool wxToolBar::Realize()
// Create a selected/toggled bitmap. If there isn't a 2nd // Create a selected/toggled bitmap. If there isn't a 2nd
// bitmap, we need to create it (with a darker, selected // bitmap, we need to create it (with a darker, selected
// background) // background)
int backgroundPixel; WXPixel backgroundPixel;
if ( tool->CanBeToggled() ) if ( tool->CanBeToggled() )
XtVaGetValues(button, XmNselectColor, &backgroundPixel, XtVaGetValues(button, XmNselectColor, &backgroundPixel,
NULL); NULL);