Commit Graph

84 Commits

Author SHA1 Message Date
Edward Thomson
14f6950bce buf: bom enum is in the buf namespace
Instead of a `git_bom_t` that a `git_buf` function returns, let's keep
it `git_buf_bom_t`.
2021-05-11 01:30:58 +01:00
Edward Thomson
d525e063ba buf: remove internal git_buf_text namespace
The `git_buf_text` namespace is unnecessary and strange.  Remove it,
just keep the functions prefixed with `git_buf`.
2021-05-11 01:29:22 +01:00
Tobias Nießen
52447c5c95 tests: fix variable name in list.c 2021-04-11 23:16:37 +02:00
Edward Thomson
cad7a1bad4 clar: include the function name 2020-06-05 08:49:07 +01:00
Edward Thomson
51eff5a58b strarray: we should dispose instead of free
We _dispose_ the contents of objects; we _free_ objects (and their
contents).  Update `git_strarray_free` to be `git_strarray_dispose`.
`git_strarray_free` remains as a deprecated proxy function.
2020-06-01 22:50:28 +01:00
Patrick Steinhardt
eaa70c6c36 tests: object: decrease number of concurrent cache accesses
In our test case object::cache::fast_thread_rush, we're creating 100
concurrent threads opening a repository and reading objects from it.
This test actually fails on ARM32 with an out-of-memory error, which
isn't entirely unexpected.

Work around the issue by halving the number of threads.
2020-02-18 18:09:11 +01:00
Edward Thomson
ba4c769b13 tree: ensure we protect NTFS paths everywhere 2019-12-10 18:11:45 +10:00
Edward Thomson
f3b2860423 test: ensure treebuilder validate new protection rules
Ensure that the new protection around .git::$INDEX_ALLOCATION rules are
enabled for using the treebuilder when core.protectNTFS is set.
2019-12-10 18:09:11 +10:00
Edward Thomson
6460e8abcf internal: use off64_t instead of git_off_t
Prefer `off64_t` internally.
2019-11-25 13:18:29 +11:00
Patrick Steinhardt
e54343a402 fileops: rename to "futils.h" to match function signatures
Our file utils functions all have a "futils" prefix, e.g.
`git_futils_touch`. One would thus naturally guess that their
definitions and implementation would live in files "futils.h" and
"futils.c", respectively, but in fact they live in "fileops.h".

Rename the files to match expectations.
2019-07-20 19:11:20 +02:00
Edward Thomson
3edbc44165 object: use literal constant in bigfile test
Don't calculate 4 GiB as that will produce a compiler warning on MSVC.
Just hardcode it.
2019-06-24 15:00:37 +01:00
Edward Thomson
8eb910b0e5 largefile tests: only write 2GB on 32-bit platforms
Don't try to feed 4 GB of data to APIs that only take a `size_t` on
32-bit platforms.
2019-06-23 11:26:10 +01:00
Edward Thomson
6574cd0076 index: rename frombuffer to from_buffer
The majority of functions are named `from_something` (with an
underscore) instead of `fromsomething`.  Update the index functions for
consistency with the rest of the library.
2019-06-16 00:55:14 +01:00
Edward Thomson
08f392080c blob: add underscore to from functions
The majority of functions are named `from_something` (with an
underscore) instead of `fromsomething`.  Update the blob functions for
consistency with the rest of the library.
2019-06-16 00:16:49 +01:00
Patrick Steinhardt
0c2d0d4b90 tests: object: refactor largefile test to not use p_fallocate
The `p_fallocate` platform is currently in use in our tests,
only, but it proved to be quite burdensome to get it implemented
in a cross-platform way. The only "real" user is the test
object::tree::read::largefile, where it's used to allocate a
large file in the filesystem only to commit it to the repo and
read its object back again. We can simplify this quite a bit by
just using an in-memory buffer of 4GB. Sure, this cannot be used
on platforms with low resources. But creating 4GB files is not
any better, and we already skip the test if the environment
variable "GITTEST_INVASIVE_FS_SIZE" is not set. So we're arguably
not worse off than before.
2019-06-14 15:59:47 +02:00
Patrick Steinhardt
1f47efc429 tests: object: consolidate cache tests
The object::cache test module has two tests that do nearly the
same thing: given a cache limit, load a certain set of objects
and verify if those objects have been cached or not.

