From a79c380707516e525176b8961b13dae44e87f08b Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Thu, 7 Feb 2013 14:45:12 +0200 Subject: [PATCH] Remove strange _tcslen define and fixed some potential leaks. --- src/std/c_dir.c | 33 +++++++++++++++++++++++---------- src/std/c_private.h | 2 -- src/std/c_string.c | 2 +- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/std/c_dir.c b/src/std/c_dir.c index 6462e9606b..d946745b6c 100644 --- a/src/std/c_dir.c +++ b/src/std/c_dir.c @@ -26,6 +26,7 @@ int c_mkdirs(const char *path, mode_t mode) { if (_tstat(wpath, &sb) == 0) { if (! S_ISDIR(sb.st_mode)) { errno = ENOTDIR; + c_free_multibyte(wpath); return -1; } } @@ -43,13 +44,17 @@ int c_mkdirs(const char *path, mode_t mode) { if (_tstat(swpath, &sb) == 0) { if (! S_ISDIR(sb.st_mode)) { errno = ENOTDIR; + c_free_multibyte(swpath); + c_free_multibyte(wpath); return -1; } } else if (errno != ENOENT) { c_free_multibyte(swpath); + c_free_multibyte(wpath); return -1; } else if (c_mkdirs(subpath, mode) < 0) { c_free_multibyte(swpath); + c_free_multibyte(wpath); return -1; } } @@ -74,7 +79,8 @@ int c_rmdirs(const char *path) { char *fname = NULL; _TCHAR *wfname = NULL; _TCHAR *wpath = c_multibyte(path); - + char *rd_name = NULL; + if ((d = _topendir(wpath)) != NULL) { while( _tstat(wpath, &sb) == 0) { /* if we can remove the directory we're done */ @@ -87,27 +93,30 @@ int c_rmdirs(const char *path) { case EBADF: break; /* continue */ default: + c_free_multibyte(wpath); _tclosedir(d); return 0; } while ((dp = _treaddir(d)) != NULL) { size_t len; + rd_name = c_utf8(dp->d_name); /* skip '.' and '..' */ - if (dp->d_name[0] == '.' && - (dp->d_name[1] == '\0' || - (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) { - continue; + if( c_streq( rd_name, "." ) || c_streq( rd_name, ".." ) ) { + c_free_utf8(rd_name); + continue; } - len = strlen(path) + _tcslen(dp->d_name) + 2; + len = strlen(path) + strlen(rd_name) + 2; fname = c_malloc(len); if (fname == NULL) { + c_free_multibyte(wpath); + c_free_utf8(rd_name); _tclosedir(d); return -1; } - snprintf(fname, len, "%s/%s", path, dp->d_name); - wfname = c_multibyte(fname); + snprintf(fname, len, "%s/%s", path, rd_name); + wfname = c_multibyte(fname); /* stat the file */ if (_tstat(wfname, &sb) != -1) { @@ -120,7 +129,9 @@ int c_rmdirs(const char *path) { if (errno == EACCES) { _tclosedir(d); SAFE_FREE(fname); - c_free_multibyte(wfname); + c_free_multibyte(wpath); + c_free_multibyte(wfname); + c_free_utf8(rd_name); return -1; } c_rmdirs(fname); @@ -130,7 +141,8 @@ int c_rmdirs(const char *path) { } } /* lstat */ SAFE_FREE(fname); - c_free_multibyte(wfname); + c_free_multibyte(wfname); + c_free_utf8(rd_name); } /* readdir */ _trewinddir(d); @@ -138,6 +150,7 @@ int c_rmdirs(const char *path) { } else { return -1; } + c_free_multibyte(wpath); _tclosedir(d); return 0; diff --git a/src/std/c_private.h b/src/std/c_private.h index e86370ed7e..aec65029ba 100644 --- a/src/std/c_private.h +++ b/src/std/c_private.h @@ -77,7 +77,6 @@ typedef struct stat csync_stat_t; #if defined _WIN32 && defined _UNICODE typedef wchar_t _TCHAR; -#define _tcslen wcslen #define _topen _wopen #define _tdirent _wdirent #define _TDIR _WDIR @@ -97,7 +96,6 @@ typedef wchar_t _TCHAR; #else typedef char _TCHAR; #define _tdirent dirent -#define _tcslen strlen #define _topen open #define _TDIR DIR #define _topendir opendir diff --git a/src/std/c_string.c b/src/std/c_string.c index 4b8b2977da..0e673725fb 100644 --- a/src/std/c_string.c +++ b/src/std/c_string.c @@ -281,7 +281,7 @@ char* c_utf8(const _TCHAR *wstr) char *dst = NULL; #ifdef _WIN32 if(!wstr) return NULL; - size_t len = _tcslen( wstr ); + size_t len = wcslen( wstr ); /* Call once to get the required size. */ int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL); if( size_needed > 0 ) {