summaryrefslogtreecommitdiff
path: root/src/lib/evil
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2017-12-11 15:06:02 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2017-12-11 16:04:02 +0900
commit771bb6e6a79113fe86b9bd641d1418a78aee81ae (patch)
treef1dccef88a7d64f06191a779f9e5080497bfcb57 /src/lib/evil
parent196b6f1ba48bf06a11c0017e43550583a05d00f5 (diff)
Evil: use static buffer to store error messages
Reviewers: cedric, jpeg Differential Revision: https://phab.enlightenment.org/D5394
Diffstat (limited to 'src/lib/evil')
-rw-r--r--src/lib/evil/evil_dlfcn.c3
-rw-r--r--src/lib/evil/evil_main.c40
-rw-r--r--src/lib/evil/evil_mman.c25
-rw-r--r--src/lib/evil/evil_util.c34
-rw-r--r--src/lib/evil/evil_util.h4
5 files changed, 61 insertions, 45 deletions
diff --git a/src/lib/evil/evil_dlfcn.c b/src/lib/evil/evil_dlfcn.c
index fe1dc7d..1a7db5e 100644
--- a/src/lib/evil/evil_dlfcn.c
+++ b/src/lib/evil/evil_dlfcn.c
@@ -24,7 +24,7 @@ static int _dl_err_viewed = 0;
24static void 24static void
25_dl_get_last_error(char *desc) 25_dl_get_last_error(char *desc)
26{ 26{
27 char *str; 27 const char *str;
28 size_t l1; 28 size_t l1;
29 size_t l2; 29 size_t l2;
30 30
@@ -45,7 +45,6 @@ _dl_get_last_error(char *desc)
45 memcpy(_dl_err + l1, str, l2); 45 memcpy(_dl_err + l1, str, l2);
46 _dl_err[l1 + l2] = '\0'; 46 _dl_err[l1 + l2] = '\0';
47 } 47 }
48 free(str);
49 _dl_err_viewed = 0; 48 _dl_err_viewed = 0;
50} 49}
51 50
diff --git a/src/lib/evil/evil_main.c b/src/lib/evil/evil_main.c
index 8ddd85f..875d6c5 100644
--- a/src/lib/evil/evil_main.c
+++ b/src/lib/evil/evil_main.c
@@ -22,6 +22,8 @@ extern LONGLONG _evil_time_freq;
22extern LONGLONG _evil_time_count; 22extern LONGLONG _evil_time_count;
23extern long _evil_time_second; 23extern long _evil_time_second;
24 24
25extern DWORD _evil_tls_index;
26
25long _evil_systemtime_to_time(SYSTEMTIME st); 27long _evil_systemtime_to_time(SYSTEMTIME st);
26 28
27int 29int
@@ -96,3 +98,41 @@ evil_shutdown(void)
96 98
97 return _evil_init_count; 99 return _evil_init_count;
98} 100}
101
102BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
103{
104 LPVOID data;
105
106 switch (reason)
107 {
108 case DLL_PROCESS_ATTACH:
109 _evil_tls_index = TlsAlloc();
110 if (_evil_tls_index == TLS_OUT_OF_INDEXES)
111 return FALSE;
112 case DLL_THREAD_ATTACH:
113 data = (LPVOID)LocalAlloc(LPTR, 4096);
114 if (!data)
115 return FALSE;
116 if (!TlsSetValue(_evil_tls_index, data))
117 return FALSE;
118 break;
119 case DLL_THREAD_DETACH:
120 data = TlsGetValue(_evil_tls_index);
121 if (data)
122 LocalFree((HLOCAL)data);
123 break;
124 case DLL_PROCESS_DETACH:
125 data = TlsGetValue(_evil_tls_index);
126 if (data)
127 LocalFree((HLOCAL)data);
128 TlsFree(_evil_tls_index);
129 break;
130 default:
131 break;
132 }
133
134 return TRUE;
135
136 (void)inst;
137 (void)reserved;
138}
diff --git a/src/lib/evil/evil_mman.c b/src/lib/evil/evil_mman.c
index fca3c72..3f632e5 100644
--- a/src/lib/evil/evil_mman.c
+++ b/src/lib/evil/evil_mman.c
@@ -83,12 +83,8 @@ mmap(void *addr EVIL_UNUSED,
83 fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL); 83 fm = CreateFileMapping(handle, NULL, protect, 0, 0, NULL);
84 if (!fm) 84 if (!fm)
85 { 85 {
86 char *str; 86 fprintf(stderr, "[Evil] [mmap] CreateFileMapping failed: %s\n",
87 87 evil_last_error_get());
88 str = evil_last_error_get();
89 fprintf(stderr, "[Evil] [mmap] CreateFileMapping failed: %s\n", str);
90 free(str);
91
92 return MAP_FAILED; 88 return MAP_FAILED;
93 } 89 }
94 90
@@ -117,12 +113,8 @@ mmap(void *addr EVIL_UNUSED,
117 113
118 if (!data) 114 if (!data)
119 { 115 {
120 char *str; 116 fprintf(stderr, "[Evil] [mmap] MapViewOfFile failed: %s\n",
121 117 evil_last_error_get());
122 str = evil_last_error_get();
123 fprintf(stderr, "[Evil] [mmap] MapViewOfFile failed: %s\n", str);
124 free(str);
125
126 return MAP_FAILED; 118 return MAP_FAILED;
127 } 119 }
128 120
@@ -137,13 +129,8 @@ munmap(void *addr,
137 129
138 res = UnmapViewOfFile(addr); 130 res = UnmapViewOfFile(addr);
139 if (!res) 131 if (!res)
140 { 132 fprintf(stderr, "[Evil] [munmap] UnmapViewOfFile failed: %s\n",
141 char *str; 133 evil_last_error_get());
142
143 str = evil_last_error_get();
144 fprintf(stderr, "[Evil] [munmap] UnmapViewOfFile failed: %s\n", str);
145 free(str);
146 }
147 134
148 return (res == 0) ? -1 : 0; 135 return (res == 0) ? -1 : 0;
149} 136}
diff --git a/src/lib/evil/evil_util.c b/src/lib/evil/evil_util.c
index 4f74353..36ee38e 100644
--- a/src/lib/evil/evil_util.c
+++ b/src/lib/evil/evil_util.c
@@ -17,6 +17,7 @@
17#include "evil_macro.h" 17#include "evil_macro.h"
18#include "evil_util.h" 18#include "evil_util.h"
19 19
20DWORD _evil_tls_index;
20 21
21/* static void _evil_error_display(const char *fct, LONG res); */ 22/* static void _evil_error_display(const char *fct, LONG res); */
22static void _evil_last_error_display(const char *fct); 23static void _evil_last_error_display(const char *fct);
@@ -113,12 +114,12 @@ evil_utf16_to_utf8(const wchar_t *text16)
113 return text8; 114 return text8;
114} 115}
115 116
116char * 117const char *
117evil_format_message(long err) 118evil_format_message(long err)
118{ 119{
120 char *buf;
119 LPTSTR msg; 121 LPTSTR msg;
120 char *str; 122 char *str;
121 char *disp;
122 123
123 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 124 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
124 NULL, 125 NULL,
@@ -128,10 +129,10 @@ evil_format_message(long err)
128 0, 129 0,
129 NULL)) 130 NULL))
130 { 131 {
131 char buf[4096]; 132 buf = (char *)TlsGetValue(_evil_tls_index);
132 133 snprintf(buf, 4096,
133 snprintf(buf, 4096, "FormatMessage failed with error %ld\n", GetLastError()); 134 "FormatMessage failed with error %ld\n", GetLastError());
134 return strdup(buf); 135 return (const char *)buf;
135 } 136 }
136 137
137#ifdef UNICODE 138#ifdef UNICODE
@@ -140,15 +141,8 @@ evil_format_message(long err)
140 str = msg; 141 str = msg;
141#endif /* UNICODE */ 142#endif /* UNICODE */
142 143
143 disp = (char *)malloc((strlen(str) + strlen("(00000) ") + 1) * sizeof(char)); 144 buf = (char *)TlsGetValue(_evil_tls_index);
144 if (!disp) 145 snprintf(buf, 4096, "(%5ld) %s", err, str);
145 {
146 LocalFree(msg);
147 return NULL;
148 }
149
150 snprintf(disp, strlen(str) + strlen("(00000) ") + 1,
151 "(%5ld) %s", err, str);
152 146
153#ifdef UNICODE 147#ifdef UNICODE
154 free(str); 148 free(str);
@@ -156,10 +150,10 @@ evil_format_message(long err)
156 150
157 LocalFree(msg); 151 LocalFree(msg);
158 152
159 return disp; 153 return (const char *)buf;
160} 154}
161 155
162char * 156const char *
163evil_last_error_get(void) 157evil_last_error_get(void)
164{ 158{
165 DWORD err; 159 DWORD err;
@@ -171,11 +165,7 @@ evil_last_error_get(void)
171static void 165static void
172_evil_last_error_display(const char *fct) 166_evil_last_error_display(const char *fct)
173{ 167{
174 char *error; 168 fprintf(stderr, "[Evil] [%s] ERROR: %s\n", fct, evil_last_error_get());
175
176 error = evil_last_error_get();
177 fprintf(stderr, "[Evil] [%s] ERROR: %s\n", fct, error);
178 free(error);
179} 169}
180 170
181 171
diff --git a/src/lib/evil/evil_util.h b/src/lib/evil/evil_util.h
index 8392d96..6b3e504 100644
--- a/src/lib/evil/evil_util.h
+++ b/src/lib/evil/evil_util.h
@@ -59,9 +59,9 @@ EAPI char *evil_wchar_to_char(const wchar_t *text);
59 */ 59 */
60EAPI char *evil_utf16_to_utf8(const wchar_t *text); 60EAPI char *evil_utf16_to_utf8(const wchar_t *text);
61 61
62EAPI char *evil_format_message(long err); 62EAPI const char *evil_format_message(long err);
63 63
64EAPI char *evil_last_error_get(void); 64EAPI const char *evil_last_error_get(void);
65 65
66/** 66/**
67 * @brief Return a dir to store temporary files. 67 * @brief Return a dir to store temporary files.