Browse code

win32/freshclam: fix IMS mode crash

Don't crash if gmtime is NULL (for example out of range).
FileTime is not time_t, conversion is needed, otherwise gmtime returns NULL.

Török Edvin authored on 2011/05/12 04:02:47
Showing 2 changed files
... ...
@@ -758,6 +758,10 @@ static int Rfc2822DateTime(char *buf, time_t mtime)
758 758
 	struct tm *gmt;
759 759
 
760 760
     gmt = gmtime(&mtime);
761
+    if (!gmt) {
762
+	logg("gmtime: %s\n", strerror(errno));
763
+	return "";
764
+    }
761 765
     return strftime(buf, 36, "%a, %d %b %Y %X GMT", gmt);
762 766
 }
763 767
 
... ...
@@ -141,6 +141,12 @@ int safe_open(const char *path, int flags, ... ) {
141 141
     return ret;
142 142
 }
143 143
 
144
+static time_t FileTimeToUnixTime(FILETIME t)
145
+{
146
+	LONGLONG ll = ((LONGLONG)t.dwHighDateTime << 32) | t.dwLowDateTime;
147
+	ll -= 116444736000000000;
148
+	return (time_t)(ll/10000000);
149
+}
144 150
 
145 151
 w32_stat(const char *path, struct stat *buf) {
146 152
     int len;
... ...
@@ -162,12 +168,13 @@ w32_stat(const char *path, struct stat *buf) {
162 162
     buf->st_uid = 0;
163 163
     buf->st_gid = 0;
164 164
     buf->st_ino = 1;
165
-    buf->st_atime = ((time_t)attrs.ftLastAccessTime.dwHighDateTime<<32) | attrs.ftLastAccessTime.dwLowDateTime;
166
-    buf->st_ctime = ((time_t)attrs.ftCreationTime.dwHighDateTime<<32) | attrs.ftCreationTime.dwLowDateTime;
167
-    buf->st_mtime = ((time_t)attrs.ftLastWriteTime.dwHighDateTime<<32) | attrs.ftLastWriteTime.dwLowDateTime;
165
+    buf->st_atime = FileTimeToUnixTime(attrs.ftLastAccessTime);
166
+    buf->st_ctime = FileTimeToUnixTime(attrs.ftCreationTime);
167
+    buf->st_mtime = FileTimeToUnixTime(attrs.ftLastWriteTime);
168 168
     buf->st_mode = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? S_IRUSR: S_IRWXU;
169 169
     buf->st_mode |= (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR :  _S_IFREG;
170 170
     buf->st_nlink = 1;
171 171
     buf->st_size = ((uint64_t)attrs.nFileSizeHigh << (sizeof(attrs.nFileSizeLow)*8)) | attrs.nFileSizeLow;
172 172
     return 0;
173 173
 }
174
+