Commit ccbc5864 authored by Petri Hintukainen's avatar Petri Hintukainen

Check malloc/calloc result

parent 80fdc46b
......@@ -32,7 +32,6 @@
#include <windows.h>
#include <shlobj.h>
#include <limits.h>
#include <direct.h>
char *win32_get_font_dir(const char *font_file)
......@@ -45,9 +44,11 @@ char *win32_get_font_dir(const char *font_file)
int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
char *path = malloc(len + strlen(font_file) + 2);
WideCharToMultiByte(CP_UTF8, 0, wdir, -1, path, len, NULL, NULL);
path[len - 1] = '\\';
strcpy(path + len, font_file);
if (path) {
WideCharToMultiByte(CP_UTF8, 0, wdir, -1, path, len, NULL, NULL);
path[len - 1] = '\\';
strcpy(path + len, font_file);
}
return path;
}
......@@ -65,7 +66,9 @@ char *file_get_data_home(void)
NULL, SHGFP_TYPE_CURRENT, wdir)) {
int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
char *appdir = malloc(len);
WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
if (appdir) {
WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
}
return appdir;
}
......@@ -94,7 +97,9 @@ const char *file_get_config_system(const char *dir)
NULL, SHGFP_TYPE_CURRENT, wdir)) {
int len = WideCharToMultiByte (CP_UTF8, 0, wdir, -1, NULL, 0, NULL, NULL);
appdir = malloc(len);
WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
if (appdir) {
WideCharToMultiByte (CP_UTF8, 0, wdir, -1, appdir, len, NULL, NULL);
}
return appdir;
} else {
BD_DEBUG(DBG_FILE, "Can't find common configuration directory !\n");
......
......@@ -95,28 +95,40 @@ static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
static BD_FILE_H *_file_open(const char* filename, const char *mode)
{
FILE *fp = NULL;
BD_FILE_H *file;
FILE *fp;
wchar_t wfilename[MAX_PATH], wmode[8];
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) &&
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8) &&
(fp = _wfopen(wfilename, wmode))) {
BD_FILE_H *file = calloc(1, sizeof(BD_FILE_H));
file->internal = fp;
file->close = _file_close;
file->seek = _file_seek;
file->read = _file_read;
file->write = _file_write;
file->tell = _file_tell;
//file->eof = _file_eof;
BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
return file;
if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) ||
!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8)) {
BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
return NULL;
}
fp = _wfopen(wfilename, wmode);
if (!fp) {
BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
return NULL;
}
file = calloc(1, sizeof(BD_FILE_H));
if (!file) {
BD_DEBUG(DBG_FILE | DBG_CRIT, "Error opening file %s (out of memory)\n", filename);
fclose(fp);
return NULL;
}
BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
return NULL;
file->internal = fp;
file->close = _file_close;
file->seek = _file_seek;
file->read = _file_read;
file->write = _file_write;
file->tell = _file_tell;
//file->eof = _file_eof;
BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
return file;
}
BD_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
......
......@@ -33,7 +33,16 @@
char *str_dup(const char *str)
{
return str ? strcpy (malloc(strlen(str) + 1), str) : NULL;
char *dup = NULL;
if (str) {
size_t size = strlen(str) + 1;
dup = malloc(size);
if (dup) {
memcpy(dup, str, size);
}
}
return dup;
}
char *str_printf(const char *fmt, ...)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment