summaryrefslogtreecommitdiff
path: root/src/integer.h
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-10-03 09:39:42 +0200
committerPatrick Steinhardt <ps@pks.im>2019-10-03 12:23:52 +0200
commit5cf17e0f269387f8345201aef482a10c96490d95 (patch)
treec9999766ad5a8e7293805bef3a06ad1b4c45a25b /src/integer.h
parent5988cf34f0c77a426510c3bff665fd8a1e8cceb9 (diff)
downloadlibgit2-5cf17e0f269387f8345201aef482a10c96490d95.tar.gz
commit_list: store in/out-degrees as uint16_t
The commit list's in- and out-degrees are currently stored as `unsigned short`. When assigning it the value of `git_array_size`, which returns an `size_t`, this generates a warning on some Win32 platforms due to loosing precision. We could just cast the returned value of `git_array_size`, which would work fine for 99.99% of all cases as commits typically have less than 2^16 parents. For crafted commits though we might end up with a wrong value, and thus we should definitely check whether the array size actually fits into the field. To ease the check, let's convert the fields to store the degrees as `uint16_t`. We shouldn't rely on such unspecific types anyway, as it may lead to different behaviour across platforms. Furthermore, this commit introduces a new `git__is_uint16` function to check whether it actually fits -- if not, we return an error.
Diffstat (limited to 'src/integer.h')
-rw-r--r--src/integer.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/integer.h b/src/integer.h
index 10b109737..4738e9e35 100644
--- a/src/integer.h
+++ b/src/integer.h
@@ -21,6 +21,13 @@ GIT_INLINE(int) git__is_ssizet(size_t p)
return p == (size_t)r;
}
+/** @return true if p fits into the range of a uint16_t */
+GIT_INLINE(int) git__is_uint16(size_t p)
+{
+ uint16_t r = (uint16_t)p;
+ return p == (size_t)r;
+}
+
/** @return true if p fits into the range of a uint32_t */
GIT_INLINE(int) git__is_uint32(size_t p)
{