<feed xmlns='http://www.w3.org/2005/Atom'>
<title>delta/libgit2.git/src/util.h, branch ethomson/github_actions</title>
<subtitle>github.com: libgit2/libgit2.git
</subtitle>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/'/>
<entry>
<title>Merge pull request #5054 from tniessen/util-use-64-bit-timer</title>
<updated>2019-08-23T13:58:15+00:00</updated>
<author>
<name>Edward Thomson</name>
<email>ethomson@edwardthomson.com</email>
</author>
<published>2019-08-23T13:58:15+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=60319788928def21d2164e472a0347bcd8cdab70'/>
<id>60319788928def21d2164e472a0347bcd8cdab70</id>
<content type='text'>
util: use 64 bit timer on Windows</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
util: use 64 bit timer on Windows</pre>
</div>
</content>
</entry>
<entry>
<title>util: do not perform allocations in insertsort</title>
<updated>2019-08-23T10:54:02+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2019-08-08T09:52:54+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=8cbef12d45db531933e7795820f116a07a21f48b'/>
<id>8cbef12d45db531933e7795820f116a07a21f48b</id>
<content type='text'>
Our hand-rolled fallback sorting function `git__insertsort_r` does an
in-place sort of the given array. As elements may not necessarily be
pointers, it needs a way of swapping two values of arbitrary size, which
is currently implemented by allocating a temporary buffer of the
element's size. This is problematic, though, as the emulated `qsort`
interface doesn't provide any return values and thus cannot signal an
error if allocation of that temporary buffer has failed.

Convert the function to swap via a temporary buffer allocated on the
stack. Like this, it can `memcpy` contents of both elements in small
batches without requiring a heap allocation. The buffer size has been
chosen such that in most cases, a single iteration of copying will
suffice. Most importantly, it can fully contain `git_oid` structures and
pointers.

Add a bunch of tests for the `git__qsort_r` interface to verify nothing
breaks. Furthermore, this removes the declaration of `git__insertsort_r`
and makes it static as it is not used anywhere else.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Our hand-rolled fallback sorting function `git__insertsort_r` does an
in-place sort of the given array. As elements may not necessarily be
pointers, it needs a way of swapping two values of arbitrary size, which
is currently implemented by allocating a temporary buffer of the
element's size. This is problematic, though, as the emulated `qsort`
interface doesn't provide any return values and thus cannot signal an
error if allocation of that temporary buffer has failed.

Convert the function to swap via a temporary buffer allocated on the
stack. Like this, it can `memcpy` contents of both elements in small
batches without requiring a heap allocation. The buffer size has been
chosen such that in most cases, a single iteration of copying will
suffice. Most importantly, it can fully contain `git_oid` structures and
pointers.

Add a bunch of tests for the `git__qsort_r` interface to verify nothing
breaks. Furthermore, this removes the declaration of `git__insertsort_r`
and makes it static as it is not used anywhere else.
</pre>
</div>
</content>
</entry>
<entry>
<title>util: use 64 bit timer on Windows</title>
<updated>2019-07-29T16:06:48+00:00</updated>
<author>
<name>Tobias Nießen</name>
<email>tniessen@tnie.de</email>
</author>
<published>2019-04-16T21:49:16+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=fb0730f1f3957b41fa0dd2594fb7d0214b2594da'/>
<id>fb0730f1f3957b41fa0dd2594fb7d0214b2594da</id>
<content type='text'>
git__timer was originally implemented using a 32 bit timer since
Windows XP did not support GetTickCount64. Windows XP was discontinued
five years ago, so it should be safe to use the new API.

As a benefit, we do not need to care about overflows for the next 585
million years.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
git__timer was originally implemented using a 32 bit timer since
Windows XP did not support GetTickCount64. Windows XP was discontinued
five years ago, so it should be safe to use the new API.

As a benefit, we do not need to care about overflows for the next 585
million years.
</pre>
</div>
</content>
</entry>
<entry>
<title>util: introduce GIT_CONTAINER_OF macro</title>
<updated>2019-04-16T11:21:03+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2019-04-16T11:21:03+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=b5f40441f51b091cf34e33153271fbaee133ddc2'/>
<id>b5f40441f51b091cf34e33153271fbaee133ddc2</id>
<content type='text'>
In some parts of our code, we make rather heavy use of casting
structures to their respective specialized implementation. One
example is the configuration code with the general
`git_config_backend` and the specialized `diskfile_header`
structures. At some occasions, it can get confusing though with
regards to the correct inheritance structure, which led to the
recent bug fixed in 2424e64c4 (config: harden our use of the
backend objects a bit, 2018-02-28).

