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.
This commit is contained in:
Vicent Marti 2014-11-21 13:32:21 +01:00
parent 92e0b67930
commit 72d0024134

View File

@ -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;
}