diff options
author | Jean-Philippe Andre <jp.andre@samsung.com> | 2014-01-09 16:26:18 +0900 |
---|---|---|
committer | Jean-Philippe Andre <jp.andre@samsung.com> | 2014-01-09 17:35:35 +0900 |
commit | b84787bf5751152419f5994093b9f1e01a416913 (patch) | |
tree | 4fed5ab513839111566d5d075d282d7d0ca43338 /src/lib/evas/cserve2 | |
parent | 3b06d11fdfdfbdf64bceaee8bc4a9685b1941b9c (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.
Diffstat (limited to 'src/lib/evas/cserve2')
-rw-r--r-- | src/lib/evas/cserve2/evas_cs2_client.c | 84 |
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 ab6c30eeeb..ed25b0f580 100644 --- a/src/lib/evas/cserve2/evas_cs2_client.c +++ b/src/lib/evas/cserve2/evas_cs2_client.c | |||
@@ -942,6 +942,60 @@ _evas_image_load_opts_empty(Evas_Image_Load_Opts *lo) | |||
942 | && (lo->scale_load.scale_hint == 0)); // Skip smooth | 942 | && (lo->scale_load.scale_hint == 0)); // Skip smooth |
943 | } | 943 | } |
944 | 944 | ||
945 | // Valgrind complains about uninitialized memory, because the load_opts | ||
946 | // can be allocated on the stack, and each field is set independently, | ||
947 | // leaving empty spaces in the struct non initialized. So, compare fields | ||
948 | // one by one, don't take shortcuts like memcmp. | ||
949 | |||
950 | static Eina_Bool | ||
951 | _evas_image_load_opts_equal(const Evas_Image_Load_Opts *lo1, | ||
952 | const Evas_Image_Load_Opts *lo2) | ||
953 | { | ||
954 | return ((lo1->scale_down_by == lo2->scale_down_by) | ||
955 | && (lo1->dpi == lo2->dpi) | ||
956 | && (lo1->w == lo2->w) | ||
957 | && (lo1->h == lo2->h) | ||
958 | && (lo1->region.x == lo2->region.x) | ||
959 | && (lo1->region.y == lo2->region.y) | ||
960 | && (lo1->region.w == lo2->region.w) | ||
961 | && (lo1->region.h == lo2->region.h) | ||
962 | && (lo1->scale_load.src_x == lo2->scale_load.src_x) | ||
963 | && (lo1->scale_load.src_y == lo2->scale_load.src_y) | ||
964 | && (lo1->scale_load.src_w == lo2->scale_load.src_w) | ||
965 | && (lo1->scale_load.src_h == lo2->scale_load.src_h) | ||
966 | && (lo1->scale_load.dst_w == lo2->scale_load.dst_w) | ||
967 | && (lo1->scale_load.dst_h == lo2->scale_load.dst_h) | ||
968 | && (lo1->scale_load.smooth == lo2->scale_load.smooth) | ||
969 | && (lo1->scale_load.scale_hint == lo2->scale_load.scale_hint) | ||
970 | && (lo1->orientation == lo2->orientation) | ||
971 | && (lo1->degree == lo2->degree)); | ||
972 | } | ||
973 | |||
974 | static void | ||
975 | _evas_image_load_opts_set(Evas_Image_Load_Opts *lo1, | ||
976 | const Evas_Image_Load_Opts *lo2) | ||
977 | { | ||
978 | memset(lo1, 0, sizeof(Evas_Image_Load_Opts)); | ||
979 | lo1->scale_down_by = lo2->scale_down_by; | ||
980 | lo1->dpi = lo2->dpi; | ||
981 | lo1->w = lo2->w; | ||
982 | lo1->h = lo2->h; | ||
983 | lo1->region.x = lo2->region.x; | ||
984 | lo1->region.y = lo2->region.y; | ||
985 | lo1->region.w = lo2->region.w; | ||
986 | lo1->region.h = lo2->region.h; | ||
987 | lo1->scale_load.src_x = lo2->scale_load.src_x; | ||
988 | lo1->scale_load.src_y = lo2->scale_load.src_y; | ||
989 | lo1->scale_load.src_w = lo2->scale_load.src_w; | ||
990 | lo1->scale_load.src_h = lo2->scale_load.src_h; | ||
991 | lo1->scale_load.dst_w = lo2->scale_load.dst_w; | ||
992 | lo1->scale_load.dst_h = lo2->scale_load.dst_h; | ||
993 | lo1->scale_load.smooth = lo2->scale_load.smooth; | ||
994 | lo1->scale_load.scale_hint = lo2->scale_load.scale_hint; | ||
995 | lo1->orientation = lo2->orientation; | ||
996 | lo1->degree = lo2->degree; | ||
997 | } | ||
998 | |||
945 | static void | 999 | static void |
946 | _file_hkey_get(char *buf, size_t sz, const char *path, const char *key, | 1000 | _file_hkey_get(char *buf, size_t sz, const char *path, const char *key, |
947 | Evas_Image_Load_Opts *lo) | 1001 | Evas_Image_Load_Opts *lo) |
@@ -981,7 +1035,8 @@ _image_open_server_send(Image_Entry *ie) | |||
981 | File_Entry *fentry; | 1035 | File_Entry *fentry; |
982 | Data_Entry *dentry; | 1036 | Data_Entry *dentry; |
983 | const char *file, *key; | 1037 | const char *file, *key; |
984 | Evas_Image_Load_Opts *opts = NULL; | 1038 | Evas_Image_Load_Opts opts; |
1039 | Eina_Bool has_load_opts = EINA_FALSE; | ||
985 | 1040 | ||
986 | if (cserve2_init == 0) | 1041 | if (cserve2_init == 0) |
987 | { | 1042 | { |
@@ -990,7 +1045,10 @@ _image_open_server_send(Image_Entry *ie) | |||
990 | } | 1045 | } |
991 | 1046 | ||
992 | if (!_evas_image_load_opts_empty(&ie->load_opts)) | 1047 | if (!_evas_image_load_opts_empty(&ie->load_opts)) |
993 | opts = &ie->load_opts; | 1048 | { |
1049 | _evas_image_load_opts_set(&opts, &ie->load_opts); | ||
1050 | has_load_opts = EINA_TRUE; | ||
1051 | } | ||
994 | 1052 | ||
995 | ie->data1 = NULL; | 1053 | ie->data1 = NULL; |
996 | ie->data2 = NULL; | 1054 | ie->data2 = NULL; |
@@ -1011,7 +1069,7 @@ _image_open_server_send(Image_Entry *ie) | |||
1011 | 1069 | ||
1012 | hkey_len = flen + klen + 1024; | 1070 | hkey_len = flen + klen + 1024; |
1013 | hkey = alloca(hkey_len); | 1071 | hkey = alloca(hkey_len); |
1014 | _file_hkey_get(hkey, hkey_len, filebuf, key, opts); | 1072 | _file_hkey_get(hkey, hkey_len, filebuf, key, (has_load_opts ? &opts : NULL)); |
1015 | fentry = eina_hash_find(_file_entries, hkey); | 1073 | fentry = eina_hash_find(_file_entries, hkey); |
1016 | if (!fentry) | 1074 | if (!fentry) |
1017 | { | 1075 | { |
@@ -1036,18 +1094,17 @@ _image_open_server_send(Image_Entry *ie) | |||
1036 | } | 1094 | } |
1037 | 1095 | ||
1038 | memset(&msg_open, 0, sizeof(msg_open)); | 1096 | memset(&msg_open, 0, sizeof(msg_open)); |
1039 | |||
1040 | msg_open.base.rid = _next_rid(); | 1097 | msg_open.base.rid = _next_rid(); |
1041 | msg_open.base.type = CSERVE2_OPEN; | 1098 | msg_open.base.type = CSERVE2_OPEN; |
1042 | msg_open.file_id = fentry->file_id; | 1099 | msg_open.file_id = fentry->file_id; |
1043 | msg_open.path_offset = 0; | 1100 | msg_open.path_offset = 0; |
1044 | msg_open.key_offset = flen; | 1101 | msg_open.key_offset = flen; |
1045 | msg_open.has_load_opts = (opts != NULL); | 1102 | msg_open.has_load_opts = has_load_opts; |
1046 | msg_open.image_id = ++_data_id; | 1103 | msg_open.image_id = ++_data_id; |
1047 | 1104 | ||
1048 | size = sizeof(msg_open) + flen + klen; | 1105 | size = sizeof(msg_open) + flen + klen; |
1049 | if (opts) | 1106 | if (has_load_opts) |
1050 | size += sizeof(*opts); | 1107 | size += sizeof(opts); |
1051 | buf = malloc(size); | 1108 | buf = malloc(size); |
1052 | if (!buf) | 1109 | if (!buf) |
1053 | { | 1110 | { |
@@ -1059,8 +1116,8 @@ _image_open_server_send(Image_Entry *ie) | |||
1059 | memcpy(buf, &msg_open, sizeof(msg_open)); | 1116 | memcpy(buf, &msg_open, sizeof(msg_open)); |
1060 | memcpy(buf + sizeof(msg_open), filebuf, flen); | 1117 | memcpy(buf + sizeof(msg_open), filebuf, flen); |
1061 | memcpy(buf + sizeof(msg_open) + flen, key, klen); | 1118 | memcpy(buf + sizeof(msg_open) + flen, key, klen); |
1062 | if (opts) | 1119 | if (has_load_opts) |
1063 | memcpy(buf + sizeof(msg_open) + flen + klen, opts, sizeof(*opts)); | 1120 | memcpy(buf + sizeof(msg_open) + flen + klen, &opts, sizeof(opts)); |
1064 | 1121 | ||
1065 | if (!_server_send(buf, size, _image_opened_cb, ie)) | 1122 | if (!_server_send(buf, size, _image_opened_cb, ie)) |
1066 | { | 1123 | { |
@@ -2798,9 +2855,12 @@ _shared_file_data_get_by_id(unsigned int id) | |||
2798 | static inline Eina_Bool | 2855 | static inline Eina_Bool |
2799 | _shared_image_entry_image_data_match(Image_Entry *ie, const Image_Data *id) | 2856 | _shared_image_entry_image_data_match(Image_Entry *ie, const Image_Data *id) |
2800 | { | 2857 | { |
2801 | int cmp; | 2858 | const Evas_Image_Load_Opts *lo1, *lo2; |
2802 | cmp = memcmp(&ie->load_opts, &id->opts, sizeof(ie->load_opts)); | 2859 | |
2803 | if (!cmp) | 2860 | lo1 = &ie->load_opts; |
2861 | lo2 = &id->opts; | ||
2862 | |||
2863 | if (_evas_image_load_opts_equal(lo1, lo2)) | ||
2804 | { | 2864 | { |
2805 | DBG("Found loaded image entry at %d", id->id); | 2865 | DBG("Found loaded image entry at %d", id->id); |
2806 | return EINA_TRUE; | 2866 | return EINA_TRUE; |