Commit 72d0024134a532374e7fa17ae9e5b6b021ffe324

Vicent Marti 2014-11-21T13:32:21

attr_file: Do not assume ODB data is NULL-terminated That's a bad assumption to make, even though right now it holds (because of the way we've implemented decompression of packfiles), this may change in the future, given that ODB objects can be binary data. Furthermore, the ODB object can return a NULL pointer if the object is empty. Copying the NULL pointer to the strbuf lets us handle it like an empty string. Again, the NULL pointer is valid behavior because you're supposed to check the *size* of the object before working on it.

diff --git a/src/attr_file.c b/src/attr_file.c
index e3692ce..b3efeef 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -103,7 +103,6 @@ int git_attr_file__load(
 	int error = 0;
 	git_blob *blob = NULL;
 	git_buf content = GIT_BUF_INIT;
-	const char *data = NULL;
 	git_attr_file *file;
 	struct stat st;
 
@@ -120,7 +119,9 @@ int git_attr_file__load(
 			(error = git_blob_lookup(&blob, repo, &id)) < 0)
 			return error;
 
-		data = git_blob_rawcontent(blob);
+		/* Do not assume that data straight from the ODB is NULL-terminated;
+		 * copy the contents of a file to a buffer to work on */
+		git_buf_put(&content, git_blob_rawcontent(blob), git_blob_rawsize(blob));
 		break;
 	}
 	case GIT_ATTR_FILE__FROM_FILE: {
@@ -143,7 +144,6 @@ int git_attr_file__load(
 		if (error < 0)
 			return GIT_ENOTFOUND;
 
-		data = content.ptr;
 		break;
 	}
 	default:
@@ -154,7 +154,7 @@ int git_attr_file__load(
 	if ((error = git_attr_file__new(&file, entry, source)) < 0)
 		goto cleanup;
 
-	if (parser && (error = parser(repo, file, data)) < 0) {
+	if (parser && (error = parser(repo, file, git_buf_cstr(&content))) < 0) {
 		git_attr_file__free(file);
 		goto cleanup;
 	}