Browse code

check ctime return value (bb #869).

git-svn: trunk@3713

Török Edvin authored on 2008/03/18 00:35:37
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Mar 17 16:43:23 EET 2008 (edwin)
2
+-----------------------------------
3
+  * libclamav/others.c: check ctime return value (bb #869).
4
+
1 5
 Thu Mar 13 10:50:36 CET 2008 (tk)
2 6
 ---------------------------------
3 7
   * unit_tests: add initial support for unit tests; based on a patch by
... ...
@@ -901,33 +901,49 @@ int cli_bitset_test(bitset_t *bs, unsigned long bit_offset)
901 901
 	return (bs->bitset[char_offset] & ((unsigned char)1 << bit_offset));
902 902
 }
903 903
 
904
+/* returns converted timestamp, in case of error the returned string contains at least one character */
904 905
 const char* cli_ctime(const time_t *timep, char *buf, const size_t bufsize)
905 906
 {
907
+	const char *ret;
906 908
 	if(bufsize < 26) {
907 909
 		/* standard says we must have at least 26 bytes buffer */
908 910
 		cli_warnmsg("buffer too small for ctime\n");
909
-		return NULL;
911
+		return " ";
912
+	}
913
+	if((uint32_t)(*timep) > 0x7fffffff) {
914
+		/* some systems can consider these timestamps invalid */
915
+		strncpy(buf, "invalid timestamp", bufsize-1);
916
+		buf[bufsize-1] = '\0';
917
+		return buf;
910 918
 	}
919
+
911 920
 #ifdef HAVE_CTIME_R	
912 921
 # ifdef HAVE_CTIME_R_2
913
-	{
914
-	    char* y = ctime_r(timep, buf);
915
-	    return y;
916
-	}
922
+	ret = ctime_r(timep, buf);
917 923
 # else
918
-	return ctime_r(timep, buf, bufsize);
924
+	ret = ctime_r(timep, buf, bufsize);
919 925
 # endif
920 926
 #else /* no ctime_r */
921 927
 
922 928
 # ifdef CL_THREAD_SAFE
923 929
 	pthread_mutex_lock(&cli_ctime_mutex);
924 930
 # endif
925
-	strncpy(buf, ctime(timep), bufsize-1);
926
-	buf[bufsize-1] = '\0';
931
+	ret = ctime(timep);
932
+	if(ret) {
933
+		strncpy(buf, ret, bufsize-1);
934
+		buf[bufsize-1] = '\0';
935
+		ret = buf;
936
+	}
927 937
 # ifdef CL_THREAD_SAFE
928 938
 	pthread_mutex_unlock(&cli_ctime_mutex);
929 939
 # endif
930
-	return buf;
931 940
 #endif
941
+	/* common */
942
+	if(!ret) {
943
+		buf[0] = ' ';
944
+		buf[1] = '\0';
945
+		return buf;
946
+	}
947
+	return ret;
932 948
 }
933 949