Object-oriented programming in C is hard, but we can at least try
to have some checks when it comes to casting around stuff. Thus,
this commit introduces a `GIT_CONTAINER_OF` macro, which accepts
as parameters the pointer that is to be casted, the pointer it
should be cast to as well as the member inside of the target
structure that is the containing structure. This macro then tries
hard to detect mis-casts:

- It checks whether the source and target pointers are of the
  same type. This requires support by the compiler, as it makes
  use of the builtin `__builtin_types_compatible_p`.

- It checks whether the embedded member of the target structure
  is the first member. In order to make this a compile-time
  constant, the compiler-provided `__builtin_offsetof` is being
  used for this.

- It ties these two checks together by the compiler-builtin
  `__builtin_choose_expr`. Based on whether the previous two
  checks evaluate to `true`, the compiler will either compile in
  the correct cast, or it will output `(void)0`. The second case
  results in a compiler error, resulting in a compile-time check
  for wrong casts.

The only downside to this is that it relies heavily on
compiler-specific extensions. As both GCC and Clang support these
features, only define this macro like explained above in case
`__GNUC__` is set (Clang also defines `__GNUC__`). If the
compiler is not Clang or GCC, just go with a simple cast without
any additional checks.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
In some parts of our code, we make rather heavy use of casting
structures to their respective specialized implementation. One
example is the configuration code with the general
`git_config_backend` and the specialized `diskfile_header`
structures. At some occasions, it can get confusing though with
regards to the correct inheritance structure, which led to the
recent bug fixed in 2424e64c4 (config: harden our use of the
backend objects a bit, 2018-02-28).

Object-oriented programming in C is hard, but we can at least try
to have some checks when it comes to casting around stuff. Thus,
this commit introduces a `GIT_CONTAINER_OF` macro, which accepts
as parameters the pointer that is to be casted, the pointer it
should be cast to as well as the member inside of the target
structure that is the containing structure. This macro then tries
hard to detect mis-casts:

- It checks whether the source and target pointers are of the
  same type. This requires support by the compiler, as it makes
  use of the builtin `__builtin_types_compatible_p`.

- It checks whether the embedded member of the target structure
  is the first member. In order to make this a compile-time
  constant, the compiler-provided `__builtin_offsetof` is being
  used for this.

- It ties these two checks together by the compiler-builtin
  `__builtin_choose_expr`. Based on whether the previous two
  checks evaluate to `true`, the compiler will either compile in
  the correct cast, or it will output `(void)0`. The second case
  results in a compiler error, resulting in a compile-time check
  for wrong casts.

The only downside to this is that it relies heavily on
compiler-specific extensions. As both GCC and Clang support these
features, only define this macro like explained above in case
`__GNUC__` is set (Clang also defines `__GNUC__`). If the
compiler is not Clang or GCC, just go with a simple cast without
any additional checks.
</pre>
</div>
</content>
</entry>
<entry>
<title>optimize string comparisons</title>
<updated>2019-03-29T12:34:55+00:00</updated>
<author>
<name>romkatv</name>
<email>roman.perepelitsa@gmail.com</email>
</author>
<published>2019-03-14T13:54:47+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=30a56ba644e0f0d34c71658c9a53328a1ccc3538'/>
<id>30a56ba644e0f0d34c71658c9a53328a1ccc3538</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>Merge pull request #4864 from pks-t/pks/object-parse-fixes</title>
<updated>2018-10-26T10:33:59+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2018-10-26T10:33:59+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=623647af9959e0ce8d265ef0060a01b0da6b5fd4'/>
<id>623647af9959e0ce8d265ef0060a01b0da6b5fd4</id>
<content type='text'>
Object parse fixes</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Object parse fixes</pre>
</div>
</content>
</entry>
<entry>
<title>util: provide `git__memmem` function</title>
<updated>2018-10-25T10:52:47+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2018-10-18T14:08:46+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=83e8a6b36acc67f2702cbbc7d4e334c7f7737719'/>
<id>83e8a6b36acc67f2702cbbc7d4e334c7f7737719</id>
<content type='text'>
Unfortunately, neither the `memmem` nor the `strnstr` functions are part
of any C standard but are merely extensions of C that are implemented by
e.g. glibc. Thus, there is no standardized way to search for a string in
a block of memory with a limited size, and using `strstr` is to be
considered unsafe in case where the buffer has not been sanitized. In
fact, there are some uses of `strstr` in exactly that unsafe way in our
codebase.