Convert those tests to the new data-driven initializers to
demonstrate how these are to be used. Furthermore, add some
additional test data. This conversion is mainly done to show this
new facility.
2019-06-07 14:23:49 +02:00
Edward Thomson
fb7614c0f3 tests: test largefiles on win32 2019-04-04 13:51:52 -07:00
Etienne Samson
4e3949b73e tests: test that largefiles can be read through the tree API 2019-01-30 02:14:11 +01:00
Edward Thomson
f673e232af git_error: use new names in internal APIs and usage
Move to the `git_error` name in the internal API for error-related
functions.
2019-01-22 22:30:35 +00:00
Edward Thomson
cd35085220 object_type: GIT_OBJECT_BAD is now GIT_OBJECT_INVALID
We use the term "invalid" to refer to bad or malformed data, eg
`GIT_REF_INVALID` and `GIT_EINVALIDSPEC`.  Since we're changing the
names of the `git_object_t`s in this release, update it to be
`GIT_OBJECT_INVALID` instead of `BAD`.
2019-01-17 10:54:03 +00:00
Edward Thomson
168fe39bea object_type: use new enumeration names
Use the new object_type enumeration names within the codebase.
2018-12-01 11:54:57 +00:00
Edward Thomson
18e71e6d59 index: use new enum and structure names
Use the new-style index names throughout our own codebase.
2018-12-01 10:46:44 +00:00
Patrick Steinhardt
7fafec0e53 tree: fix integer overflow when reading unreasonably large filemodes
The `parse_mode` option uses an open-coded octal number parser. The
parser is quite naive in that it simply parses until hitting a character
that is not in the accepted range of '0' - '7', completely ignoring the
fact that we can at most accept a 16 bit unsigned integer as filemode.
If the filemode is bigger than UINT16_MAX, it will thus overflow and
provide an invalid filemode for the object entry.

Fix the issue by using `git__strntol32` instead and doing a bounds
check. As this function already handles overflows, it neatly solves the
problem.

Note that previously, `parse_mode` was also skipping the character
immediately after the filemode. In proper trees, this should be a simple
space, but in fact the parser accepted any character and simply skipped
over it. As a consequence of using `git__strntol32`, we now need to an
explicit check for a trailing whitespace after having parsed the
filemode. Because of the newly introduced error message, the test
object::tree::parse::mode_doesnt_cause_oob_read needs adjustment to its
error message check, which in fact is a good thing as it demonstrates
that we now fail looking for the whitespace immediately following the
filemode.

Add a test that shows that we will fail to parse such invalid filemodes
now.
2018-11-02 13:31:09 +01:00
Patrick Steinhardt
f647bbc88d tree: fix mode parsing reading out-of-bounds
When parsing a tree entry's mode, we will eagerly parse until we hit a
character that is not in the accepted set of octal digits '0' - '7'. If
the provided buffer is not a NUL terminated one, we may thus read
out-of-bounds.

Fix the issue by passing the buffer length to `parse_mode` and paying
attention to it. Note that this is not a vulnerability in our usual code
paths, as all object data read from the ODB is NUL terminated.
2018-11-02 13:31:09 +01:00
Patrick Steinhardt
d4ad658a69 tree: add various tests exercising the tree parser
We currently don't have any tests that directly exercise the tree
parser. This is due to the fact that the parsers for raw object data has
only been recently introduce with commit ca4db5f4a (object: implement
function to parse raw data, 2017-10-13), and previous to that the setup
simply was too cumbersome as it always required going through the ODB.

Now that we have the infrastructure, add a suite of tests that directly
exercise the tree parser and various edge cases.
2018-11-02 13:31:09 +01:00
Patrick Steinhardt
7655b2d89e commit: fix reading out of bounds when parsing encoding
The commit message encoding is currently being parsed by the
`git__prefixcmp` function. As this function does not accept a buffer
length, it will happily skip over a buffer's end if it is not `NUL`
terminated.

