diff options
author | Reimar Döffinger <Reimar.Doeffinger@gmx.de> | 2013-03-10 23:03:19 +0400 |
---|---|---|
committer | Martin Storsjö <martin@martin.st> | 2013-03-27 20:54:46 +0400 |
commit | ad04025987e5c926ae5bde8947a44b3f1346e64c (patch) | |
tree | 76973d99b90da7fc434589695acf5bba3f9979a9 /libavformat | |
parent | e9cc98839574c7e8d546e890ebbf57d1766e5d8a (diff) |
win32: Make ff_win32_open more robust
- Make MultiByteToWideChar fail when it encounters invalid encoding.
Without this, invalid characters might just be skipped
- When MultiByteToWideChar fails, assume the file name is in CP_ACP
and open it via normal open function, even when the file will be
written
- When malloc fails return error instead of crashing
Signed-off-by: Martin Storsjö <martin@martin.st>
Diffstat (limited to 'libavformat')
-rw-r--r-- | libavformat/os_support.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/libavformat/os_support.c b/libavformat/os_support.c index 1ecf43c3dc..30581ed7c2 100644 --- a/libavformat/os_support.c +++ b/libavformat/os_support.c @@ -33,6 +33,7 @@ #include <io.h> #include <windows.h> #include <share.h> +#include <errno.h> int ff_win32_open(const char *filename_utf8, int oflag, int pmode) { @@ -41,20 +42,25 @@ int ff_win32_open(const char *filename_utf8, int oflag, int pmode) wchar_t *filename_w; /* convert UTF-8 to wide chars */ - num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0); + num_chars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename_utf8, -1, NULL, 0); if (num_chars <= 0) - return -1; + goto fallback; filename_w = av_mallocz(sizeof(wchar_t) * num_chars); + if (!filename_w) { + errno = ENOMEM; + return -1; + } MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars); fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode); av_freep(&filename_w); - /* filename maybe be in CP_ACP */ - if (fd == -1 && !(oflag & O_CREAT)) - return _sopen(filename_utf8, oflag, SH_DENYNO, pmode); + if (fd != -1 || (oflag & O_CREAT)) + return fd; - return fd; +fallback: + /* filename may be be in CP_ACP */ + return _sopen(filename_utf8, oflag, SH_DENYNO, pmode); } #endif |