Provide a new function `git__memmem` that implements the `memmem`
semantics. That is in a given haystack of `n` bytes, search for the
occurrence of a byte sequence of `m` bytes and return a pointer to the
first occurrence. The implementation chosen is the "Not So Naive"
algorithm from [1]. It was chosen as the implementation is comparably
simple while still being reasonably efficient in most cases.
Preprocessing happens in constant time and space, searching has a time
complexity of O(n*m) with a slightly sub-linear average case.

[1]: http://www-igm.univ-mlv.fr/~lecroq/string/
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Unfortunately, neither the `memmem` nor the `strnstr` functions are part
of any C standard but are merely extensions of C that are implemented by
e.g. glibc. Thus, there is no standardized way to search for a string in
a block of memory with a limited size, and using `strstr` is to be
considered unsafe in case where the buffer has not been sanitized. In
fact, there are some uses of `strstr` in exactly that unsafe way in our
codebase.

Provide a new function `git__memmem` that implements the `memmem`
semantics. That is in a given haystack of `n` bytes, search for the
occurrence of a byte sequence of `m` bytes and return a pointer to the
first occurrence. The implementation chosen is the "Not So Naive"
algorithm from [1]. It was chosen as the implementation is comparably
simple while still being reasonably efficient in most cases.
Preprocessing happens in constant time and space, searching has a time
complexity of O(n*m) with a slightly sub-linear average case.

[1]: http://www-igm.univ-mlv.fr/~lecroq/string/
</pre>
</div>
</content>
</entry>
<entry>
<title>util: remove `git__strtol32`</title>
<updated>2018-10-18T10:04:07+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2018-10-18T10:04:07+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=8d7fa88a9d5011b653035497b0f523e0f177b6a6'/>
<id>8d7fa88a9d5011b653035497b0f523e0f177b6a6</id>
<content type='text'>
The function `git__strtol32` can easily be misused when untrusted data
is passed to it that may not have been sanitized with trailing `NUL`
bytes. As all usages of this function have now been removed, we can
remove this function altogether to avoid future misuse of it.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
The function `git__strtol32` can easily be misused when untrusted data
is passed to it that may not have been sanitized with trailing `NUL`
bytes. As all usages of this function have now been removed, we can
remove this function altogether to avoid future misuse of it.
</pre>
</div>
</content>
</entry>
<entry>
<title>util: remove unsafe `git__strtol64` function</title>
<updated>2018-10-18T09:49:23+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2018-10-18T09:37:10+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=68deb2cc80ef19bf3a1915c26b5308b283a6d69a'/>
<id>68deb2cc80ef19bf3a1915c26b5308b283a6d69a</id>
<content type='text'>
The function `git__strtol64` does not take a maximum buffer length as
parameter. This has led to some unsafe usages of this function, and as
such we may consider it as being unsafe to use. As we have now
eradicated all usages of this function, let's remove it completely to
avoid future misuse.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
The function `git__strtol64` does not take a maximum buffer length as
parameter. This has led to some unsafe usages of this function, and as
such we may consider it as being unsafe to use. As we have now
eradicated all usages of this function, let's remove it completely to
avoid future misuse.
</pre>
</div>
</content>
</entry>
<entry>
<title>util: extract allocators into its own "alloc.h" header</title>
<updated>2018-06-07T10:57:39+00:00</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2018-03-14T10:36:14+00:00</published>
<link rel='alternate' type='text/html' href='http://91.123.203.49/cgit/delta/libgit2.git/commit/?id=d2e996fa17c0c39afe32a703a88b5f1f7b256c1c'/>
<id>d2e996fa17c0c39afe32a703a88b5f1f7b256c1c</id>
<content type='text'>
Our "util.h" header is a grabbag of various different functions, where
many don't have a clear group they belong to. Our set of allocator
functions though can be clearly singled out as a single group of
functions that always belongs together. Furthermore, we will need to
implement additional functions relating to our allocators subsystem when
moving to pluggable allocators. Thus, we should just move these
functions into their own "alloc" module.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Our "util.h" header is a grabbag of various different functions, where
many don't have a clear group they belong to. Our set of allocator
functions though can be clearly singled out as a single group of
functions that always belongs together. Furthermore, we will need to
implement additional functions relating to our allocators subsystem when
moving to pluggable allocators. Thus, we should just move these
functions into their own "alloc" module.
</pre>
</div>
</content>
</entry>
</feed>
