song_print: check gmtime_r()'s return value

When song->mtime was not initialized properly, it was revealed that
strftime() might crash when gmtime_r() returns NULL due to an invalid
time_t input value.
This commit is contained in:
Max Kellermann 2009-07-06 11:48:21 +02:00
parent 09aadffe9b
commit 17e3054399

View File

@ -51,20 +51,24 @@ song_print_info(struct client *client, struct song *song)
song_print_url(client, song); song_print_url(client, song);
if (song->mtime > 0) { if (song->mtime > 0) {
time_t t = song->mtime;
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
struct tm tm; struct tm tm;
#endif #endif
char timestamp[32]; const struct tm *tm2;
strftime(timestamp, sizeof(timestamp), "%FT%TZ",
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
gmtime(&t) tm2 = gmtime(&song->mtime);
#else #else
gmtime_r(&t, &tm) tm2 = gmtime_r(&song->mtime, &tm);
#endif #endif
);
client_printf(client, "Last-Modified: %s\n", timestamp); if (tm2 != NULL) {
char timestamp[32];
strftime(timestamp, sizeof(timestamp), "%FT%TZ", tm2);
client_printf(client, "Last-Modified: %s\n",
timestamp);
}
} }
if (song->tag) if (song->tag)