summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_promise.c
diff options
context:
space:
mode:
authorFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-06-03 20:02:02 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2016-06-03 20:02:02 -0300
commitcd91b02dd1f04daaed19b51c7fda2d4047b4ad78 (patch)
treeabd67fb32ef8cc551628705323aaf79dac9390eb /src/lib/eina/eina_promise.c
parente98c57d9d414c654c2e90cad19ec791d02374448 (diff)
eina: Add NULL checks and EINA_MAGIC checks to promises
Add NULL checks and EINA_MAGIC checks that avoid crashing when it shouldn't.
Diffstat (limited to '')
-rw-r--r--src/lib/eina/eina_promise.c64
1 files changed, 62 insertions, 2 deletions
diff --git a/src/lib/eina/eina_promise.c b/src/lib/eina/eina_promise.c
index e1fda3ca93..7eac26ef4a 100644
--- a/src/lib/eina/eina_promise.c
+++ b/src/lib/eina/eina_promise.c
@@ -667,6 +667,8 @@ eina_promise_all(Eina_Iterator* it)
667 Eina_Promise **cur_promise, **last; 667 Eina_Promise **cur_promise, **last;
668 _Eina_Promise_Iterator* internal_it; 668 _Eina_Promise_Iterator* internal_it;
669 669
670 EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL);
671
670 promises = eina_array_new(20); 672 promises = eina_array_new(20);
671 673
672 EINA_ITERATOR_FOREACH(it, current) 674 EINA_ITERATOR_FOREACH(it, current)
@@ -755,6 +757,7 @@ _eina_promise_progress_notify_fulfilled(void* data, Eina_Promise_Owner* p EINA_U
755 757
756EAPI Eina_Error EINA_ERROR_PROMISE_NO_NOTIFY; 758EAPI Eina_Error EINA_ERROR_PROMISE_NO_NOTIFY;
757EAPI Eina_Error EINA_ERROR_PROMISE_CANCEL; 759EAPI Eina_Error EINA_ERROR_PROMISE_CANCEL;
760EAPI Eina_Error EINA_ERROR_PROMISE_NULL;
758 761
759static void 762static void
760_eina_promise_progress_notify_failed(void* data) 763_eina_promise_progress_notify_failed(void* data)
@@ -831,6 +834,8 @@ eina_promise_race(Eina_Iterator* it)
831 _Eina_Promise_Race_Value_Type *value; 834 _Eina_Promise_Race_Value_Type *value;
832 int num_promises; 835 int num_promises;
833 836
837 EINA_SAFETY_ON_NULL_RETURN_VAL(it, NULL);
838
834 promises = eina_array_new(20); 839 promises = eina_array_new(20);
835 840
836 EINA_ITERATOR_FOREACH(it, current) 841 EINA_ITERATOR_FOREACH(it, current)
@@ -872,36 +877,65 @@ EAPI void
872eina_promise_then(Eina_Promise* promise, Eina_Promise_Cb callback, 877eina_promise_then(Eina_Promise* promise, Eina_Promise_Cb callback,
873 Eina_Promise_Error_Cb error_cb, void* data) 878 Eina_Promise_Error_Cb error_cb, void* data)
874{ 879{
875 promise->then(promise, callback, error_cb, data); 880 if(!promise)
876} 881 {
882 error_cb(data, EINA_ERROR_PROMISE_NULL);
883 return;
884 }
885 else
886 promise->then(promise, callback, error_cb, data);
887}
888
889#define _EINA_PROMISE_NULL_CHECK(promise, v) \
890 if(!promise) \
891 { \
892 eina_error_set(EINA_ERROR_PROMISE_NULL); \
893 return v; \
894 }
895#define EINA_MAGIC_CHECK_PROMISE(promise) \
896 do if(!EINA_MAGIC_CHECK(promise, EINA_MAGIC_PROMISE)) \
897 {EINA_MAGIC_FAIL(promise, EINA_MAGIC_PROMISE);} while(0)
898#define EINA_MAGIC_CHECK_PROMISE_OWNER(promise) \
899 do {if(!EINA_MAGIC_CHECK(promise, EINA_MAGIC_PROMISE_OWNER)) \
900 {EINA_MAGIC_FAIL(promise, EINA_MAGIC_PROMISE_OWNER);} } while(0)
877 901
878EAPI void 902EAPI void
879eina_promise_owner_value_set(Eina_Promise_Owner* promise, const void* value, Eina_Promise_Free_Cb free) 903eina_promise_owner_value_set(Eina_Promise_Owner* promise, const void* value, Eina_Promise_Free_Cb free)
880{ 904{
905 _EINA_PROMISE_NULL_CHECK(promise, );
906 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
881 promise->value_set(promise, value, free); 907 promise->value_set(promise, value, free);
882} 908}
883 909
884EAPI void 910EAPI void
885eina_promise_owner_error_set(Eina_Promise_Owner* promise, Eina_Error error) 911eina_promise_owner_error_set(Eina_Promise_Owner* promise, Eina_Error error)
886{ 912{
913 _EINA_PROMISE_NULL_CHECK(promise, );
914 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
887 promise->error_set(promise, error); 915 promise->error_set(promise, error);
888} 916}
889 917
890EAPI void * 918EAPI void *
891eina_promise_value_get(Eina_Promise const* promise) 919eina_promise_value_get(Eina_Promise const* promise)
892{ 920{
921 _EINA_PROMISE_NULL_CHECK(promise, NULL);
922 EINA_MAGIC_CHECK_PROMISE(promise);
893 return promise->value_get(promise); 923 return promise->value_get(promise);
894} 924}
895 925
896EAPI Eina_Error 926EAPI Eina_Error
897eina_promise_error_get(Eina_Promise const* promise) 927eina_promise_error_get(Eina_Promise const* promise)
898{ 928{
929 _EINA_PROMISE_NULL_CHECK(promise, EINA_ERROR_PROMISE_NULL);
930 EINA_MAGIC_CHECK_PROMISE(promise);
899 return promise->error_get(promise); 931 return promise->error_get(promise);
900} 932}
901 933
902EAPI Eina_Bool 934EAPI Eina_Bool
903eina_promise_pending_is(Eina_Promise const* promise) 935eina_promise_pending_is(Eina_Promise const* promise)
904{ 936{
937 _EINA_PROMISE_NULL_CHECK(promise, EINA_FALSE);
938 EINA_MAGIC_CHECK_PROMISE(promise);
905 return promise->pending_is(promise); 939 return promise->pending_is(promise);
906} 940}
907 941
@@ -909,66 +943,88 @@ EAPI void
909eina_promise_progress_cb_add(Eina_Promise* promise, Eina_Promise_Progress_Cb callback, void* data, 943eina_promise_progress_cb_add(Eina_Promise* promise, Eina_Promise_Progress_Cb callback, void* data,
910 Eina_Promise_Free_Cb free_cb) 944 Eina_Promise_Free_Cb free_cb)
911{ 945{
946 _EINA_PROMISE_NULL_CHECK(promise, );
947 EINA_MAGIC_CHECK_PROMISE(promise);
912 promise->progress_cb_add(promise, callback, data, free_cb); 948 promise->progress_cb_add(promise, callback, data, free_cb);
913} 949}
914 950
915EAPI void 951EAPI void
916eina_promise_cancel(Eina_Promise* promise) 952eina_promise_cancel(Eina_Promise* promise)
917{ 953{
954 _EINA_PROMISE_NULL_CHECK(promise, );
955 EINA_MAGIC_CHECK_PROMISE(promise);
918 promise->cancel(promise); 956 promise->cancel(promise);
919} 957}
920 958
921EAPI void 959EAPI void
922eina_promise_ref(Eina_Promise* promise) 960eina_promise_ref(Eina_Promise* promise)
923{ 961{
962 _EINA_PROMISE_NULL_CHECK(promise, );
963 EINA_MAGIC_CHECK_PROMISE(promise);
924 promise->ref(promise); 964 promise->ref(promise);
925} 965}
926 966
927EAPI void 967EAPI void
928eina_promise_unref(Eina_Promise* promise) 968eina_promise_unref(Eina_Promise* promise)
929{ 969{
970 _EINA_PROMISE_NULL_CHECK(promise, );
971 EINA_MAGIC_CHECK_PROMISE(promise);
930 promise->unref(promise); 972 promise->unref(promise);
931} 973}
932 974
933EAPI void * 975EAPI void *
934eina_promise_owner_buffer_get(Eina_Promise_Owner* promise) 976eina_promise_owner_buffer_get(Eina_Promise_Owner* promise)
935{ 977{
978 _EINA_PROMISE_NULL_CHECK(promise, NULL);
979 EINA_MAGIC_CHECK_PROMISE(promise);
936 return promise->buffer_get(promise); 980 return promise->buffer_get(promise);
937} 981}
938 982
939EAPI void * 983EAPI void *
940eina_promise_buffer_get(Eina_Promise* promise) 984eina_promise_buffer_get(Eina_Promise* promise)
941{ 985{
986 _EINA_PROMISE_NULL_CHECK(promise, NULL);
987 EINA_MAGIC_CHECK_PROMISE(promise);
942 return promise->buffer_get(promise); 988 return promise->buffer_get(promise);
943} 989}
944 990
945EAPI size_t 991EAPI size_t
946eina_promise_value_size_get(Eina_Promise const* promise) 992eina_promise_value_size_get(Eina_Promise const* promise)
947{ 993{
994 _EINA_PROMISE_NULL_CHECK(promise, 0);
995 EINA_MAGIC_CHECK_PROMISE(promise);
948 return promise->value_size_get(promise); 996 return promise->value_size_get(promise);
949} 997}
950 998
951EAPI Eina_Promise * 999EAPI Eina_Promise *
952eina_promise_owner_promise_get(Eina_Promise_Owner* promise) 1000eina_promise_owner_promise_get(Eina_Promise_Owner* promise)
953{ 1001{
1002 _EINA_PROMISE_NULL_CHECK(promise, NULL);
1003 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
954 return promise->promise_get(promise); 1004 return promise->promise_get(promise);
955} 1005}
956 1006
957EAPI Eina_Bool 1007EAPI Eina_Bool
958eina_promise_owner_pending_is(Eina_Promise_Owner const* promise) 1008eina_promise_owner_pending_is(Eina_Promise_Owner const* promise)
959{ 1009{
1010 _EINA_PROMISE_NULL_CHECK(promise, EINA_FALSE);
1011 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
960 return promise->pending_is(promise); 1012 return promise->pending_is(promise);
961} 1013}
962 1014
963EAPI Eina_Bool 1015EAPI Eina_Bool
964eina_promise_owner_cancelled_is(Eina_Promise_Owner const* promise) 1016eina_promise_owner_cancelled_is(Eina_Promise_Owner const* promise)
965{ 1017{
1018 _EINA_PROMISE_NULL_CHECK(promise, EINA_FALSE);
1019 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
966 return promise->cancelled_is(promise); 1020 return promise->cancelled_is(promise);
967} 1021}
968 1022
969EAPI void 1023EAPI void
970eina_promise_owner_progress(Eina_Promise_Owner const* promise, void* progress) 1024eina_promise_owner_progress(Eina_Promise_Owner const* promise, void* progress)
971{ 1025{
1026 _EINA_PROMISE_NULL_CHECK(promise, );
1027 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
972 promise->progress(promise, progress); 1028 promise->progress(promise, progress);
973} 1029}
974 1030
@@ -976,16 +1032,20 @@ EAPI void
976eina_promise_owner_progress_notify(Eina_Promise_Owner* promise, Eina_Promise_Progress_Notify_Cb progress_cb, 1032eina_promise_owner_progress_notify(Eina_Promise_Owner* promise, Eina_Promise_Progress_Notify_Cb progress_cb,
977 void* data, Eina_Promise_Free_Cb free_cb) 1033 void* data, Eina_Promise_Free_Cb free_cb)
978{ 1034{
1035 _EINA_PROMISE_NULL_CHECK(promise, );
1036 EINA_MAGIC_CHECK_PROMISE_OWNER(promise);
979 promise->progress_notify(promise, progress_cb, data, free_cb); 1037 promise->progress_notify(promise, progress_cb, data, free_cb);
980} 1038}
981 1039
982static const char EINA_ERROR_PROMISE_NO_NOTIFY_STR[] = "Out of memory"; 1040static const char EINA_ERROR_PROMISE_NO_NOTIFY_STR[] = "Out of memory";
983static const char EINA_ERROR_PROMISE_CANCEL_STR[] = "Promise cancelled"; 1041static const char EINA_ERROR_PROMISE_CANCEL_STR[] = "Promise cancelled";
1042static const char EINA_ERROR_PROMISE_NULL_STR[] = "NULL promise";
984 1043
985Eina_Bool eina_promise_init() 1044Eina_Bool eina_promise_init()
986{ 1045{
987 EINA_ERROR_PROMISE_NO_NOTIFY = eina_error_msg_static_register(EINA_ERROR_PROMISE_NO_NOTIFY_STR); 1046 EINA_ERROR_PROMISE_NO_NOTIFY = eina_error_msg_static_register(EINA_ERROR_PROMISE_NO_NOTIFY_STR);
988 EINA_ERROR_PROMISE_CANCEL = eina_error_msg_static_register(EINA_ERROR_PROMISE_CANCEL_STR); 1047 EINA_ERROR_PROMISE_CANCEL = eina_error_msg_static_register(EINA_ERROR_PROMISE_CANCEL_STR);
1048 EINA_ERROR_PROMISE_NULL = eina_error_msg_static_register(EINA_ERROR_PROMISE_NULL_STR);
989 1049
990 _eina_promise_log_dom = eina_log_domain_register("eina_promise", 1050 _eina_promise_log_dom = eina_log_domain_register("eina_promise",
991 EINA_LOG_COLOR_DEFAULT); 1051 EINA_LOG_COLOR_DEFAULT);