... | ... |
@@ -127,7 +127,7 @@ static int scanfile(const char *filename, struct cl_engine *engine, const struct |
127 | 127 |
|
128 | 128 |
logg("*Scanning %s\n", filename); |
129 | 129 |
|
130 |
- if((fd = open(filename, O_RDONLY|O_BINARY)) == -1) { |
|
130 |
+ if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1) { |
|
131 | 131 |
logg("^Can't open file %s: %s\n", filename, strerror(errno)); |
132 | 132 |
return 54; |
133 | 133 |
} |
... | ... |
@@ -2221,7 +2221,7 @@ static int cli_scanfile(const char *filename, cli_ctx *ctx) |
2221 | 2221 |
int fd, ret; |
2222 | 2222 |
|
2223 | 2223 |
/* internal version of cl_scanfile with arec/mrec preserved */ |
2224 |
- if((fd = open(filename, O_RDONLY|O_BINARY)) == -1) |
|
2224 |
+ if((fd = safe_open(filename, O_RDONLY|O_BINARY)) == -1) |
|
2225 | 2225 |
return CL_EOPEN; |
2226 | 2226 |
|
2227 | 2227 |
ret = cli_magic_scandesc(fd, ctx); |
... | ... |
@@ -19,11 +19,14 @@ |
19 | 19 |
*/ |
20 | 20 |
|
21 | 21 |
#include <errno.h> |
22 |
+#include <sys/types.h> |
|
23 |
+#include <sys/stat.h> |
|
24 |
+#include <fcntl.h> |
|
25 |
+#include <io.h> |
|
22 | 26 |
|
23 | 27 |
#include "others.h" |
24 | 28 |
#include "shared/misc.h" |
25 | 29 |
|
26 |
- |
|
27 | 30 |
wchar_t *uncpath(const char *path) { |
28 | 31 |
DWORD len = 0; |
29 | 32 |
wchar_t *dest = cli_malloc((PATH_MAX + 1) * sizeof(wchar_t)); |
... | ... |
@@ -69,6 +72,26 @@ wchar_t *uncpath(const char *path) { |
69 | 69 |
return dest; |
70 | 70 |
} |
71 | 71 |
|
72 |
+int safe_open(const char *path, int flags, ... ) { |
|
73 |
+ wchar_t *wpath = uncpath(path); |
|
74 |
+ int ret; |
|
75 |
+ |
|
76 |
+ if(!wpath) |
|
77 |
+ return -1; |
|
78 |
+ |
|
79 |
+ if(flags & O_CREAT) { |
|
80 |
+ int mode; |
|
81 |
+ va_list ap; |
|
82 |
+ va_start(ap, flags); |
|
83 |
+ mode = va_arg(ap, int); |
|
84 |
+ va_end(ap); |
|
85 |
+ ret = _wopen(wpath, flags, mode); |
|
86 |
+ } else |
|
87 |
+ ret = _wopen(wpath, flags); |
|
88 |
+ free(wpath); |
|
89 |
+ return ret; |
|
90 |
+} |
|
91 |
+ |
|
72 | 92 |
|
73 | 93 |
w32_stat(const char *path, struct stat *buf) { |
74 | 94 |
int len; |