Blame src/common.h

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#ifndef INCLUDE_common_h__
Packit Service 20376f
#define INCLUDE_common_h__
Packit Service 20376f
Packit Service 20376f
#include "git2/common.h"
Packit Service 20376f
#include "cc-compat.h"
Packit Service 20376f
Packit Service 20376f
/** Declare a function as always inlined. */
Packit Service 20376f
#if defined(_MSC_VER)
Packit Service 20376f
# define GIT_INLINE(type) static __inline type
Packit Service 20376f
#else
Packit Service 20376f
# define GIT_INLINE(type) static inline type
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/** Support for gcc/clang __has_builtin intrinsic */
Packit Service 20376f
#ifndef __has_builtin
Packit Service 20376f
# define __has_builtin(x) 0
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#include <assert.h>
Packit Service 20376f
#include <errno.h>
Packit Service 20376f
#include <limits.h>
Packit Service 20376f
#include <stdlib.h>
Packit Service 20376f
#include <stdio.h>
Packit Service 20376f
#include <string.h>
Packit Service 20376f
Packit Service 20376f
#include <sys/types.h>
Packit Service 20376f
#include <sys/stat.h>
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
Packit Service 20376f
# include <io.h>
Packit Service 20376f
# include <direct.h>
Packit Service 20376f
# include <winsock2.h>
Packit Service 20376f
# include <windows.h>
Packit Service 20376f
# include <ws2tcpip.h>
Packit Service 20376f
# include "win32/msvc-compat.h"
Packit Service 20376f
# include "win32/mingw-compat.h"
Packit Service 20376f
# include "win32/win32-compat.h"
Packit Service 20376f
# include "win32/error.h"
Packit Service 20376f
# include "win32/version.h"
Packit Service 20376f
# ifdef GIT_THREADS
Packit Service 20376f
#	include "win32/thread.h"
Packit Service 20376f
# endif
Packit Service 20376f
# if defined(GIT_MSVC_CRTDBG)
Packit Service 20376f
#   include "win32/w32_stack.h"
Packit Service 20376f
#   include "win32/w32_crtdbg_stacktrace.h"
Packit Service 20376f
# endif
Packit Service 20376f
Packit Service 20376f
#else
Packit Service 20376f
Packit Service 20376f
# include <unistd.h>
Packit Service 20376f
# include <strings.h>
Packit Service 20376f
# ifdef GIT_THREADS
Packit Service 20376f
#	include <pthread.h>
Packit Service 20376f
#	include <sched.h>
Packit Service 20376f
# endif
Packit Service 20376f
#define GIT_STDLIB_CALL
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_USE_STAT_ATIMESPEC
Packit Service 20376f
# define st_atim st_atimespec
Packit Service 20376f
# define st_ctim st_ctimespec
Packit Service 20376f
# define st_mtim st_mtimespec
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
# include <arpa/inet.h>
Packit Service 20376f
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#include "git2/types.h"
Packit Service 20376f
#include "git2/errors.h"
Packit Service 20376f
#include "thread-utils.h"
Packit Service 20376f
#include "integer.h"
Packit Service 20376f
Packit Service 20376f
#include <regex.h>
Packit Service 20376f
Packit Service 20376f
#define DEFAULT_BUFSIZE 65536
Packit Service 20376f
#define FILEIO_BUFSIZE DEFAULT_BUFSIZE
Packit Service 20376f
#define FILTERIO_BUFSIZE DEFAULT_BUFSIZE
Packit Service 20376f
#define NETIO_BUFSIZE DEFAULT_BUFSIZE
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check a pointer allocation result, returning -1 if it failed.
Packit Service 20376f
 */
Packit Service 20376f
#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check a buffer allocation result, returning -1 if it failed.
Packit Service 20376f
 */
Packit Service 20376f
#define GITERR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; }
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check a return value and propagate result if non-zero.
Packit Service 20376f
 */
Packit Service 20376f
#define GITERR_CHECK_ERROR(code) \
Packit Service 20376f
	do { int _err = (code); if (_err) return _err; } while (0)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set the error message for this thread, formatting as needed.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
void giterr_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set the error message for a regex failure, using the internal regex
Packit Service 20376f
 * error code lookup and return a libgit error code.
Packit Service 20376f
 */
Packit Service 20376f
int giterr_set_regex(const regex_t *regex, int error_code);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Set error message for user callback if needed.
Packit Service 20376f
 *
Packit Service 20376f
 * If the error code in non-zero and no error message is set, this
Packit Service 20376f
 * sets a generic error message.
Packit Service 20376f
 *
