From 17e3054399ba649b6e2c6fa8e7a946c286eafe77 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Mon, 6 Jul 2009 11:48:21 +0200
Subject: 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.
---
 src/song_print.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/song_print.c b/src/song_print.c
index 2efae9b89..2c4da6cb0 100644
--- a/src/song_print.c
+++ b/src/song_print.c
@@ -51,20 +51,24 @@ song_print_info(struct client *client, struct song *song)
 	song_print_url(client, song);
 
 	if (song->mtime > 0) {
-		time_t t = song->mtime;
 #ifndef G_OS_WIN32
 		struct tm tm;
 #endif
-		char timestamp[32];
+		const struct tm *tm2;
 
-		strftime(timestamp, sizeof(timestamp), "%FT%TZ",
 #ifdef G_OS_WIN32
-			 gmtime(&t)
+		tm2 = gmtime(&song->mtime);
 #else
-			 gmtime_r(&t, &tm)
+		tm2 = gmtime_r(&song->mtime, &tm);
 #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)
-- 
cgit v1.2.3