use GFX2_malloc() to log allocation errors

also check malloc() return more often
This commit is contained in:
Thomas Bernard 2019-05-04 17:52:34 +02:00
parent 7f78f3ec72
commit c051ac2189
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
8 changed files with 139 additions and 102 deletions

View File

@ -66,6 +66,7 @@ char * Bound_script[10];
#endif #endif
#include "unicode.h" #include "unicode.h"
#include "gfx2mem.h"
/// ///
/// Number of characters for name in fileselector. /// Number of characters for name in fileselector.
@ -2366,7 +2367,9 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
/// @todo as the value doesn't vary, this should be /// @todo as the value doesn't vary, this should be
/// done once at the start of the program /// done once at the start of the program
path = malloc(strlen(Data_directory) + strlen(SCRIPTS_SUBDIRECTORY) + strlen(LUALIB_SUBDIRECTORY) + 5 + 3 * strlen(PATH_SEPARATOR) + 9 + 1); path = GFX2_malloc(strlen(Data_directory) + strlen(SCRIPTS_SUBDIRECTORY) + strlen(LUALIB_SUBDIRECTORY) + 5 + 3 * strlen(PATH_SEPARATOR) + 9 + 1);
if (path == NULL)
return;
strcpy(path, Data_directory); strcpy(path, Data_directory);
Append_path(path, SCRIPTS_SUBDIRECTORY, NULL); Append_path(path, SCRIPTS_SUBDIRECTORY, NULL);
Append_path(path, LUALIB_SUBDIRECTORY, NULL); Append_path(path, LUALIB_SUBDIRECTORY, NULL);
@ -2495,7 +2498,7 @@ void Run_script(const char *script_subdirectory, const char *script_filename)
Original_fore_color=Fore_color; Original_fore_color=Fore_color;
// Backup the brush // Backup the brush
Brush_backup=(byte *)malloc(((long)Brush_height)*Brush_width); Brush_backup=(byte *)GFX2_malloc(((long)Brush_height)*Brush_width);
Brush_backup_width = Brush_width; Brush_backup_width = Brush_width;
Brush_backup_height = Brush_height; Brush_backup_height = Brush_height;

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "gfx2surface.h" #include "gfx2surface.h"
#include "gfx2mem.h"
#include "errors.h" #include "errors.h"
T_GFX2_Surface * New_GFX2_Surface(word width, word height) T_GFX2_Surface * New_GFX2_Surface(word width, word height)
@ -32,10 +33,10 @@ T_GFX2_Surface * New_GFX2_Surface(word width, word height)
if (size == 0) // surfaces with no pixels not allowed if (size == 0) // surfaces with no pixels not allowed
return NULL; return NULL;
surface = malloc(sizeof(T_GFX2_Surface)); surface = GFX2_malloc(sizeof(T_GFX2_Surface));
if (surface == NULL) if (surface == NULL)
return NULL; return NULL;
surface->pixels = malloc(size); surface->pixels = GFX2_malloc(size);
if(surface->pixels == NULL) if(surface->pixels == NULL)
{ {
free(surface); free(surface);

View File

@ -67,6 +67,7 @@
#endif #endif
#endif #endif
#include "gfx2mem.h"
#include "buttons.h" #include "buttons.h"
#include "const.h" #include "const.h"
#include "errors.h" #include "errors.h"
@ -667,20 +668,18 @@ T_Gui_skin * Load_graphics(const char * skin_file, T_Gradient_array *gradients)
return NULL; return NULL;
} }
gfx = (T_Gui_skin *)malloc(sizeof(T_Gui_skin)); gfx = (T_Gui_skin *)GFX2_malloc(sizeof(T_Gui_skin));
if (gfx == NULL) if (gfx == NULL)
{ {
GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes\n", (unsigned long)sizeof(T_Gui_skin));
sprintf(Gui_loading_error_message, "Not enough memory to read skin file\n"); sprintf(Gui_loading_error_message, "Not enough memory to read skin file\n");
return NULL; return NULL;
} }
// Read the "skin" file // Read the "skin" file
len = strlen(Data_directory) + strlen(SKINS_SUBDIRECTORY) + strlen(PATH_SEPARATOR) + strlen(skin_file) + 1; len = strlen(Data_directory) + strlen(SKINS_SUBDIRECTORY) + strlen(PATH_SEPARATOR) + strlen(skin_file) + 1;
filename = malloc(len); filename = GFX2_malloc(len);
if (filename == NULL) if (filename == NULL)
{ {
GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes\n", (unsigned long)len);
free(gfx); free(gfx);
return NULL; return NULL;
} }
@ -728,7 +727,7 @@ static byte * Parse_font(T_GFX2_Surface * image, int is_main)
sprintf(Gui_loading_error_message, "Error in font file: Image is too small to be a 256-character 8x8 font.\n"); sprintf(Gui_loading_error_message, "Error in font file: Image is too small to be a 256-character 8x8 font.\n");
return NULL; return NULL;
} }
font = (byte *)malloc(8*8*character_count); font = (byte *)GFX2_malloc(8*8*character_count);
if (font == NULL) if (font == NULL)
{ {
sprintf(Gui_loading_error_message, "Not enough memory to read font file\n"); sprintf(Gui_loading_error_message, "Not enough memory to read font file\n");
@ -773,12 +772,9 @@ byte * Load_font(const char * font_name, int is_main)
// Read the file containing the image // Read the file containing the image
len = strlen(Data_directory) + strlen(SKINS_SUBDIRECTORY) + strlen(PATH_SEPARATOR) + strlen(font_name) + 1; len = strlen(Data_directory) + strlen(SKINS_SUBDIRECTORY) + strlen(PATH_SEPARATOR) + strlen(font_name) + 1;
filename = malloc(len); filename = GFX2_malloc(len);
if (filename == NULL) if (filename == NULL)
{
GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes\n", len);
return NULL; return NULL;
}
snprintf(filename, len, "%s%s%s%s", Data_directory, SKINS_SUBDIRECTORY, PATH_SEPARATOR, font_name); snprintf(filename, len, "%s%s%s%s", Data_directory, SKINS_SUBDIRECTORY, PATH_SEPARATOR, font_name);
image = Load_surface(filename, NULL); image = Load_surface(filename, NULL);
@ -808,12 +804,15 @@ static void Load_Unicode_font(const char * fullname, const char * filename)
font = Load_font(filename, 0); font = Load_font(filename, 0);
if (font) if (font)
{ {
ufont = malloc(sizeof(T_Unicode_Font)); ufont = GFX2_malloc(sizeof(T_Unicode_Font));
ufont->FirstChar = first; if (ufont != NULL)
ufont->LastChar = last; {
ufont->FontData = font; ufont->FirstChar = first;
ufont->Next = Unicode_fonts; ufont->LastChar = last;
Unicode_fonts = ufont; ufont->FontData = font;
ufont->Next = Unicode_fonts;
Unicode_fonts = ufont;
}
} }
} }
else else
@ -1955,8 +1954,8 @@ int Load_CFG(int reload_all)
if (size!=0) if (size!=0)
{ {
// Alloc string // Alloc string
Bound_script[current_script]=malloc(size+1); Bound_script[current_script] = GFX2_malloc(size+1);
if (Bound_script[current_script]==NULL) if (Bound_script[current_script] == NULL)
return ERROR_MEMORY; return ERROR_MEMORY;
// Init and load string // Init and load string

View File

@ -68,6 +68,7 @@
#include "unicode.h" #include "unicode.h"
#include "global.h" #include "global.h"
#include "gfx2log.h" #include "gfx2log.h"
#include "gfx2mem.h"
// Lit un octet // Lit un octet
// Renvoie -1 si OK, 0 en cas d'erreur // Renvoie -1 si OK, 0 en cas d'erreur
@ -307,7 +308,7 @@ char * Filepath_append_to_dir(const char * dir, const char * filename)
) )
{ {
len += strlen(filename) + 1; len += strlen(filename) + 1;
path = malloc(len); path = GFX2_malloc(len);
if (path == NULL) if (path == NULL)
return NULL; return NULL;
snprintf(path, len, "%s%s", dir, filename); snprintf(path, len, "%s%s", dir, filename);
@ -316,7 +317,7 @@ char * Filepath_append_to_dir(const char * dir, const char * filename)
{ {
// need to add a path separator // need to add a path separator
len += strlen(PATH_SEPARATOR) + strlen(filename) + 1; len += strlen(PATH_SEPARATOR) + strlen(filename) + 1;
path = malloc(len); path = GFX2_malloc(len);
if (path == NULL) if (path == NULL)
return NULL; return NULL;
snprintf(path, len, "%s%s%s", dir, PATH_SEPARATOR, filename); snprintf(path, len, "%s%s%s", dir, PATH_SEPARATOR, filename);
@ -655,7 +656,9 @@ void For_each_directory_entry(const char * directory_name, void * pdata, T_File_
HANDLE h; HANDLE h;
len = strlen(directory_name) + 3; len = strlen(directory_name) + 3;
search_string = (word *)malloc(sizeof(word) * len); search_string = (word *)GFX2_malloc(sizeof(word) * len);
if (search_string == NULL)
return;
Unicode_char_strlcpy(search_string, directory_name, len); Unicode_char_strlcpy(search_string, directory_name, len);
Unicode_char_strlcat(search_string, "\\*", len); Unicode_char_strlcat(search_string, "\\*", len);
h = FindFirstFileW((WCHAR *)search_string, &fd); h = FindFirstFileW((WCHAR *)search_string, &fd);
@ -708,19 +711,22 @@ void For_each_directory_entry(const char * directory_name, void * pdata, T_File_
size_t outbytesleft; size_t outbytesleft;
size_t r; size_t r;
unicode_filename = malloc(sizeof(word) * (inbytesleft + 1)); unicode_filename = GFX2_malloc(sizeof(word) * (inbytesleft + 1));
output = (char *)unicode_filename; if (unicode_filename != NULL)
outbytesleft = sizeof(word) * inbytesleft;
r = iconv(cd_utf16, &input, &inbytesleft, &output, &outbytesleft);
if (r != (size_t)-1)
{ {
output[0] = '\0'; output = (char *)unicode_filename;
output[1] = '\0'; outbytesleft = sizeof(word) * inbytesleft;
} r = iconv(cd_utf16, &input, &inbytesleft, &output, &outbytesleft);
else if (r != (size_t)-1)
{ {
free(unicode_filename); output[0] = '\0';
unicode_filename = NULL; output[1] = '\0';
}
else
{
free(unicode_filename);
unicode_filename = NULL;
}
} }
} }
#endif #endif
@ -765,7 +771,9 @@ word * Get_Unicode_Filename(word * filename_unicode, const char * filename, cons
WCHAR * longPath; WCHAR * longPath;
WCHAR * sep; WCHAR * sep;
shortPath = (WCHAR *)malloc(sizeof(WCHAR) * (strlen(filename) + strlen(directory) + 2)); shortPath = (WCHAR *)GFX2_malloc(sizeof(WCHAR) * (strlen(filename) + strlen(directory) + 2));
if (shortPath == NULL)
return NULL;
// copy the full path to a wide character buffer : // copy the full path to a wide character buffer :
while (directory[0] != '\0') while (directory[0] != '\0')
shortPath[i++] = *directory++; shortPath[i++] = *directory++;
@ -780,8 +788,8 @@ word * Get_Unicode_Filename(word * filename_unicode, const char * filename, cons
free(shortPath); free(shortPath);
return NULL; return NULL;
} }
longPath = (WCHAR *)malloc(len * sizeof(WCHAR)); longPath = (WCHAR *)GFX2_malloc(len * sizeof(WCHAR));
if (GetLongPathNameW(shortPath, longPath, len) == 0) if (longPath == NULL || GetLongPathNameW(shortPath, longPath, len) == 0)
{ {
free(shortPath); free(shortPath);
return NULL; return NULL;
@ -799,8 +807,9 @@ word * Get_Unicode_Filename(word * filename_unicode, const char * filename, cons
sep++; sep++;
len = wcslen(sep) + 1; len = wcslen(sep) + 1;
if (filename_unicode == NULL) if (filename_unicode == NULL)
filename_unicode = (word *)malloc(sizeof(word) * len); filename_unicode = (word *)GFX2_malloc(sizeof(word) * len);
memcpy(filename_unicode, sep, sizeof(word) * len); if (filename_unicode != NULL)
memcpy(filename_unicode, sep, sizeof(word) * len);
} }
free(longPath); free(longPath);
return filename_unicode; return filename_unicode;
@ -817,7 +826,7 @@ word * Get_Unicode_Filename(word * filename_unicode, const char * filename, cons
return NULL; return NULL;
if (filename_unicode == NULL) if (filename_unicode == NULL)
{ {
filename_unicode = malloc(sizeof(word) * (inbytesleft + 1)); filename_unicode = GFX2_malloc(sizeof(word) * (inbytesleft + 1));
if (filename_unicode == NULL) if (filename_unicode == NULL)
return NULL; return NULL;
allocated_memory = 1; allocated_memory = 1;
@ -936,7 +945,11 @@ char * Get_current_directory(char * buf, word * * unicode, size_t size)
{ {
#if defined(__MINT__) #if defined(__MINT__)
if (buf == NULL) if (buf == NULL)
buf = malloc(MAX_PATH_CHARACTERS); {
buf = GFX2_malloc(MAX_PATH_CHARACTERS);
if (buf == NULL)
return NULL;
}
buf[0] = 'A'+Dgetdrv(); buf[0] = 'A'+Dgetdrv();
buf[1] = ':'; buf[1] = ':';
buf[2] = '\\'; buf[2] = '\\';
@ -953,18 +966,15 @@ char * Get_current_directory(char * buf, word * * unicode, size_t size)
size = (size_t)GetCurrentDirectoryA(0, NULL); size = (size_t)GetCurrentDirectoryA(0, NULL);
if (size == 0) if (size == 0)
return NULL; return NULL;
buf = (char *)malloc(size); buf = (char *)GFX2_malloc(size);
if (buf == NULL) if (buf == NULL)
{
GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes.\n", (unsigned long)size);
return NULL; return NULL;
}
} }
if (GetCurrentDirectoryA(size, buf) == 0) if (GetCurrentDirectoryA(size, buf) == 0)
return NULL; return NULL;
if (unicode != NULL) if (unicode != NULL)
{ {
WCHAR * temp = (WCHAR *)malloc(sizeof(WCHAR) * size); WCHAR * temp = (WCHAR *)GFX2_malloc(sizeof(WCHAR) * size);
if (temp != NULL) if (temp != NULL)
{ {
size_t i; size_t i;
@ -972,8 +982,9 @@ char * Get_current_directory(char * buf, word * * unicode, size_t size)
temp[i] = (WCHAR)buf[i] & 0x00ff; temp[i] = (WCHAR)buf[i] & 0x00ff;
temp[i] = 0; temp[i] = 0;
size = GetLongPathNameW(temp, NULL, 0); size = GetLongPathNameW(temp, NULL, 0);
*unicode = (word *)malloc(size*sizeof(word)); *unicode = (word *)GFX2_malloc(size*sizeof(word));
GetLongPathNameW(temp, (WCHAR *)*unicode, size); if (*unicode != NULL)
GetLongPathNameW(temp, (WCHAR *)*unicode, size);
free(temp); free(temp);
} }
} }
@ -987,7 +998,7 @@ char * Get_current_directory(char * buf, word * * unicode, size_t size)
{ {
char * input = ret; char * input = ret;
size_t inbytesleft = strlen(ret); size_t inbytesleft = strlen(ret);
word * buf_unicode = malloc((inbytesleft + 1) * 2); word * buf_unicode = GFX2_malloc((inbytesleft + 1) * 2);
char * output = (char *)buf_unicode; char * output = (char *)buf_unicode;
size_t outbytesleft = 2 * inbytesleft; size_t outbytesleft = 2 * inbytesleft;
if (cd_utf16 != (iconv_t)-1 && buf_unicode != NULL) if (cd_utf16 != (iconv_t)-1 && buf_unicode != NULL)
@ -1100,16 +1111,18 @@ char * Calculate_relative_path(const char * ref_path, const char * path)
{ {
free(real_ref_path); free(real_ref_path);
len = strlen(path + i) + 1; len = strlen(path + i) + 1;
rel_path = malloc(len + 1); rel_path = GFX2_malloc(len + 1);
snprintf(rel_path, len, ".%s", path + i); if (rel_path != NULL)
snprintf(rel_path, len, ".%s", path + i);
return rel_path; return rel_path;
} }
else if (i > 0 && real_ref_path[i - 1] == PATH_SEPARATOR[0]) else if (i > 0 && real_ref_path[i - 1] == PATH_SEPARATOR[0])
{ {
free(real_ref_path); free(real_ref_path);
len = strlen(path + i - 1) + 1; len = strlen(path + i - 1) + 1;
rel_path = malloc(len + 1); rel_path = GFX2_malloc(len + 1);
snprintf(rel_path, len, ".%s", path + i - 1); if (rel_path != NULL)
snprintf(rel_path, len, ".%s", path + i - 1);
return rel_path; return rel_path;
} }
} }
@ -1128,10 +1141,13 @@ char * Calculate_relative_path(const char * ref_path, const char * path)
i = 0; i = 0;
// construct the relative path // construct the relative path
len = separator_count * (2 + strlen(PATH_SEPARATOR)) + strlen(path + last_separator + 1) + 1; len = separator_count * (2 + strlen(PATH_SEPARATOR)) + strlen(path + last_separator + 1) + 1;
rel_path = malloc(len + 1); rel_path = GFX2_malloc(len + 1);
while(separator_count-- > 0) if (rel_path != NULL)
i += snprintf(rel_path + i, len + 1 - i, "..%s", PATH_SEPARATOR); {
strncpy(rel_path + i, path + last_separator + 1, len + 1 - i); while(separator_count-- > 0)
rel_path[len] = '\0'; i += snprintf(rel_path + i, len + 1 - i, "..%s", PATH_SEPARATOR);
strncpy(rel_path + i, path + last_separator + 1, len + 1 - i);
rel_path[len] = '\0';
}
return rel_path; return rel_path;
} }

View File

@ -52,6 +52,7 @@
#endif #endif
#include "gfx2log.h" #include "gfx2log.h"
#include "gfx2mem.h"
#include "buttons.h" #include "buttons.h"
#include "const.h" #include "const.h"
#include "errors.h" #include "errors.h"
@ -501,7 +502,7 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size,
break; break;
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
context->Buffer_image = (byte *)malloc(width*height); context->Buffer_image = (byte *)GFX2_malloc(width*height);
if (! context->Buffer_image) if (! context->Buffer_image)
{ {
File_error=3; File_error=3;
@ -540,8 +541,8 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size,
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
case CONTEXT_SURFACE: case CONTEXT_SURFACE:
// Allocate 24bit buffer // Allocate 24bit buffer
context->Buffer_image_24b= context->Buffer_image_24b =
(T_Components *)malloc(width*height*sizeof(T_Components)); (T_Components *)GFX2_malloc(width*height*sizeof(T_Components));
if (!context->Buffer_image_24b) if (!context->Buffer_image_24b)
{ {
// Print an error message // Print an error message
@ -1950,7 +1951,7 @@ static void Add_backup_file(const char * full_name, const char *file_name)
} }
// Add to list (top insertion) // Add to list (top insertion)
elem = (T_String_list *)malloc(sizeof(T_String_list)); elem = (T_String_list *)GFX2_malloc(sizeof(T_String_list));
elem->String=strdup(file_name); elem->String=strdup(file_name);
elem->Next=*list; elem->Next=*list;
*list=elem; *list=elem;
@ -1991,7 +1992,8 @@ byte Process_backups(T_String_list **list)
element = element->Next; element = element->Next;
} }
// Allocate a vector // Allocate a vector
files_vector = (char **)malloc(sizeof(char *) * nb_files); files_vector = (char **)GFX2_malloc(sizeof(char *) * nb_files);
// TODO
// Copy from list to vector // Copy from list to vector
for (i=0;i<nb_files;i++) for (i=0;i<nb_files;i++)
{ {
@ -2109,12 +2111,9 @@ void Rotate_safety_backups(void)
char * deleted_file; char * deleted_file;
size_t len = strlen(Config_directory) + strlen(BACKUP_FILE_EXTENSION) + 1 + 6 + 1; size_t len = strlen(Config_directory) + strlen(BACKUP_FILE_EXTENSION) + 1 + 6 + 1;
deleted_file = malloc(len); deleted_file = GFX2_malloc(len);
if (deleted_file == NULL) if (deleted_file == NULL)
{
GFX2_Log(GFX2_ERROR, "Failed to allocate %lu bytes.\n", (unsigned long)len);
return; return;
}
// Clear a previous save (rotating saves) // Clear a previous save (rotating saves)
snprintf(deleted_file, len, "%s%c%6.6d" BACKUP_FILE_EXTENSION, snprintf(deleted_file, len, "%s%c%6.6d" BACKUP_FILE_EXTENSION,
Config_directory, Config_directory,
@ -2200,7 +2199,9 @@ FILE * Open_file_write(T_IO_Context *context)
len = strlen(context->File_directory) + strlen(PATH_SEPARATOR) len = strlen(context->File_directory) + strlen(PATH_SEPARATOR)
+ Unicode_strlen(context->File_name_unicode) + 1; + Unicode_strlen(context->File_name_unicode) + 1;
filename_unicode = (WCHAR *)malloc(sizeof(WCHAR) * len); filename_unicode = (WCHAR *)GFX2_malloc(sizeof(WCHAR) * len);
if (filename_unicode == NULL)
return NULL;
Unicode_char_strlcpy((word *)filename_unicode, context->File_directory, len); Unicode_char_strlcpy((word *)filename_unicode, context->File_directory, len);
Unicode_char_strlcat((word *)filename_unicode, PATH_SEPARATOR, len); Unicode_char_strlcat((word *)filename_unicode, PATH_SEPARATOR, len);
@ -2213,17 +2214,23 @@ FILE * Open_file_write(T_IO_Context *context)
len = GetShortPathNameW(filename_unicode, NULL, 0); len = GetShortPathNameW(filename_unicode, NULL, 0);
if (len > 0) if (len > 0)
{ {
WCHAR * shortpath = (WCHAR *)malloc(sizeof(WCHAR) * len); WCHAR * shortpath = (WCHAR *)GFX2_malloc(sizeof(WCHAR) * len);
len = GetShortPathNameW(filename_unicode, shortpath, len); if (shortpath != NULL)
if (len > 0)
{ {
DWORD start, index; len = GetShortPathNameW(filename_unicode, shortpath, len);
for (start = len; start > 0 && shortpath[start-1] != '\\'; start--); if (len > 0)
free(context->File_name); {
context->File_name = (char *)malloc(len + 1 - start); DWORD start, index;
for (index = 0; index < len - start; index++) for (start = len; start > 0 && shortpath[start-1] != '\\'; start--);
context->File_name[index] = shortpath[start + index]; free(context->File_name);
context->File_name[index] = '\0'; context->File_name = (char *)GFX2_malloc(len + 1 - start);
if (context->File_name != NULL)
{
for (index = 0; index < len - start; index++)
context->File_name[index] = shortpath[start + index];
context->File_name[index] = '\0';
}
}
} }
} }
} }
@ -2233,6 +2240,8 @@ FILE * Open_file_write(T_IO_Context *context)
#endif #endif
filename = Filepath_append_to_dir(context->File_directory, context->File_name); filename = Filepath_append_to_dir(context->File_directory, context->File_name);
if (filename == NULL)
return NULL;
f = fopen(filename, "wb"); f = fopen(filename, "wb");
free(filename); free(filename);
return f; return f;
@ -2252,7 +2261,9 @@ FILE * Open_file_write_with_alternate_ext(T_IO_Context *context, const char * ex
len = strlen(context->File_directory) + strlen(PATH_SEPARATOR) len = strlen(context->File_directory) + strlen(PATH_SEPARATOR)
+ Unicode_strlen(context->File_name_unicode) + strlen(ext) + 1 + 1; + Unicode_strlen(context->File_name_unicode) + strlen(ext) + 1 + 1;
filename_unicode = (WCHAR *)malloc(len * sizeof(WCHAR)); filename_unicode = (WCHAR *)GFX2_malloc(len * sizeof(WCHAR));
if (filename_unicode == NULL)
return NULL;
Unicode_char_strlcpy((word *)filename_unicode, context->File_directory, len); Unicode_char_strlcpy((word *)filename_unicode, context->File_directory, len);
Unicode_char_strlcat((word *)filename_unicode, PATH_SEPARATOR, len); Unicode_char_strlcat((word *)filename_unicode, PATH_SEPARATOR, len);
Unicode_strlcat((word *)filename_unicode, context->File_name_unicode, len); Unicode_strlcat((word *)filename_unicode, context->File_name_unicode, len);
@ -2323,7 +2334,9 @@ static void Look_for_alternate_ext(void * pdata, const char * filename, const wo
#if defined(WIN32) #if defined(WIN32)
{ {
int cmp; int cmp;
WCHAR * temp_string = (WCHAR *)malloc((base_len + 1) * sizeof(WCHAR)); WCHAR * temp_string = (WCHAR *)GFX2_malloc((base_len + 1) * sizeof(WCHAR));
if (temp_string == NULL)
return;
memcpy(temp_string, filename_unicode, base_len * sizeof(word)); memcpy(temp_string, filename_unicode, base_len * sizeof(word));
temp_string[base_len] = 0; temp_string[base_len] = 0;
cmp = _wcsicmp((const WCHAR *)params->basename_unicode, temp_string); cmp = _wcsicmp((const WCHAR *)params->basename_unicode, temp_string);
@ -2383,12 +2396,8 @@ FILE * Open_file_read_with_alternate_ext(T_IO_Context *context, const char * ext
if (context->File_name_unicode != NULL) if (context->File_name_unicode != NULL)
{ {
size_t i = Unicode_strlen(context->File_name_unicode); size_t i = Unicode_strlen(context->File_name_unicode);
params.basename_unicode = malloc(sizeof(word) * (i + 1)); params.basename_unicode = GFX2_malloc(sizeof(word) * (i + 1));
if (params.basename_unicode == NULL) if (params.basename_unicode != NULL)
{
GFX2_Log(GFX2_ERROR, "Open_file_read_with_alternate_ext() failed to allocate %lu bytes\n", (unsigned long)(sizeof(word) * (i + 1)));
}
else
{ {
memcpy(params.basename_unicode, context->File_name_unicode, (i + 1) * sizeof(word)); memcpy(params.basename_unicode, context->File_name_unicode, (i + 1) * sizeof(word));
while (i-- > 0) while (i-- > 0)

View File

@ -104,6 +104,7 @@
#endif #endif
#include "gfx2log.h" #include "gfx2log.h"
#include "gfx2mem.h"
#include "const.h" #include "const.h"
#include "struct.h" #include "struct.h"
#include "global.h" #include "global.h"
@ -649,8 +650,8 @@ int Init_program(int argc,char * argv[])
// principale et la page de brouillon afin que leurs champs ne soient pas // principale et la page de brouillon afin que leurs champs ne soient pas
// invalide lors des appels aux multiples fonctions manipulées à // invalide lors des appels aux multiples fonctions manipulées à
// l'initialisation du programme. // l'initialisation du programme.
Main.backups=(T_List_of_pages *)malloc(sizeof(T_List_of_pages)); Main.backups=(T_List_of_pages *)GFX2_malloc(sizeof(T_List_of_pages));
Spare.backups=(T_List_of_pages *)malloc(sizeof(T_List_of_pages)); Spare.backups=(T_List_of_pages *)GFX2_malloc(sizeof(T_List_of_pages));
Init_list_of_pages(Main.backups); Init_list_of_pages(Main.backups);
Init_list_of_pages(Spare.backups); Init_list_of_pages(Spare.backups);
@ -870,7 +871,9 @@ int Init_program(int argc,char * argv[])
Windows_open=0; Windows_open=0;
// Paintbrush // Paintbrush
if (!(Paintbrush_sprite=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE))) Error(ERROR_MEMORY); Paintbrush_sprite = (byte *)GFX2_malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE);
if (!Paintbrush_sprite)
Error(ERROR_MEMORY);
// Load preset paintbrushes (uses Paintbrush_ variables) // Load preset paintbrushes (uses Paintbrush_ variables)
Init_paintbrushes(); Init_paintbrushes();
@ -959,8 +962,12 @@ int Init_program(int argc,char * argv[])
Back_color=Best_color_range(0,0,0,Config.Palette_cells_X*Config.Palette_cells_Y); Back_color=Best_color_range(0,0,0,Config.Palette_cells_X*Config.Palette_cells_Y);
// Allocation de mémoire pour la brosse // Allocation de mémoire pour la brosse
if (!(Brush =(byte *)malloc( 1* 1))) Error(ERROR_MEMORY); Brush = (byte *)GFX2_malloc(1*1);
if (!(Smear_brush =(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE))) Error(ERROR_MEMORY); if (!Brush)
Error(ERROR_MEMORY);
Smear_brush = (byte *)GFX2_malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE);
if (!Smear_brush)
Error(ERROR_MEMORY);
starting_videomode=Current_resolution; starting_videomode=Current_resolution;

View File

@ -30,6 +30,7 @@
#define strdup _strdup #define strdup _strdup
#endif #endif
#include "gfx2mem.h"
#include "global.h" #include "global.h"
#include "pages.h" #include "pages.h"
#include "errors.h" #include "errors.h"
@ -66,7 +67,7 @@ T_Page * New_page(int nb_layers)
{ {
T_Page * page; T_Page * page;
page = (T_Page *)malloc(sizeof(T_Page)+nb_layers*sizeof(T_Image)); page = (T_Page *)GFX2_malloc(sizeof(T_Page)+nb_layers*sizeof(T_Image));
if (page!=NULL) if (page!=NULL)
{ {
int i; int i;
@ -107,7 +108,7 @@ T_Page * New_page(int nb_layers)
/// Allocate a new layer /// Allocate a new layer
byte * New_layer(long pixel_size) byte * New_layer(long pixel_size)
{ {
short * ptr = malloc(sizeof(short)+pixel_size); short * ptr = GFX2_malloc(sizeof(short)+pixel_size);
if (ptr==NULL) if (ptr==NULL)
return NULL; return NULL;
@ -715,7 +716,7 @@ int Update_buffers(int width, int height)
{ {
// Current image // Current image
free(Main.visible_image.Image); free(Main.visible_image.Image);
Main.visible_image.Image = (byte *)malloc(width * height); Main.visible_image.Image = (byte *)GFX2_malloc(width * height);
if (Main.visible_image.Image == NULL) if (Main.visible_image.Image == NULL)
return 0; return 0;
} }
@ -726,7 +727,7 @@ int Update_buffers(int width, int height)
{ {
// Previous image // Previous image
free(Main_visible_image_backup.Image); free(Main_visible_image_backup.Image);
Main_visible_image_backup.Image = (byte *)malloc(width * height); Main_visible_image_backup.Image = (byte *)GFX2_malloc(width * height);
if (Main_visible_image_backup.Image == NULL) if (Main_visible_image_backup.Image == NULL)
return 0; return 0;
} }
@ -737,7 +738,7 @@ int Update_buffers(int width, int height)
{ {
// Depth buffer // Depth buffer
free(Main_visible_image_depth_buffer.Image); free(Main_visible_image_depth_buffer.Image);
Main_visible_image_depth_buffer.Image = (byte *)malloc(width * height); Main_visible_image_depth_buffer.Image = (byte *)GFX2_malloc(width * height);
if (Main_visible_image_depth_buffer.Image == NULL) if (Main_visible_image_depth_buffer.Image == NULL)
return 0; return 0;
} }
@ -757,7 +758,7 @@ int Update_spare_buffers(int width, int height)
{ {
// Current image // Current image
free(Spare.visible_image.Image); free(Spare.visible_image.Image);
Spare.visible_image.Image = (byte *)malloc(width * height); Spare.visible_image.Image = (byte *)GFX2_malloc(width * height);
if (Spare.visible_image.Image == NULL) if (Spare.visible_image.Image == NULL)
return 0; return 0;
} }
@ -1022,7 +1023,7 @@ int Backup_in_place(int width,int height)
{ {
// Current image // Current image
free(Main.visible_image.Image); free(Main.visible_image.Image);
Main.visible_image.Image = (byte *)malloc(width * height); Main.visible_image.Image = (byte *)GFX2_malloc(width * height);
if (Main.visible_image.Image == NULL) if (Main.visible_image.Image == NULL)
return 0; return 0;
} }
@ -1033,7 +1034,7 @@ int Backup_in_place(int width,int height)
{ {
// Depth buffer // Depth buffer
free(Main_visible_image_depth_buffer.Image); free(Main_visible_image_depth_buffer.Image);
Main_visible_image_depth_buffer.Image = (byte *)malloc(width * height); Main_visible_image_depth_buffer.Image = (byte *)GFX2_malloc(width * height);
if (Main_visible_image_depth_buffer.Image == NULL) if (Main_visible_image_depth_buffer.Image == NULL)
return 0; return 0;
} }

View File

@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "gfx2mem.h"
#include "unicode.h" #include "unicode.h"
size_t Unicode_strlen(const word * str) size_t Unicode_strlen(const word * str)
@ -43,7 +44,7 @@ word * Unicode_strdup(const word * str)
if (str == NULL) if (str == NULL)
return NULL; return NULL;
byte_size = Unicode_strlen(str) * 2 + 2; byte_size = Unicode_strlen(str) * 2 + 2;
new_str = malloc(byte_size); new_str = GFX2_malloc(byte_size);
if (new_str != NULL) if (new_str != NULL)
memcpy(new_str, str, byte_size); memcpy(new_str, str, byte_size);
return new_str; return new_str;