str: introduce git_str for internal, git_buf is external

libgit2 has two distinct requirements that were previously solved by
`git_buf`.  We require:

1. A general purpose string class that provides a number of utility APIs
   for manipulating data (eg, concatenating, truncating, etc).
2. A structure that we can use to return strings to callers that they
   can take ownership of.

By using a single class (`git_buf`) for both of these purposes, we have
confused the API to the point that refactorings are difficult and
reasoning about correctness is also difficult.

Move the utility class `git_buf` to be called `git_str`: this represents
its general purpose, as an internal string buffer class.  The name also
is an homage to Junio Hamano ("gitstr").

The public API remains `git_buf`, and has a much smaller footprint.  It
is generally only used as an "out" param with strict requirements that
follow the documentation.  (Exceptions exist for some legacy APIs to
avoid breaking callers unnecessarily.)

Utility functions exist to convert a user-specified `git_buf` to a
`git_str` so that we can call internal functions, then converting it
back again.
This commit is contained in:
Edward Thomson
2021-09-07 17:53:49 -04:00
parent 5346be3ddd
commit f0e693b18a
346 changed files with 6771 additions and 6320 deletions

View File

@@ -11,8 +11,8 @@
#include "git2.h"
#include "buffer.h"
#include "common.h"
#include "str.h"
#include "futils.h"
#include "hash.h"
#include "commit_graph.h"
@@ -33,7 +33,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
git_commit_graph_file file = {{0}};
git_commit_graph_entry e;
git_buf commit_graph_buf = GIT_BUF_INIT;
git_str commit_graph_buf = GIT_STR_INIT;
unsigned char hash[GIT_HASH_SHA1_SIZE];
git_oid oid = {{0}};
bool append_hash = false;
@@ -51,7 +51,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
size -= 4;
if (append_hash) {
if (git_buf_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0)
if (git_str_init(&commit_graph_buf, size + GIT_HASH_SHA1_SIZE) < 0)
goto cleanup;
if (git_hash_buf(hash, data, size, GIT_HASH_ALGORITHM_SHA1) < 0) {
fprintf(stderr, "Failed to compute the SHA1 hash\n");
@@ -62,13 +62,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
memcpy(oid.id, hash, GIT_OID_RAWSZ);
} else {
git_buf_attach_notowned(&commit_graph_buf, (char *)data, size);
git_str_attach_notowned(&commit_graph_buf, (char *)data, size);
}
if (git_commit_graph_file_parse(
&file,
(const unsigned char *)git_buf_cstr(&commit_graph_buf),
git_buf_len(&commit_graph_buf))
(const unsigned char *)git_str_cstr(&commit_graph_buf),
git_str_len(&commit_graph_buf))
< 0)
goto cleanup;
@@ -78,6 +78,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
cleanup:
git_commit_graph_file_close(&file);
git_buf_dispose(&commit_graph_buf);
git_str_dispose(&commit_graph_buf);
return 0;
}