Fix the issue by using `git__prefixncmp` instead. Add a test that
verifies that we are unable to parse the encoding field if it's cut off
by the supplied buffer length.
2018-10-25 12:52:54 +02:00
Patrick Steinhardt
c2e3d8ef69 tests: add tests that exercise commit parsing
We currently do not have any test suites dedicated to parsing commits
from their raw representations. Add one based on `git_object__from_raw`
to be able to test special cases more easily.
2018-10-25 12:52:54 +02:00
Patrick Steinhardt
ee11d47e3d tag: fix out of bounds read when searching for tag message
When parsing tags, we skip all unknown fields that appear before the tag
message. This skipping is done by using a plain `strstr(buffer, "\n\n")`
to search for the two newlines that separate tag fields from tag
message. As it is not possible to supply a buffer length to `strstr`,
this call may skip over the buffer's end and thus result in an out of
bounds read. As `strstr` may return a pointer that is out of bounds, the
following computation of `buffer_end - buffer` will overflow and result
in an allocation of an invalid length.

Fix the issue by using `git__memmem` instead. Add a test that verifies
parsing the tag fails not due to the allocation failure but due to the
tag having no message.
2018-10-25 12:52:54 +02:00
Patrick Steinhardt
4c738e563d tests: add tests that exercise tag parsing
While the tests in object::tag::read exercises reading and parsing valid
tags from the ODB, they barely try to verify that the parser fails in a
sane way when parsing invalid tags. Create a new test suite
object::tag::parse that directly exercise the parser by using
`git_object__from_raw` and add various tests for valid and invalid tags.
2018-10-25 12:52:54 +02:00
Carlos Martín Nieto
f00db9ed67 tree: rename from_tree to validate and clarify the tree in the test 2018-07-27 12:00:37 +02:00
Carlos Martín Nieto
2dff7e282d tree: accept null ids in existing trees when updating
When we add entries to a treebuilder we validate them. But we validate even
those that we're adding because they exist in the base tree. This disables
using the normal mechanisms on these trees, even to fix them.

Keep track of whether the entry we're appending comes from an existing tree and
bypass the name and id validation if it's from existing data.
2018-07-18 21:07:57 +02:00
Patrick Steinhardt
9994cd3f0f treewide: remove use of C++ style comments
C++ style comment ("//") are not specified by the ISO C90 standard and
thus do not conform to it. While libgit2 aims to conform to C90, we did
not enforce it until now, which is why quite a lot of these
non-conforming comments have snuck into our codebase. Do a tree-wide
conversion of all C++ style comments to the supported C style comments
to allow us enforcing strict C90 compliance in a later commit.
2018-07-13 08:25:12 +02:00
Patrick Steinhardt
ecf4f33a4e Convert usage of git_buf_free to new git_buf_dispose 2018-06-10 19:34:37 +02:00
Carlos Martín Nieto
a554d588cc tree: initialize the id we use for testing submodule insertions
Instead of laving it uninitialized and relying on luck for it to be non-zero,
let's give it a dummy hash so we make valgrind happy (in this case the hash
comes from `sha1sum </dev/null`.
2018-02-28 12:21:08 +01:00
Patrick Steinhardt
c0487bde7e tree: reject writing null-OID entries to a tree
In commit a96d3cc3f (cache-tree: reject entries with null sha1,
2017-04-21), the git.git project has changed its stance on null OIDs in
tree objects. Previously, null OIDs were accepted in tree entries to
help tools repair broken history. This resulted in some problems though
in that many code paths mistakenly passed null OIDs to be added to a
tree, which was not properly detected.

Align our own code base according to the upstream change and reject
writing tree entries early when the OID is all-zero.
2018-01-26 13:08:40 +00:00
Edward Thomson
1dc89aab24 object validation: free some memleaks 2017-05-01 22:55:12 +01:00
Patrick Steinhardt
35079f507b odb: add option to turn off hash verification
Verifying hashsums of objects we are reading from the ODB may be costly
as we have to perform an additional hashsum calculation on the object.
Especially when reading large objects, the penalty can be as high as
35%, as can be seen when executing the equivalent of `git cat-file` with
and without verification enabled. To mitigate for this, we add a global
option for libgit2 which enables the developer to turn off the
verification, e.g. when he can be reasonably sure that the objects on
disk won't be corrupted.
2017-04-28 14:05:45 +02:00
Patrick Steinhardt
28a0741f1a odb: verify object hashes
The upstream git.git project verifies objects when looking them up from
disk. This avoids scenarios where objects have somehow become corrupt on
disk, e.g. due to hardware failures or bit flips. While our mantra is
usually to follow upstream behavior, we do not do so in this case, as we
never check hashes of objects we have just read from disk.

