riched20: Add checks for allocation failures.

Signed-off-by: Huw Davies <huw@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Huw Davies 2016-09-29 10:13:12 +01:00 committed by Alexandre Julliard
parent b3b5c25628
commit 1acc6dcc58

View File

@ -24,19 +24,23 @@ WINE_DEFAULT_DEBUG_CHANNEL(richedit);
static int ME_GetOptimalBuffer(int nLen)
{
/* FIXME: This seems wasteful for tabs and end of lines strings,
* since they have a small fixed length. */
return ((sizeof(WCHAR) * nLen) + 128) & ~63;
}
/* Create a buffer (uninitialized string) of size nMaxChars */
static ME_String *ME_MakeStringB(int nMaxChars)
{
ME_String *s = ALLOC_OBJ(ME_String);
ME_String *s = heap_alloc( sizeof(*s) );
if (!s) return NULL;
s->nLen = nMaxChars;
s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
s->szData = heap_alloc( s->nBuffer * sizeof(WCHAR) );
if (!s->szData)
{
heap_free( s );
return NULL;
}
s->szData[s->nLen] = 0;
return s;
}
@ -44,7 +48,8 @@ static ME_String *ME_MakeStringB(int nMaxChars)
ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
{
ME_String *s = ME_MakeStringB(nMaxChars);
/* Native allows NULL chars */
if (!s) return NULL;
memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
return s;
}
@ -54,6 +59,8 @@ ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
{
int i;
ME_String *s = ME_MakeStringB(nMaxChars);
if (!s) return NULL;
for (i = 0; i < nMaxChars; i++)
s->szData[i] = cRepeat;
return s;
@ -62,20 +69,23 @@ ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
void ME_DestroyString(ME_String *s)
{
if (!s) return;
FREE_OBJ(s->szData);
FREE_OBJ(s);
heap_free( s->szData );
heap_free( s );
}
BOOL ME_InsertString(ME_String *s, int ofs, const WCHAR *insert, int len)
{
DWORD new_len = s->nLen + len + 1;
WCHAR *new;
assert( ofs <= s->nLen );
if( new_len > s->nBuffer )
{
s->nBuffer = ME_GetOptimalBuffer( new_len );
s->szData = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
if (!s->szData) return FALSE;
new = heap_realloc( s->szData, s->nBuffer * sizeof(WCHAR) );
if (!new) return FALSE;
s->szData = new;
}
memmove( s->szData + ofs + len, s->szData + ofs, (s->nLen - ofs + 1) * sizeof(WCHAR) );
@ -101,6 +111,8 @@ ME_String *ME_VSplitString(ME_String *orig, int charidx)
assert(charidx<=orig->nLen);
s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
if (!s) return NULL;
orig->nLen = charidx;
orig->szData[charidx] = '\0';
return s;
@ -163,6 +175,7 @@ ME_CallWordBreakProc(ME_TextEditor *editor, WCHAR *str, INT len, INT start, INT
int buffer_size = WideCharToMultiByte(CP_ACP, 0, str, len,
NULL, 0, NULL, NULL);
char *buffer = heap_alloc(buffer_size);
if (!buffer) return 0;
WideCharToMultiByte(CP_ACP, 0, str, len,
buffer, buffer_size, NULL, NULL);
result = editor->pfnWordBreak((WCHAR*)buffer, start, buffer_size, code);
@ -187,7 +200,7 @@ LPWSTR ME_ToUnicode(LONG codepage, LPVOID psz, INT *len)
if(!nChars) return NULL;
if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
if((tmp = heap_alloc( nChars * sizeof(WCHAR) )) != NULL)
*len = MultiByteToWideChar(codepage, 0, psz, -1, tmp, nChars) - 1;
return tmp;
}
@ -196,5 +209,5 @@ LPWSTR ME_ToUnicode(LONG codepage, LPVOID psz, INT *len)
void ME_EndToUnicode(LONG codepage, LPVOID psz)
{
if (codepage != CP_UNICODE)
FREE_OBJ(psz);
heap_free( psz );
}