1. 15 May, 2023 3 commits
    • tests: only copy when `ptr` is non-NULL · 27576416
      Avoid passing a `NULL` ptr to `memcpy` -- that's UB (even if size is 0)
      Edward Thomson committed
    • tests: add allocation failure test for buffers · 8e5281c8
      Test that `git_buf` correctly fails if no more bytes can be allocated.
      This is mostly for demonstration purposes.
      Patrick Steinhardt committed
    • tests: add allocator with limited number of bytes · 90cc0771
      In several circumstances, we get bug reports about things that happen in
      situations where the environment is quite limited with regards to
      available memory. While it's expected that functionality will fail if
      memory allocations fail, the assumption is that we should do so in a
      controlled way. Most importantly, we do not want to crash hard due to
      e.g. accessing NULL pointers.
      
      Naturally, it is quite hard to debug such situations. But since our
      addition of pluggable allocators, we are able to implement allocators
      that fail in deterministic ways, e.g. after a certain amount of bytes
      has been allocated. This commit does exactly that.
      
      To be able to properly keep track of the amount of bytes currently
      allocated, allocated pointers contain tracking information. This
      tracking information is currently limited to the number of bytes
      allocated, so that we can correctly replenish them on calling `free` on
      the pointer. In the future, it would be feasible to extend the tracked
      information even further, e.g. by adding information about file and line
      where the allocation has been performed. As this introduced some
      overhead to allocations though, only information essential to limited
      allocations is currently tracked.
      Patrick Steinhardt committed
  2. 14 May, 2023 1 commit
    • alloc: simplify pluggable allocators · 9dd1bfe8
      Remove the number of functions that custom allocator users need to
      provide; nobody should need to implement `substrdup`. Keep it to the
      basics that are actually _needed_ for allocation (malloc, realloc,
      free) and reimplement the rest ourselves.
      
      In addition, move the failure check and error setting _out_ of the
      custom allocators and into a wrapper so that users don't need to deal
      with this. This also allows us to call our allocator (without the
      wrapper) early so that it does not try to set an error on failure, which
      may be important for bootstrapping.
      Edward Thomson committed
  3. 13 May, 2023 20 commits
  4. 12 May, 2023 3 commits
  5. 11 May, 2023 2 commits
  6. 09 May, 2023 5 commits
  7. 08 May, 2023 6 commits
    • util: detect all possible qsort_r and qsort_s variants · d873966f
      As reported in https://bugs.freebsd.org/271234, recent versions of
      FreeBSD have adjusted the prototype for qsort_r() to match the POSIX
      interface. This causes libgit2's CMake configuration check to fail to
      detect qsort_r(), making it fall back to qsort_s(), which in libgit2
      also has an incompatible interface. With recent versions of clang this
      results in a "incompatible function pointer types" compile error.
      
      Summarizing, there are four variations of 'qsort-with-context':
      * old style BSD qsort_r(), used in FreeBSD 13 and earlier, where the
        comparison function has the context parameter first
      * GNU or POSIX qsort_r(), also used in FreeBSD 14 and later, where the
        comparison function has the context parameter last
      * C11 qsort_s(), where the comparison function has the context parameter
        last
      * Microsoft qsort_s(), where the comparison function has the context
        parameter first
      
      Add explicit detections for all these variants, so they get detected as
      (in the same order as above):
      
      * `GIT_QSORT_R_BSD`
      * `GIT_QSORT_R_GNU`
      * `GIT_QSORT_S_C11`
      * `GIT_QSORT_S_MSC`
      
      An additional complication is that on FreeBSD 14 and later, <stdlib.h>
      uses the C11 _Generic() macro mechanism to automatically select the
      correct qsort_r() prototype, depending on the caller's comparison
      function argument. This breaks CMake's check_prototype_definition()
      functionality, since it tries to redefine the function, and _Generic
      macro is expanded inline causing a compile error.
      
      Work around that problem by putting the function names in parentheses,
      to prevent the preprocessor from using a macro to replace the function
      name.
      
      Also, in `git__qsort_r()`, change the `#if` order so the variants that
      do not have to use glue are preferred.
      Dimitry Andric committed
    • grafts: make `from_file` be `open_or_refresh` · 43db9288
      The semantics of `from_file` are weird - it looks like a function that
      just opens a file, but it actually inspects the pointer, which is
      unexpected and could make things very crashy.
      
      Make an `open` function that just does an open, and move the magic to
      `open_or_refresh` whose name better indicates that it may do weird
      stuff.
      Edward Thomson committed
    • shallow: don't default to -1 for depth · 3388f5ba
      Depth of `0` should indicate full depth. Disallow negative values (they
      may have a future meaning) and use `0` as the default.
      Edward Thomson committed