Blame src/integer.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
#ifndef INCLUDE_integer_h__
Packit ae9e2a
#define INCLUDE_integer_h__
Packit ae9e2a
Packit ae9e2a
/** @return true if p fits into the range of a size_t */
Packit ae9e2a
GIT_INLINE(int) git__is_sizet(git_off_t p)
Packit ae9e2a
{
Packit ae9e2a
	size_t r = (size_t)p;
Packit ae9e2a
	return p == (git_off_t)r;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** @return true if p fits into the range of an ssize_t */
Packit ae9e2a
GIT_INLINE(int) git__is_ssizet(size_t p)
Packit ae9e2a
{
Packit ae9e2a
	ssize_t r = (ssize_t)p;
Packit ae9e2a
	return p == (size_t)r;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** @return true if p fits into the range of a uint32_t */
Packit ae9e2a
GIT_INLINE(int) git__is_uint32(size_t p)
Packit ae9e2a
{
Packit ae9e2a
	uint32_t r = (uint32_t)p;
Packit ae9e2a
	return p == (size_t)r;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** @return true if p fits into the range of an unsigned long */
Packit ae9e2a
GIT_INLINE(int) git__is_ulong(git_off_t p)
Packit ae9e2a
{
Packit ae9e2a
	unsigned long r = (unsigned long)p;
Packit ae9e2a
	return p == (git_off_t)r;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/** @return true if p fits into the range of an int */
Packit ae9e2a
GIT_INLINE(int) git__is_int(long long p)
Packit ae9e2a
{
Packit ae9e2a
	int r = (int)p;
Packit ae9e2a
	return p == (long long)r;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Sets `one + two` into `out`, unless the arithmetic would overflow.
Packit ae9e2a
 * @return true if the result fits in a `uint64_t`, false on overflow.
Packit ae9e2a
 */
Packit ae9e2a
GIT_INLINE(bool) git__add_uint64_overflow(uint64_t *out, uint64_t one, uint64_t two)
Packit ae9e2a
{
Packit ae9e2a
	if (UINT64_MAX - one < two)
Packit ae9e2a
		return true;
Packit ae9e2a
	*out = one + two;
Packit ae9e2a
	return false;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/* Use clang/gcc compiler intrinsics whenever possible */
Packit ae9e2a
#if (SIZE_MAX == ULONG_MAX) && __has_builtin(__builtin_uaddl_overflow)
Packit ae9e2a
# define git__add_sizet_overflow(out, one, two) \
Packit ae9e2a
	__builtin_uaddl_overflow(one, two, out)
Packit ae9e2a
# define git__multiply_sizet_overflow(out, one, two) \
Packit ae9e2a
	__builtin_umull_overflow(one, two, out)
Packit ae9e2a
#elif (SIZE_MAX == UINT_MAX) && __has_builtin(__builtin_uadd_overflow)
Packit ae9e2a
# define git__add_sizet_overflow(out, one, two) \
Packit ae9e2a
	__builtin_uadd_overflow(one, two, out)
Packit ae9e2a
# define git__multiply_sizet_overflow(out, one, two) \
Packit ae9e2a
	__builtin_umul_overflow(one, two, out)
Packit ae9e2a
#else
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Sets `one + two` into `out`, unless the arithmetic would overflow.
Packit ae9e2a
 * @return true if the result fits in a `size_t`, false on overflow.
Packit ae9e2a
 */
Packit ae9e2a
GIT_INLINE(bool) git__add_sizet_overflow(size_t *out, size_t one, size_t two)
Packit ae9e2a
{
Packit ae9e2a
	if (SIZE_MAX - one < two)
Packit ae9e2a
		return true;
Packit ae9e2a
	*out = one + two;
Packit ae9e2a
	return false;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Sets `one * two` into `out`, unless the arithmetic would overflow.
Packit ae9e2a
 * @return true if the result fits in a `size_t`, false on overflow.
Packit ae9e2a
 */
Packit ae9e2a
GIT_INLINE(bool) git__multiply_sizet_overflow(size_t *out, size_t one, size_t two)
Packit ae9e2a
{
Packit ae9e2a
	if (one && SIZE_MAX / one < two)
Packit ae9e2a
		return true;
Packit ae9e2a
	*out = one * two;
Packit ae9e2a
	return false;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
#endif
Packit ae9e2a
Packit ae9e2a
#endif /* INCLUDE_integer_h__ */