aboutsummaryrefslogtreecommitdiffstats
path: root/c_src/sdl_window.c
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-04-04 20:13:10 +0200
committerLoïc Hoguin <[email protected]>2014-04-04 20:13:10 +0200
commitbe44046850adf018e648752571247115638df75b (patch)
tree05987991abd2472db7f666334eeee64a885565a5 /c_src/sdl_window.c
parent0633a7b6f0b1eb20043b71cc59847ca8648732f0 (diff)
downloadesdl2-be44046850adf018e648752571247115638df75b.tar.gz
esdl2-be44046850adf018e648752571247115638df75b.tar.bz2
esdl2-be44046850adf018e648752571247115638df75b.zip
Fix potential memory leaks and other small improvements
Diffstat (limited to 'c_src/sdl_window.c')
-rw-r--r--c_src/sdl_window.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/c_src/sdl_window.c b/c_src/sdl_window.c
index c35fb0c..c644c09 100644
--- a/c_src/sdl_window.c
+++ b/c_src/sdl_window.c
@@ -75,12 +75,11 @@ NIF_CALL_HANDLER(thread_create_window)
NIF_FUNCTION(create_window)
{
- char* title = (char*)enif_alloc(255);
+ unsigned int len;
+ char* title;
int x, y, w, h;
Uint32 flags = 0;
- BADARG_IF(!enif_get_string(env, argv[0], title, 255, ERL_NIF_LATIN1));
-
if (enif_is_atom(env, argv[1])) {
BADARG_IF(!atom_to_window_pos(env, argv[1], &x));
} else {
@@ -97,6 +96,16 @@ NIF_FUNCTION(create_window)
BADARG_IF(!enif_get_int(env, argv[4], &h));
BADARG_IF(!list_to_window_flags(env, argv[5], &flags));
+ // Getting the title last to simplify the code due to memory allocation.
+
+ BADARG_IF(!enif_get_list_length(env, argv[0], &len));
+ title = (char*)enif_alloc(len + 1);
+
+ if (!enif_get_string(env, argv[0], title, len + 1, ERL_NIF_LATIN1)) {
+ enif_free(title);
+ return enif_make_badarg(env);
+ }
+
return nif_thread_call(env, thread_create_window, 6,
title, x, y, w, h, flags);
}
@@ -465,17 +474,18 @@ NIF_CALL_HANDLER(thread_set_window_brightness)
NIF_FUNCTION(set_window_brightness)
{
void* window_res;
- double f;
- double *fp;
+ double *brightnessPtr;
BADARG_IF(!enif_get_resource(env, argv[0], res_Window, &window_res));
- BADARG_IF(!enif_get_double(env, argv[1], &f));
- fp = (double*)enif_alloc(sizeof(double));
- *fp = f;
+ brightnessPtr = (double*)enif_alloc(sizeof(double));
+ if (!enif_get_double(env, argv[1], brightnessPtr)) {
+ enif_free(brightnessPtr);
+ return enif_make_badarg(env);
+ }
return nif_thread_call(env, thread_set_window_brightness, 2,
- NIF_RES_GET(Window, window_res), fp);
+ NIF_RES_GET(Window, window_res), brightnessPtr);
}
// set_window_fullscreen
@@ -636,10 +646,18 @@ NIF_CAST_HANDLER(thread_set_window_title)
NIF_FUNCTION(set_window_title)
{
void* window_res;
- char* title = (char*)enif_alloc(255);
+ unsigned int len;
+ char* title;
BADARG_IF(!enif_get_resource(env, argv[0], res_Window, &window_res));
- BADARG_IF(!enif_get_string(env, argv[1], title, 255, ERL_NIF_LATIN1));
+
+ BADARG_IF(!enif_get_list_length(env, argv[1], &len));
+ title = (char*)enif_alloc(len + 1);
+
+ if (!enif_get_string(env, argv[1], title, len + 1, ERL_NIF_LATIN1)) {
+ enif_free(title);
+ return enif_make_badarg(env);
+ }
return nif_thread_cast(env, thread_set_window_title, 2,
NIF_RES_GET(Window, window_res), title);