vim-patch:8.1.1313: warnings for using localtime() and ctime()

Problem:    Warnings for using localtime() and ctime().
Solution:   Use localtime_r() if available.  Avoid using ctime().
63d2555c9c
This commit is contained in:
Jan Edmund Lazo
2020-03-20 05:50:16 -04:00
parent e700a88bb6
commit 573671b1fb
3 changed files with 47 additions and 24 deletions

View File

@@ -2416,9 +2416,7 @@ static int prt_add_resource(struct prt_ps_resource_S *resource)
int mch_print_begin(prt_settings_T *psettings) int mch_print_begin(prt_settings_T *psettings)
{ {
time_t now;
int bbox[4]; int bbox[4];
char *p_time;
double left; double left;
double right; double right;
double top; double top;
@@ -2442,10 +2440,10 @@ int mch_print_begin(prt_settings_T *psettings)
} }
prt_dsc_textline("For", buffer); prt_dsc_textline("For", buffer);
prt_dsc_textline("Creator", longVersion); prt_dsc_textline("Creator", longVersion);
/* Note: to ensure Clean8bit I don't think we can use LC_TIME */ // Note: to ensure Clean8bit I don't think we can use LC_TIME
now = time(NULL); char ctime_buf[50];
p_time = ctime(&now); char *p_time = os_ctime(ctime_buf, sizeof(ctime_buf));
/* Note: ctime() adds a \n so we have to remove it :-( */ // Note: os_ctime() adds a \n so we have to remove it :-(
p = vim_strchr((char_u *)p_time, '\n'); p = vim_strchr((char_u *)p_time, '\n');
if (p != NULL) if (p != NULL)
*p = NUL; *p = NUL;

View File

@@ -1504,16 +1504,15 @@ static time_t swapfile_info(char_u *fname)
int fd; int fd;
struct block0 b0; struct block0 b0;
time_t x = (time_t)0; time_t x = (time_t)0;
char *p;
#ifdef UNIX #ifdef UNIX
char uname[B0_UNAME_SIZE]; char uname[B0_UNAME_SIZE];
#endif #endif
/* print the swap file date */ // print the swap file date
FileInfo file_info; FileInfo file_info;
if (os_fileinfo((char *)fname, &file_info)) { if (os_fileinfo((char *)fname, &file_info)) {
#ifdef UNIX #ifdef UNIX
/* print name of owner of the file */ // print name of owner of the file
if (os_get_uname(file_info.stat.st_uid, uname, B0_UNAME_SIZE) == OK) { if (os_get_uname(file_info.stat.st_uid, uname, B0_UNAME_SIZE) == OK) {
MSG_PUTS(_(" owned by: ")); MSG_PUTS(_(" owned by: "));
msg_outtrans((char_u *)uname); msg_outtrans((char_u *)uname);
@@ -1522,11 +1521,8 @@ static time_t swapfile_info(char_u *fname)
#endif #endif
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
x = file_info.stat.st_mtim.tv_sec; x = file_info.stat.st_mtim.tv_sec;
p = ctime(&x); // includes '\n' char ctime_buf[50];
if (p == NULL) MSG_PUTS(os_ctime_r(&x, ctime_buf, sizeof(ctime_buf)));
MSG_PUTS("(invalid)\n");
else
MSG_PUTS(p);
} }
/* /*
@@ -3267,15 +3263,13 @@ attention_message (
) )
{ {
assert(buf->b_fname != NULL); assert(buf->b_fname != NULL);
time_t x, sx;
char *p;
++no_wait_return; ++no_wait_return;
(void)EMSG(_("E325: ATTENTION")); (void)EMSG(_("E325: ATTENTION"));
MSG_PUTS(_("\nFound a swap file by the name \"")); MSG_PUTS(_("\nFound a swap file by the name \""));
msg_home_replace(fname); msg_home_replace(fname);
MSG_PUTS("\"\n"); MSG_PUTS("\"\n");
sx = swapfile_info(fname); const time_t swap_mtime = swapfile_info(fname);
MSG_PUTS(_("While opening file \"")); MSG_PUTS(_("While opening file \""));
msg_outtrans(buf->b_fname); msg_outtrans(buf->b_fname);
MSG_PUTS("\"\n"); MSG_PUTS("\"\n");
@@ -3284,14 +3278,12 @@ attention_message (
MSG_PUTS(_(" CANNOT BE FOUND")); MSG_PUTS(_(" CANNOT BE FOUND"));
} else { } else {
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
x = file_info.stat.st_mtim.tv_sec; time_t x = file_info.stat.st_mtim.tv_sec;
p = ctime(&x); // includes '\n' char ctime_buf[50];
if (p == NULL) MSG_PUTS(os_ctime_r(&x, ctime_buf, sizeof(ctime_buf)));
MSG_PUTS("(invalid)\n"); if (swap_mtime != 0 && x > swap_mtime) {
else
MSG_PUTS(p);
if (sx != 0 && x > sx)
MSG_PUTS(_(" NEWER than swap file!\n")); MSG_PUTS(_(" NEWER than swap file!\n"));
}
} }
/* Some of these messages are long to allow translation to /* Some of these messages are long to allow translation to
* other languages. */ * other languages. */

View File

@@ -144,6 +144,39 @@ struct tm *os_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL
return os_localtime_r(&rawtime, result); return os_localtime_r(&rawtime, result);
} }
/// Portable version of POSIX ctime_r()
///
/// @param clock[in]
/// @param result[out] Pointer to a 'char' where the result should be placed
/// @param result_len length of result buffer
/// @return human-readable string of current local time
char *os_ctime_r(const time_t *restrict clock, char *restrict result,
size_t result_len)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
struct tm clock_local;
struct tm *clock_local_ptr = os_localtime_r(clock, &clock_local);
// MSVC returns NULL for an invalid value of seconds.
if (clock_local_ptr == NULL) {
snprintf(result, result_len, "%s\n", _("(Invalid)"));
} else {
strftime(result, result_len, "%a %b %d %H:%M:%S %Y\n", clock_local_ptr);
}
return result;
}
/// Gets the current Unix timestamp and adjusts it to local time.
///
/// @param result[out] Pointer to a 'char' where the result should be placed
/// @param result_len length of result buffer
/// @return human-readable string of current local time
char *os_ctime(char *result, size_t result_len)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
time_t rawtime = time(NULL);
return os_ctime_r(&rawtime, result, result_len);
}
/// Obtains the current Unix timestamp. /// Obtains the current Unix timestamp.
/// ///
/// @return Seconds since epoch. /// @return Seconds since epoch.