Skip to content

Commit 4a77ce2

Browse files
authored
Remove unnecessary Windows specific time formatting (GH-15474)
`gettimeofday()` is supported by PHP on Windows for ages; and generally `localtime()` is supported on Windows for a long time. As such, there is no need for the Windows specific formatting code. However, the general Windows caveat regarding `time_t` applies, namely that it is usually `__time64_t`, unless `_USE_32BIT_TIME_T` is declared, what we do for 32bit architectures, in which case it is `__time32_t`. Now, `struct timeval` is imported from WinSock2.h, where the members are declared as long (i.e. 32bit on both x86 and x64). That means passing a pointer to `tv_sec` to `localtime()` likely fails on x64, or at least doesn't yield the desired result. Therefore, we assign `tv_sec` to an appropriate `time_t` variable, and also make sure that the `time_buffer` is zero-terminated even if the `localtime()` call still fails.
1 parent f16cb18 commit 4a77ce2

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

ext/mysqlnd/mysqlnd_debug.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,22 @@ MYSQLND_METHOD(mysqlnd_debug, log)(MYSQLND_DEBUG * self,
7676
}
7777
if (flags & MYSQLND_DEBUG_DUMP_TIME) {
7878
/* The following from FF's DBUG library, which is in the public domain */
79-
#ifdef PHP_WIN32
80-
/* FIXME This doesn't give microseconds as in Unix case, and the resolution is
81-
in system ticks, 10 ms intervals. See my_getsystime.c for high res */
82-
SYSTEMTIME loc_t;
83-
GetLocalTime(&loc_t);
84-
snprintf(time_buffer, sizeof(time_buffer) - 1,
85-
/* "%04d-%02d-%02d " */
86-
"%02d:%02d:%02d.%06d ",
87-
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
88-
loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
89-
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
90-
#else
9179
struct timeval tv;
9280
struct tm *tm_p;
9381
if (gettimeofday(&tv, NULL) != -1) {
94-
if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
82+
const time_t sec = tv.tv_sec;
83+
if ((tm_p = localtime((const time_t *)&sec))) {
9584
snprintf(time_buffer, sizeof(time_buffer) - 1,
9685
/* "%04d-%02d-%02d " */
9786
"%02d:%02d:%02d.%06d ",
9887
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
9988
tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
10089
(int) (tv.tv_usec));
10190
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
91+
} else {
92+
time_buffer[0] = '\0';
10293
}
10394
}
104-
#endif
10595
}
10696
if (flags & MYSQLND_DEBUG_DUMP_FILE) {
10797
snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);
@@ -173,32 +163,22 @@ MYSQLND_METHOD(mysqlnd_debug, log_va)(MYSQLND_DEBUG *self,
173163
}
174164
if (flags & MYSQLND_DEBUG_DUMP_TIME) {
175165
/* The following from FF's DBUG library, which is in the public domain */
176-
#ifdef PHP_WIN32
177-
/* FIXME This doesn't give microseconds as in Unix case, and the resolution is
178-
in system ticks, 10 ms intervals. See my_getsystime.c for high res */
179-
SYSTEMTIME loc_t;
180-
GetLocalTime(&loc_t);
181-
snprintf(time_buffer, sizeof(time_buffer) - 1,
182-
/* "%04d-%02d-%02d " */
183-
"%02d:%02d:%02d.%06d ",
184-
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
185-
loc_t.wHour, loc_t.wMinute, loc_t.wSecond, loc_t.wMilliseconds);
186-
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
187-
#else
188166
struct timeval tv;
189167
struct tm *tm_p;
190168
if (gettimeofday(&tv, NULL) != -1) {
191-
if ((tm_p= localtime((const time_t *)&tv.tv_sec))) {
169+
const time_t sec = tv.tv_sec;
170+
if ((tm_p = localtime((const time_t *)&sec))) {
192171
snprintf(time_buffer, sizeof(time_buffer) - 1,
193172
/* "%04d-%02d-%02d " */
194173
"%02d:%02d:%02d.%06d ",
195174
/*tm_p->tm_year + 1900, tm_p->tm_mon + 1, tm_p->tm_mday,*/
196175
tm_p->tm_hour, tm_p->tm_min, tm_p->tm_sec,
197176
(int) (tv.tv_usec));
198177
time_buffer[sizeof(time_buffer) - 1 ] = '\0';
178+
} else {
179+
time_buffer[0] = '\0';
199180
}
200181
}
201-
#endif
202182
}
203183
if (flags & MYSQLND_DEBUG_DUMP_FILE) {
204184
snprintf(file_buffer, sizeof(file_buffer) - 1, "%14s: ", file);

0 commit comments

Comments
 (0)