Windows crash in ctime_r()
authorMichael Schoberg (mschoberg) <mschoberg@micron.com>
Mon, 16 May 2016 21:50:19 +0000 (21:50 +0000)
committerJens Axboe <axboe@fb.com>
Tue, 17 May 2016 01:25:48 +0000 (19:25 -0600)
I think I found an issue in os\windows\posix.c that results in a FIO
crash (on Windows.)  I'm including a patch that resolves the crash for
us, but includes another (optional) fix.

Crash issue:  possix.c  - ctime_r() will reference a negative array
index on Sunday.  SYSTEMTIME states the days of the week as: "0=Sunday,
.. , 6=Saturday."  The "fix" can likely be dialed back to safely assume
the days/months will adhere to how they're documented.

Optional - StringCchPrintfA() calls should allow for the string plus a
NULL character.  Instead, the value getting passed in is for the entire
string size.

os/windows/posix.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Signed-off-by: Jens Axboe <axboe@fb.com>
os/windows/posix.c

index 41fc480df9f14f92a75ff07a5fda30fa098deb06..fd3d9ab38111c0ecd46f86c664b62a7ec15bb145 100755 (executable)
@@ -243,12 +243,12 @@ void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
 char* ctime_r(const time_t *t, char *buf)
 {
     SYSTEMTIME systime;
 char* ctime_r(const time_t *t, char *buf)
 {
     SYSTEMTIME systime;
-    const char * const dayOfWeek[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
+    const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
     const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
     Time_tToSystemTime(*t, &systime);
     /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
     const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
     Time_tToSystemTime(*t, &systime);
     /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
-    StringCchPrintfA(buf, 32, "%s %s %d %02d:%02d:%02d %04d", dayOfWeek[systime.wDayOfWeek - 1], monthOfYear[systime.wMonth - 1],
+    StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d", dayOfWeek[systime.wDayOfWeek % 7], monthOfYear[(systime.wMonth - 1) % 12],
                                                                                 systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
     return buf;
 }
                                                                                 systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
     return buf;
 }
@@ -888,7 +888,7 @@ struct dirent *readdir(DIR *dirp)
 
        if (dirp->find_handle == INVALID_HANDLE_VALUE) {
                char search_pattern[MAX_PATH];
 
        if (dirp->find_handle == INVALID_HANDLE_VALUE) {
                char search_pattern[MAX_PATH];
-               StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
+               StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
                dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
                if (dirp->find_handle == INVALID_HANDLE_VALUE)
                        return NULL;
                dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
                if (dirp->find_handle == INVALID_HANDLE_VALUE)
                        return NULL;