Replacing malloc calls by xmalloc or xzalloc as appropriate.

This will make sure we get an appropriate error message in case of memory allocation error, and not just a "Segmentation Fault"

Added some validation on xzalloc return in serial_tty_new to properly deallocate resources when an error occurs.
This commit is contained in:
Julien Ropé 2012-05-07 15:24:07 +02:00
parent f142ac29d6
commit b2177dc8c3
12 changed files with 72 additions and 53 deletions

View File

@ -434,12 +434,20 @@ boolean serial_tty_write(SERIAL_TTY* tty, uint8* buffer, uint32 Length)
return true;
}
/**
* This function is used to deallocated a SERIAL_TTY structure.
*
* @param tty [in] - pointer to the SERIAL_TTY structure to deallocate.
* This will typically be allocated by a call to serial_tty_new().
* On return, this pointer is invalid.
*/
void serial_tty_free(SERIAL_TTY* tty)
{
DEBUG_SVC("in");
if (tty->fd >= 0)
{
if (tty->pold_termios)
tcsetattr(tty->fd, TCSANOW, tty->pold_termios);
close(tty->fd);
}
@ -449,6 +457,7 @@ void serial_tty_free(SERIAL_TTY* tty)
xfree(tty);
}
SERIAL_TTY* serial_tty_new(const char* path, uint32 id)
{
SERIAL_TTY* tty;
@ -460,21 +469,32 @@ SERIAL_TTY* serial_tty_new(const char* path, uint32 id)
{
perror("open");
DEBUG_WARN("failed to open device %s", path);
serial_tty_free(tty) ;
return NULL;
}
else
DEBUG_SVC("tty fd %d successfully opened", tty->fd);
tty->ptermios = (struct termios*) malloc(sizeof(struct termios));
memset(tty->ptermios, 0, sizeof(struct termios));
tty->pold_termios = (struct termios*) malloc(sizeof(struct termios));
memset(tty->pold_termios, 0, sizeof(struct termios));
tty->ptermios = (struct termios*) xzalloc(sizeof(struct termios));
if (tty->ptermios == NULL)
{
serial_tty_free(tty) ;
return NULL ;
}
tty->pold_termios = (struct termios*) xzalloc(sizeof(struct termios));
if (tty->pold_termios == NULL)
{
serial_tty_free(tty) ;
return NULL ;
}
tcgetattr(tty->fd, tty->pold_termios);
if (!tty_get_termios(tty))
{
DEBUG_WARN("%s access denied", path);
fflush(stdout);
serial_tty_free(tty) ;
return NULL;
}
@ -497,6 +517,7 @@ SERIAL_TTY* serial_tty_new(const char* path, uint32 id)
{
DEBUG_WARN("%s fcntl", path);
perror("fcntl");
serial_tty_free(tty) ;
return NULL;
}

View File

@ -92,7 +92,7 @@ uint8* wf_glyph_convert(wfInfo* wfi, int width, int height, uint8* data)
src_bytes_per_row = (width + 7) / 8;
dst_bytes_per_row = src_bytes_per_row + (src_bytes_per_row % 2);
cdata = (uint8 *) malloc(dst_bytes_per_row * height);
cdata = (uint8 *) xmalloc(dst_bytes_per_row * height);
src = data;
for (indexy = 0; indexy < height; indexy++)

View File

@ -161,8 +161,7 @@ void xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
ci.height = pointer->height;
ci.xhot = pointer->xPos;
ci.yhot = pointer->yPos;
ci.pixels = (XcursorPixel*) malloc(ci.width * ci.height * 4);
memset(ci.pixels, 0, ci.width * ci.height * 4);
ci.pixels = (XcursorPixel*) xzalloc(ci.width * ci.height * 4);
if ((pointer->andMaskData != 0) && (pointer->xorMaskData != 0))
{

View File

@ -364,7 +364,7 @@ uint8* freerdp_image_convert_8bpp(uint8* srcData, uint8* dstData, int width, int
if (dstBpp == 8)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height);
dstData = (uint8*) xmalloc(width * height);
memcpy(dstData, srcData, width * height);
return dstData;
@ -372,7 +372,7 @@ uint8* freerdp_image_convert_8bpp(uint8* srcData, uint8* dstData, int width, int
else if (dstBpp == 15 || (dstBpp == 16 && clrconv->rgb555))
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
dst16 = (uint16 *) dstData;
for (i = width * height; i > 0; i--)
@ -391,7 +391,7 @@ uint8* freerdp_image_convert_8bpp(uint8* srcData, uint8* dstData, int width, int
else if (dstBpp == 16)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
dst16 = (uint16 *) dstData;
for (i = width * height; i > 0; i--)
@ -410,7 +410,7 @@ uint8* freerdp_image_convert_8bpp(uint8* srcData, uint8* dstData, int width, int
else if (dstBpp == 32)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
src8 = (uint8*) srcData;
dst32 = (uint32*) dstData;
@ -445,7 +445,7 @@ uint8* freerdp_image_convert_15bpp(uint8* srcData, uint8* dstData, int width, in
if (dstBpp == 15 || (dstBpp == 16 && clrconv->rgb555))
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
memcpy(dstData, srcData, width * height * 2);
@ -454,7 +454,7 @@ uint8* freerdp_image_convert_15bpp(uint8* srcData, uint8* dstData, int width, in
else if (dstBpp == 32)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
src16 = (uint16 *) srcData;
dst32 = (uint32 *) dstData;
@ -472,7 +472,7 @@ uint8* freerdp_image_convert_15bpp(uint8* srcData, uint8* dstData, int width, in
else if (dstBpp == 16)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
src16 = (uint16 *) srcData;
dst16 = (uint16 *) dstData;
@ -500,7 +500,7 @@ uint8* freerdp_image_convert_16bpp(uint8* srcData, uint8* dstData, int width, in
if (dstBpp == 16)
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
if (clrconv->rgb555)
{
@ -533,7 +533,7 @@ uint8* freerdp_image_convert_16bpp(uint8* srcData, uint8* dstData, int width, in
uint8 red, green, blue;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 3);
dstData = (uint8*) xmalloc(width * height * 3);
dst8 = (uint8*) dstData;
src16 = (uint16*) srcData;
@ -567,7 +567,7 @@ uint8* freerdp_image_convert_16bpp(uint8* srcData, uint8* dstData, int width, in
uint8 red, green, blue;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
src16 = (uint16*) srcData;
dst32 = (uint32*) dstData;
@ -595,7 +595,7 @@ uint8* freerdp_image_convert_24bpp(uint8* srcData, uint8* dstData, int width, in
{
uint8 *dstp;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
dstp = dstData;
for (i = width * height; i > 0; i--)
@ -621,7 +621,7 @@ uint8* freerdp_image_convert_32bpp(uint8* srcData, uint8* dstData, int width, in
uint8 red, green, blue;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
dst16 = (uint16*) dstData;
src32 = (uint32*) srcData;
@ -642,7 +642,7 @@ uint8* freerdp_image_convert_32bpp(uint8* srcData, uint8* dstData, int width, in
uint8 red, green, blue;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 3);
dstData = (uint8*) xmalloc(width * height * 3);
dstp = dstData;
for (index = 0; index < width * height; index++)
@ -676,7 +676,7 @@ uint8* freerdp_image_convert_32bpp(uint8* srcData, uint8* dstData, int width, in
uint8 *dstp;
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
memcpy(dstData, srcData, width * height * 4);
@ -694,7 +694,7 @@ uint8* freerdp_image_convert_32bpp(uint8* srcData, uint8* dstData, int width, in
else
{
if (dstData == NULL)
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
memcpy(dstData, srcData, width * height * 4);
}
@ -846,7 +846,7 @@ uint8* freerdp_glyph_convert(int width, int height, uint8* data)
*/
scanline = (width + 7) / 8;
dstData = (uint8*) malloc(width * height);
dstData = (uint8*) xmalloc(width * height);
memset(dstData, 0, width * height);
dstp = dstData;
@ -930,7 +930,7 @@ uint8* freerdp_mono_image_convert(uint8* srcData, int width, int height, int src
}
}
dstData = (uint8*) malloc(width * height * 2);
dstData = (uint8*) xmalloc(width * height * 2);
dst16 = (uint16*) dstData;
for (index = height; index > 0; index--)
@ -955,7 +955,7 @@ uint8* freerdp_mono_image_convert(uint8* srcData, int width, int height, int src
}
else if (dstBpp == 32)
{
dstData = (uint8*) malloc(width * height * 4);
dstData = (uint8*) xmalloc(width * height * 4);
dst32 = (uint32*) dstData;
for (index = height; index > 0; index--)

View File

@ -398,8 +398,8 @@ char** crypto_cert_subject_alt_name(X509* xcert, int* count, int** lengths)
return NULL;
num_subject_alt_names = sk_GENERAL_NAME_num(subject_alt_names);
strings = (char**) malloc(sizeof(char*) * num_subject_alt_names);
*lengths = (int*) malloc(sizeof(int*) * num_subject_alt_names);
strings = (char**) xmalloc(sizeof(char*) * num_subject_alt_names);
*lengths = (int*) xmalloc(sizeof(int*) * num_subject_alt_names);
for (index = 0; index < num_subject_alt_names; ++index)
{

View File

@ -130,7 +130,7 @@ INLINE void gdi_SetPixel_32bpp(HGDI_BITMAP hBmp, int X, int Y, uint32 pixel)
HGDI_BITMAP gdi_CreateBitmap(int nWidth, int nHeight, int cBitsPerPixel, uint8* data)
{
HGDI_BITMAP hBitmap = (HGDI_BITMAP) malloc(sizeof(GDI_BITMAP));
HGDI_BITMAP hBitmap = (HGDI_BITMAP) xmalloc(sizeof(GDI_BITMAP));
hBitmap->objectType = GDIOBJECT_BITMAP;
hBitmap->bitsPerPixel = cBitsPerPixel;
hBitmap->bytesPerPixel = (cBitsPerPixel + 1) / 8;
@ -152,13 +152,13 @@ HGDI_BITMAP gdi_CreateBitmap(int nWidth, int nHeight, int cBitsPerPixel, uint8*
HGDI_BITMAP gdi_CreateCompatibleBitmap(HGDI_DC hdc, int nWidth, int nHeight)
{
HGDI_BITMAP hBitmap = (HGDI_BITMAP) malloc(sizeof(GDI_BITMAP));
HGDI_BITMAP hBitmap = (HGDI_BITMAP) xmalloc(sizeof(GDI_BITMAP));
hBitmap->objectType = GDIOBJECT_BITMAP;
hBitmap->bytesPerPixel = hdc->bytesPerPixel;
hBitmap->bitsPerPixel = hdc->bitsPerPixel;
hBitmap->width = nWidth;
hBitmap->height = nHeight;
hBitmap->data = malloc(nWidth * nHeight * hBitmap->bytesPerPixel);
hBitmap->data = xmalloc(nWidth * nHeight * hBitmap->bytesPerPixel);
hBitmap->scanline = nWidth * hBitmap->bytesPerPixel;
return hBitmap;
}

View File

@ -50,7 +50,7 @@ p_PatBlt PatBlt_[5] =
HGDI_BRUSH gdi_CreateSolidBrush(GDI_COLOR crColor)
{
HGDI_BRUSH hBrush = (HGDI_BRUSH) malloc(sizeof(GDI_BRUSH));
HGDI_BRUSH hBrush = (HGDI_BRUSH) xmalloc(sizeof(GDI_BRUSH));
hBrush->objectType = GDIOBJECT_BRUSH;
hBrush->style = GDI_BS_SOLID;
hBrush->color = crColor;
@ -66,7 +66,7 @@ HGDI_BRUSH gdi_CreateSolidBrush(GDI_COLOR crColor)
HGDI_BRUSH gdi_CreatePatternBrush(HGDI_BITMAP hbmp)
{
HGDI_BRUSH hBrush = (HGDI_BRUSH) malloc(sizeof(GDI_BRUSH));
HGDI_BRUSH hBrush = (HGDI_BRUSH) xmalloc(sizeof(GDI_BRUSH));
hBrush->objectType = GDIOBJECT_BRUSH;
hBrush->style = GDI_BS_PATTERN;
hBrush->pattern = hbmp;

View File

@ -38,7 +38,7 @@
HGDI_DC gdi_GetDC()
{
HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
HGDI_DC hDC = (HGDI_DC) xmalloc(sizeof(GDI_DC));
hDC->bytesPerPixel = 4;
hDC->bitsPerPixel = 32;
hDC->drawMode = GDI_R2_BLACK;
@ -56,7 +56,7 @@ HGDI_DC gdi_GetDC()
HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp)
{
HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
HGDI_DC hDC = (HGDI_DC) xmalloc(sizeof(GDI_DC));
hDC->drawMode = GDI_R2_BLACK;
hDC->clip = gdi_CreateRectRgn(0, 0, 0, 0);
@ -70,12 +70,12 @@ HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp)
hDC->invert = clrconv->invert;
hDC->rgb555 = clrconv->rgb555;
hDC->hwnd = (HGDI_WND) malloc(sizeof(GDI_WND));
hDC->hwnd = (HGDI_WND) xmalloc(sizeof(GDI_WND));
hDC->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
hDC->hwnd->invalid->null = 1;
hDC->hwnd->count = 32;
hDC->hwnd->cinvalid = (HGDI_RGN) malloc(sizeof(GDI_RGN) * hDC->hwnd->count);
hDC->hwnd->cinvalid = (HGDI_RGN) xmalloc(sizeof(GDI_RGN) * hDC->hwnd->count);
hDC->hwnd->ninvalid = 0;
return hDC;
@ -90,7 +90,7 @@ HGDI_DC gdi_CreateDC(HCLRCONV clrconv, int bpp)
HGDI_DC gdi_CreateCompatibleDC(HGDI_DC hdc)
{
HGDI_DC hDC = (HGDI_DC) malloc(sizeof(GDI_DC));
HGDI_DC hDC = (HGDI_DC) xmalloc(sizeof(GDI_DC));
hDC->bytesPerPixel = hdc->bytesPerPixel;
hDC->bitsPerPixel = hdc->bitsPerPixel;
hDC->drawMode = hdc->drawMode;

View File

@ -371,7 +371,7 @@ gdiBitmap* gdi_glyph_new(rdpGdi* gdi, GLYPH_DATA* glyph)
uint8* extra;
gdiBitmap* gdi_bmp;
gdi_bmp = (gdiBitmap*) malloc(sizeof(gdiBitmap));
gdi_bmp = (gdiBitmap*) xmalloc(sizeof(gdiBitmap));
gdi_bmp->hdc = gdi_GetDC();
gdi_bmp->hdc->bytesPerPixel = 1;
@ -403,7 +403,7 @@ gdiBitmap* gdi_bitmap_new_ex(rdpGdi* gdi, int width, int height, int bpp, uint8*
{
gdiBitmap* bitmap;
bitmap = (gdiBitmap*) malloc(sizeof(gdiBitmap));
bitmap = (gdiBitmap*) xmalloc(sizeof(gdiBitmap));
bitmap->hdc = gdi_CreateCompatibleDC(gdi->hdc);
DEBUG_GDI("gdi_bitmap_new: width:%d height:%d bpp:%d", width, height, bpp);
@ -838,12 +838,12 @@ void gdi_init_primary(rdpGdi* gdi)
if (gdi->drawing == NULL)
gdi->drawing = gdi->primary;
gdi->primary->hdc->hwnd = (HGDI_WND) malloc(sizeof(GDI_WND));
gdi->primary->hdc->hwnd = (HGDI_WND) xmalloc(sizeof(GDI_WND));
gdi->primary->hdc->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
gdi->primary->hdc->hwnd->invalid->null = 1;
gdi->primary->hdc->hwnd->count = 32;
gdi->primary->hdc->hwnd->cinvalid = (HGDI_RGN) malloc(sizeof(GDI_RGN) * gdi->primary->hdc->hwnd->count);
gdi->primary->hdc->hwnd->cinvalid = (HGDI_RGN) xmalloc(sizeof(GDI_RGN) * gdi->primary->hdc->hwnd->count);
gdi->primary->hdc->hwnd->ninvalid = 0;
}
@ -875,8 +875,7 @@ int gdi_init(freerdp* instance, uint32 flags, uint8* buffer)
rdpGdi* gdi;
rdpCache* cache;
gdi = (rdpGdi*) malloc(sizeof(rdpGdi));
memset(gdi, 0, sizeof(rdpGdi));
gdi = (rdpGdi*) xzalloc(sizeof(rdpGdi));
instance->context->gdi = gdi;
cache = instance->context->cache;
@ -926,11 +925,11 @@ int gdi_init(freerdp* instance, uint32 flags, uint8* buffer)
gdi->hdc->bitsPerPixel = gdi->dstBpp;
gdi->hdc->bytesPerPixel = gdi->bytesPerPixel;
gdi->clrconv = (HCLRCONV) malloc(sizeof(CLRCONV));
gdi->clrconv = (HCLRCONV) xmalloc(sizeof(CLRCONV));
gdi->clrconv->alpha = (flags & CLRCONV_ALPHA) ? 1 : 0;
gdi->clrconv->invert = (flags & CLRCONV_INVERT) ? 1 : 0;
gdi->clrconv->rgb555 = (flags & CLRCONV_RGB555) ? 1 : 0;
gdi->clrconv->palette = (rdpPalette*) malloc(sizeof(rdpPalette));
gdi->clrconv->palette = (rdpPalette*) xmalloc(sizeof(rdpPalette));
gdi->hdc->alpha = gdi->clrconv->alpha;
gdi->hdc->invert = gdi->clrconv->invert;

View File

@ -66,9 +66,9 @@ static const GDI_PALETTEENTRY default_system_palette[20] =
HGDI_PALETTE gdi_CreatePalette(HGDI_PALETTE palette)
{
HGDI_PALETTE hPalette = (HGDI_PALETTE) malloc(sizeof(GDI_PALETTE));
HGDI_PALETTE hPalette = (HGDI_PALETTE) xmalloc(sizeof(GDI_PALETTE));
hPalette->count = palette->count;
hPalette->entries = (GDI_PALETTEENTRY*) malloc(sizeof(GDI_PALETTEENTRY) * hPalette->count);
hPalette->entries = (GDI_PALETTEENTRY*) xmalloc(sizeof(GDI_PALETTEENTRY) * hPalette->count);
memcpy(hPalette->entries, palette->entries, sizeof(GDI_PALETTEENTRY) * hPalette->count);
return hPalette;
}
@ -80,10 +80,10 @@ HGDI_PALETTE gdi_CreatePalette(HGDI_PALETTE palette)
HGDI_PALETTE CreateSystemPalette()
{
HGDI_PALETTE palette = (HGDI_PALETTE) malloc(sizeof(GDI_PALETTE));
HGDI_PALETTE palette = (HGDI_PALETTE) xmalloc(sizeof(GDI_PALETTE));
palette->count = 256;
palette->entries = (GDI_PALETTEENTRY*) malloc(sizeof(GDI_PALETTEENTRY) * 256);
palette->entries = (GDI_PALETTEENTRY*) xmalloc(sizeof(GDI_PALETTEENTRY) * 256);
memset(palette->entries, 0, sizeof(GDI_PALETTEENTRY) * 256);
memcpy(&palette->entries[0], &default_system_palette[0], 10 * sizeof(GDI_PALETTEENTRY));

View File

@ -40,7 +40,7 @@
HGDI_PEN gdi_CreatePen(int fnPenStyle, int nWidth, int crColor)
{
HGDI_PEN hPen = (HGDI_PEN) malloc(sizeof(GDI_PEN));
HGDI_PEN hPen = (HGDI_PEN) xmalloc(sizeof(GDI_PEN));
hPen->objectType = GDIOBJECT_PEN;
hPen->style = fnPenStyle;
hPen->color = crColor;

View File

@ -39,7 +39,7 @@
HGDI_RGN gdi_CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
{
HGDI_RGN hRgn = (HGDI_RGN) malloc(sizeof(GDI_RGN));
HGDI_RGN hRgn = (HGDI_RGN) xmalloc(sizeof(GDI_RGN));
hRgn->objectType = GDIOBJECT_REGION;
hRgn->x = nLeftRect;
hRgn->y = nTopRect;
@ -60,7 +60,7 @@ HGDI_RGN gdi_CreateRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBot
HGDI_RECT gdi_CreateRect(int xLeft, int yTop, int xRight, int yBottom)
{
HGDI_RECT hRect = (HGDI_RECT) malloc(sizeof(GDI_RECT));
HGDI_RECT hRect = (HGDI_RECT) xmalloc(sizeof(GDI_RECT));
hRect->objectType = GDIOBJECT_RECT;
hRect->left = xLeft;
hRect->top = yTop;