Packit Service 20376f
 * @return This always returns the `error_code` parameter.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) giterr_set_after_callback_function(
Packit Service 20376f
	int error_code, const char *action)
Packit Service 20376f
{
Packit Service 20376f
	if (error_code) {
Packit Service 20376f
		const git_error *e = giterr_last();
Packit Service 20376f
		if (!e || !e->message)
Packit Service 20376f
			giterr_set(e ? e->klass : GITERR_CALLBACK,
Packit Service 20376f
				"%s callback returned %d", action, error_code);
Packit Service 20376f
	}
Packit Service 20376f
	return error_code;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
#define giterr_set_after_callback(code) \
Packit Service 20376f
	giterr_set_after_callback_function((code), __FUNCTION__)
Packit Service 20376f
#else
Packit Service 20376f
#define giterr_set_after_callback(code) \
Packit Service 20376f
	giterr_set_after_callback_function((code), __func__)
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Gets the system error code for this thread.
Packit Service 20376f
 */
Packit Service 20376f
int giterr_system_last(void);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Sets the system error code for this thread.
Packit Service 20376f
 */
Packit Service 20376f
void giterr_system_set(int code);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Structure to preserve libgit2 error state
Packit Service 20376f
 */
Packit Service 20376f
typedef struct {
Packit Service 20376f
	int error_code;
Packit Service 20376f
	unsigned int oom : 1;
Packit Service 20376f
	git_error error_msg;
Packit Service 20376f
} git_error_state;
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Capture current error state to restore later, returning error code.
Packit Service 20376f
 * If `error_code` is zero, this does not clear the current error state.
Packit Service 20376f
 * You must either restore this error state, or free it.
Packit Service 20376f
 */
Packit Service 20376f
extern int giterr_state_capture(git_error_state *state, int error_code);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Restore error state to a previous value, returning saved error code.
Packit Service 20376f
 */
Packit Service 20376f
extern int giterr_state_restore(git_error_state *state);
Packit Service 20376f
Packit Service 20376f
/** Free an error state. */
Packit Service 20376f
extern void giterr_state_free(git_error_state *state);
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Check a versioned structure for validity
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int actual;
Packit Service 20376f
Packit Service 20376f
	if (!structure)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	actual = *(const unsigned int*)structure;
Packit Service 20376f
	if (actual > 0 && actual <= expected_max)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	giterr_set(GITERR_INVALID, "invalid version %d on %s", actual, name);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
#define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Initialize a structure with a version.
Packit Service 20376f
 */
Packit Service 20376f
GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version)
Packit Service 20376f
{
Packit Service 20376f
	memset(structure, 0, len);
Packit Service 20376f
	*((int*)structure) = version;
Packit Service 20376f
}
Packit Service 20376f
#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)
Packit Service 20376f
Packit Service 20376f
#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \
Packit Service 20376f
	TYPE _tmpl = TPL; \
Packit Service 20376f
	GITERR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE);	\
Packit Service 20376f
	memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0)
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/** Check for additive overflow, setting an error if would occur. */
Packit Service 20376f
#define GIT_ADD_SIZET_OVERFLOW(out, one, two) \
Packit Service 20376f
	(git__add_sizet_overflow(out, one, two) ? (giterr_set_oom(), 1) : 0)
Packit Service 20376f
Packit Service 20376f
/** Check for additive overflow, setting an error if would occur. */
Packit Service 20376f
#define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \
Packit Service 20376f
	(git__multiply_sizet_overflow(out, nelem, elsize) ? (giterr_set_oom(), 1) : 0)
Packit Service 20376f
Packit Service 20376f
/** Check for additive overflow, failing if it would occur. */
Packit Service 20376f
#define GITERR_CHECK_ALLOC_ADD(out, one, two) \
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; }
Packit Service 20376f
Packit Service 20376f
#define GITERR_CHECK_ALLOC_ADD3(out, one, two, three) \
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; }
Packit Service 20376f
Packit Service 20376f
#define GITERR_CHECK_ALLOC_ADD4(out, one, two, three, four) \
Packit Service 20376f
	if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \
Packit Service 20376f
		GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; }
Packit Service 20376f
Packit Service 20376f
/** Check for multiplicative overflow, failing if it would occur. */
Packit Service 20376f
#define GITERR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \
Packit Service 20376f
	if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; }
Packit Service 20376f
Packit Service 20376f
/* NOTE: other giterr functions are in the public errors.h header file */
Packit Service 20376f
Packit Service 20376f
#include "util.h"
Packit Service 20376f
Packit Service 20376f
#endif /* INCLUDE_common_h__ */