summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2014-01-09 16:26:18 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2014-01-09 17:37:43 +0900
commitb63675a809b1b5bb8e740b433a9b8e4e174f424e (patch)
tree364e9f11002366b04402a8ac5a5b8999ac2ab015
parent7d4209d0edc09dfa55cb40cc05b21983b241a65f (diff)
Evas/cserve2: Fix valgrind warning about uninitialized memory
In cserve2, a shortcut was taken to check if two images were the same, using memcmp() on the Evas_Image_Load_Opts struct. But it seems some empty areas in the struct are uninitialized, potentially making memcmp() fail when the images were actually the same. This is a minor issue since this function is called only when bypassing the socket wait. Also, memset load_opts to 0 and copy all the fields to avoid the same warning in socket send(). I'm just wondering about the performance impact vs. memcpy/memcmp.
-rw-r--r--src/lib/evas/cserve2/evas_cs2_client.c84
1 files changed, 72 insertions, 12 deletions
diff --git a/src/lib/evas/cserve2/evas_cs2_client.c b/src/lib/evas/cserve2/evas_cs2_client.c
index c65003f7d6..93e090ba69 100644
--- a/src/lib/evas/cserve2/evas_cs2_client.c
+++ b/src/lib/evas/cserve2/evas_cs2_client.c
@@ -929,6 +929,60 @@ _evas_image_load_opts_empty(Evas_Image_Load_Opts *lo)
929 && (lo->scale_load.scale_hint == 0)); // Skip smooth 929 && (lo->scale_load.scale_hint == 0)); // Skip smooth
930} 930}
931 931
932// Valgrind complains about uninitialized memory, because the load_opts
933// can be allocated on the stack, and each field is set independently,
934// leaving empty spaces in the struct non initialized. So, compare fields
935// one by one, don't take shortcuts like memcmp.
936
937static Eina_Bool
938_evas_image_load_opts_equal(const Evas_Image_Load_Opts *lo1,
939 const Evas_Image_Load_Opts *lo2)
940{
941 return ((lo1->scale_down_by == lo2->scale_down_by)
942 && (lo1->dpi == lo2->dpi)
943 && (lo1->w == lo2->w)
944 && (lo1->h == lo2->h)
945 && (lo1->region.x == lo2->region.x)
946 && (lo1->region.y == lo2->region.y)
947 && (lo1->region.w == lo2->region.w)
948 && (lo1->region.h == lo2->region.h)
949 && (lo1->scale_load.src_x == lo2->scale_load.src_x)
950 && (lo1->scale_load.src_y == lo2->scale_load.src_y)
951 && (lo1->scale_load.src_w == lo2->scale_load.src_w)
952 && (lo1->scale_load.src_h == lo2->scale_load.src_h)
953 && (lo1->scale_load.dst_w == lo2->scale_load.dst_w)
954 && (lo1->scale_load.dst_h == lo2->scale_load.dst_h)
955 && (lo1->scale_load.smooth == lo2->scale_load.smooth)
956 && (lo1->scale_load.scale_hint == lo2->scale_load.scale_hint)
957 && (lo1->orientation == lo2->orientation)
958 && (lo1->degree == lo2->degree));
959}
960
961static void
962_evas_image_load_opts_set(Evas_Image_Load_Opts *lo1,
963 const Evas_Image_Load_Opts *lo2)
964{
965 memset(lo1, 0, sizeof(Evas_Image_Load_Opts));
966 lo1->scale_down_by = lo2->scale_down_by;
967 lo1->dpi = lo2->dpi;
968 lo1->w = lo2->w;
969 lo1->h = lo2->h;
970 lo1->region.x = lo2->region.x;
971 lo1->region.y = lo2->region.y;
972 lo1->region.w = lo2->region.w;
973 lo1->region.h = lo2->region.h;
974 lo1->scale_load.src_x = lo2->scale_load.src_x;
975 lo1->scale_load.src_y = lo2->scale_load.src_y;
976 lo1->scale_load.src_w = lo2->scale_load.src_w;
977 lo1->scale_load.src_h = lo2->scale_load.src_h;
978 lo1->scale_load.dst_w = lo2->scale_load.dst_w;
979 lo1->scale_load.dst_h = lo2->scale_load.dst_h;
980 lo1->scale_load.smooth = lo2->scale_load.smooth;
981 lo1->scale_load.scale_hint = lo2->scale_load.scale_hint;
982 lo1->orientation = lo2->orientation;
983 lo1->degree = lo2->degree;
984}
985
932static void 986static void
933_file_hkey_get(char *buf, size_t sz, const char *path, const char *key, 987_file_hkey_get(char *buf, size_t sz, const char *path, const char *key,
934 Evas_Image_Load_Opts *lo) 988 Evas_Image_Load_Opts *lo)
@@ -968,7 +1022,8 @@ _image_open_server_send(Image_Entry *ie)
968 File_Entry *fentry; 1022 File_Entry *fentry;
969 Data_Entry *dentry; 1023 Data_Entry *dentry;
970 const char *file, *key; 1024 const char *file, *key;
971 Evas_Image_Load_Opts *opts = NULL; 1025 Evas_Image_Load_Opts opts;
1026 Eina_Bool has_load_opts = EINA_FALSE;
972 1027
973 if (cserve2_init == 0) 1028 if (cserve2_init == 0)
974 { 1029 {
@@ -977,7 +1032,10 @@ _image_open_server_send(Image_Entry *ie)
977 } 1032 }
978 1033
979 if (!_evas_image_load_opts_empty(&ie->load_opts)) 1034 if (!_evas_image_load_opts_empty(&ie->load_opts))
980 opts = &ie->load_opts; 1035 {
1036 _evas_image_load_opts_set(&opts, &ie->load_opts);
1037 has_load_opts = EINA_TRUE;
1038 }
981 1039
982 ie->data1 = NULL; 1040 ie->data1 = NULL;
983 ie->data2 = NULL; 1041 ie->data2 = NULL;
@@ -998,7 +1056,7 @@ _image_open_server_send(Image_Entry *ie)
998 1056
999 hkey_len = flen + klen + 1024; 1057 hkey_len = flen + klen + 1024;
1000 hkey = alloca(hkey_len); 1058 hkey = alloca(hkey_len);
1001 _file_hkey_get(hkey, hkey_len, filebuf, key, opts); 1059 _file_hkey_get(hkey, hkey_len, filebuf, key, (has_load_opts ? &opts : NULL));
1002 fentry = eina_hash_find(_file_entries, hkey); 1060 fentry = eina_hash_find(_file_entries, hkey);
1003 if (!fentry) 1061 if (!fentry)
1004 { 1062 {
@@ -1023,18 +1081,17 @@ _image_open_server_send(Image_Entry *ie)
1023 } 1081 }
1024 1082
1025 memset(&msg_open, 0, sizeof(msg_open)); 1083 memset(&msg_open, 0, sizeof(msg_open));
1026
1027 msg_open.base.rid = _next_rid(); 1084 msg_open.base.rid = _next_rid();
1028 msg_open.base.type = CSERVE2_OPEN; 1085 msg_open.base.type = CSERVE2_OPEN;
1029 msg_open.file_id = fentry->file_id; 1086 msg_open.file_id = fentry->file_id;
1030 msg_open.path_offset = 0; 1087 msg_open.path_offset = 0;
1031 msg_open.key_offset = flen; 1088 msg_open.key_offset = flen;
1032 msg_open.has_load_opts = (opts != NULL); 1089 msg_open.has_load_opts = has_load_opts;
1033 msg_open.image_id = ++_data_id; 1090 msg_open.image_id = ++_data_id;
1034 1091
1035 size = sizeof(msg_open) + flen + klen; 1092 size = sizeof(msg_open) + flen + klen;
1036 if (opts) 1093 if (has_load_opts)
1037 size += sizeof(*opts); 1094 size += sizeof(opts);
1038 buf = malloc(size); 1095 buf = malloc(size);
1039 if (!buf) 1096 if (!buf)
1040 { 1097 {
@@ -1046,8 +1103,8 @@ _image_open_server_send(Image_Entry *ie)
1046 memcpy(buf, &msg_open, sizeof(msg_open)); 1103 memcpy(buf, &msg_open, sizeof(msg_open));
1047 memcpy(buf + sizeof(msg_open), filebuf, flen); 1104 memcpy(buf + sizeof(msg_open), filebuf, flen);
1048 memcpy(buf + sizeof(msg_open) + flen, key, klen); 1105 memcpy(buf + sizeof(msg_open) + flen, key, klen);
1049 if (opts) 1106 if (has_load_opts)
1050 memcpy(buf + sizeof(msg_open) + flen + klen, opts, sizeof(*opts)); 1107 memcpy(buf + sizeof(msg_open) + flen + klen, &opts, sizeof(opts));
1051 1108
1052 if (!_server_send(buf, size, _image_opened_cb, ie)) 1109 if (!_server_send(buf, size, _image_opened_cb, ie))
1053 { 1110 {
@@ -2782,9 +2839,12 @@ _shared_file_data_get_by_id(unsigned int id)
2782static inline Eina_Bool 2839static inline Eina_Bool
2783_shared_image_entry_image_data_match(Image_Entry *ie, const Image_Data *id) 2840_shared_image_entry_image_data_match(Image_Entry *ie, const Image_Data *id)
2784{ 2841{
2785 int cmp; 2842 const Evas_Image_Load_Opts *lo1, *lo2;
2786 cmp = memcmp(&ie->load_opts, &id->opts, sizeof(ie->load_opts)); 2843
2787 if (!cmp) 2844 lo1 = &ie->load_opts;
2845 lo2 = &id->opts;
2846
2847 if (_evas_image_load_opts_equal(lo1, lo2))
2788 { 2848 {
2789 DBG("Found loaded image entry at %d", id->id); 2849 DBG("Found loaded image entry at %d", id->id);
2790 return EINA_TRUE; 2850 return EINA_TRUE;