Commit 0a3bcad07ee1de26dea22451952b55f0568e2985

Alex Budovski 2011-01-10T14:57:06

Fix Windows build with forced bit truncation. Windows uses a 64 bit time_t by default and assigning to unsigned int causes a 64 -> 32 bit truncation warning. This change forces the truncation, acknowledging the implications detailed in the file comments. Also, blobs are limited to 32 bit file sizes for the same reason (on all platforms).

diff --git a/src/index.c b/src/index.c
index 6fdb46e..e861d79 100644
--- a/src/index.c
+++ b/src/index.c
@@ -303,8 +303,12 @@ int git_index_add(git_index *index, const char *rel_path, int stage)
 
 	memset(&entry, 0x0, sizeof(git_index_entry));
 
-	entry.ctime.seconds = st.st_ctime;
-	entry.mtime.seconds = st.st_mtime;
+	/* Note: this won't wrap around at 2038 because we're not storing signed
+	 * 32-bit integers, but unsigned ones. We're converting from signed 64-bit
+	 * integers (on platforms that use 64bit time_t) to unsigned 32-bit integers,
+	 * which will represent up to 2^32 - 1 seconds after the epoch */
+	entry.ctime.seconds = (unsigned int)st.st_ctime;
+	entry.mtime.seconds = (unsigned int)st.st_mtime;
 	/* entry.mtime.nanoseconds = st.st_mtimensec; */
 	/* entry.ctime.nanoseconds = st.st_ctimensec; */
 	entry.dev= st.st_rdev;
@@ -312,7 +316,8 @@ int git_index_add(git_index *index, const char *rel_path, int stage)
 	entry.mode = st.st_mode;
 	entry.uid = st.st_uid;
 	entry.gid = st.st_gid;
-	entry.file_size = st.st_size;
+	/* Note: the following restricts blobs to 4GB in size */
+	entry.file_size = (unsigned int)st.st_size;
 
 	/* write the blob to disk and get the oid */
 	if ((error = git_blob_writefile(&entry.oid, index->repository, full_path)) < GIT_SUCCESS)