To fix this, we create a new error class `GIT_EMISMATCH` which denotes
that we have looked up an object with a hashsum mismatch. `odb_read_1`
will then, after having read the object from its backend, hash the
object and compare the resulting hash to the expected hash. If hashes do
not match, it will return an error.

This obviously introduces another computation of checksums and could
potentially impact performance. Note though that we usually perform I/O
operations directly before doing this computation, and as such the
actual overhead should be drowned out by I/O. Running our test suite
seems to confirm this guess. On a Linux system with best-of-five
timings, we had 21.592s with the check enabled and 21.590s with the
ckeck disabled. Note though that our test suite mostly contains very
small blobs only. It is expected that repositories with bigger blobs may
notice an increased hit by this check.

In addition to a new test, we also had to change the
odb::backend::nonrefreshing test suite, which now triggers a hashsum
mismatch when looking up the commit "deadbeef...". This is expected, as
the fake backend allocated inside of the test will return an empty
object for the OID "deadbeef...", which will obviously not hash back to
"deadbeef..." again. We can simply adjust the hash to equal the hash of
the empty object here to fix this test.
2017-04-28 14:05:45 +02:00
Patrick Steinhardt
d59dabe5cb tests: object: test looking up corrupted objects
We currently have no tests which check whether we fail reading corrupted
objects. Add one which modifies contents of an object stored on disk and
then tries to read the object.
2017-04-28 14:05:45 +02:00
Patrick Steinhardt
86c035526d tests: object: create sandbox
The object::lookup tests do use the "testrepo.git" repository in a
read-only way, so we do not set up the repository as a sandbox but
simply open it. But in a future commit, we will want to test looking up
objects which are corrupted in some way, which requires us to modify the
on-disk data. Doing this in a repository without creating the sandbox
will modify contents of our libgit2 repository, though.

Create the repository in a sandbox to avoid this.
2017-04-28 14:05:44 +02:00
Carlos Martín Nieto
1d41b86cd0 tree: add a failing test for unsorted input
We do not currently use the sorted version of this input in the
function, which means we produce bad results.
2016-11-14 12:22:20 +01:00
Patrick Steinhardt
4006455f01 tests: blob: remove unused callback function 2016-08-09 10:09:23 +02:00
Patrick Steinhardt
faebc1c6ec threads: split up OS-dependent thread code 2016-06-20 19:32:59 +02:00
Carlos Martín Nieto
a2cb47130e tree: handle removal of all entries in the updater
When we remove all entries in a tree, we should remove that tree from
its parent rather than include the empty tree.
2016-05-24 14:30:43 +02:00
Carlos Martín Nieto
5341230536 tree: plug leaks in the tree updater 2016-05-19 15:29:53 +02:00
Carlos Martín Nieto
922496562b tree: use testrepo2 for the tree updater tests
This gives us trees with subdirectories, which the new test needs.
2016-05-19 15:21:26 +02:00
Carlos Martín Nieto
9464f9ebc1 Introduce a function to create a tree based on a different one
Instead of going through the usual steps of reading a tree recursively
into an index, modifying it and writing it back out as a tree, introduce
a function to perform simple updates more efficiently.

`git_tree_create_updated` avoids reading trees which are not modified
and supports upsert and delete operations. It is not as versatile as
modifying the index, but it makes some common operations much more
efficient.
2016-05-17 17:41:05 +02:00
Carlos Martín Nieto
eb39284bab tag: ignore extra header fields
While no extra header fields are defined for tags, git accepts them by
ignoring them and continuing the search for the message. There are a few
tags like this in the wild which git parses just fine, so we should do
the same.
2016-04-25 12:18:32 +02:00
Carlos Martín Nieto
6669e3e839 blob: remove _fromchunks()
The callback mechanism makes it awkward to write data from an IO
source; move to `_fromstream()` which lets the caller remain in control,
in the same vein as we prefer iterators over foreach callbacks.
2016-03-22 20:00:25 +01:00
Carlos Martín Nieto
35e68606da blob: fix fromchunks iteration counter
By returning when the count goes to zero rather than below it, setting
`howmany` to 7 in fact writes out the string 6 times.

Correct the termination condition to write out the string the amount of
times we specify.
2016-03-22 20:00:24 +01:00