1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_common_h__
8 #define INCLUDE_common_h__
9 
10 #ifndef LIBGIT2_NO_FEATURES_H
11 # include "git2/sys/features.h"
12 #endif
13 
14 #include "git2/common.h"
15 #include "cc-compat.h"
16 
17 /** Declare a function as always inlined. */
18 #if defined(_MSC_VER)
19 # define GIT_INLINE(type) static __inline type
20 #else
21 # define GIT_INLINE(type) static inline type
22 #endif
23 
24 /** Support for gcc/clang __has_builtin intrinsic */
25 #ifndef __has_builtin
26 # define __has_builtin(x) 0
27 #endif
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 
39 #ifdef GIT_WIN32
40 
41 # include <io.h>
42 # include <direct.h>
43 # include <winsock2.h>
44 # include <windows.h>
45 # include <ws2tcpip.h>
46 # include "win32/msvc-compat.h"
47 # include "win32/mingw-compat.h"
48 # include "win32/win32-compat.h"
49 # include "win32/error.h"
50 # include "win32/version.h"
51 # ifdef GIT_THREADS
52 #	include "win32/thread.h"
53 # endif
54 
55 #else
56 
57 # include <unistd.h>
58 # include <strings.h>
59 # ifdef GIT_THREADS
60 #	include <pthread.h>
61 #	include <sched.h>
62 # endif
63 #define GIT_STDLIB_CALL
64 
65 #ifdef GIT_USE_STAT_ATIMESPEC
66 # define st_atim st_atimespec
67 # define st_ctim st_ctimespec
68 # define st_mtim st_mtimespec
69 #endif
70 
71 # include <arpa/inet.h>
72 
73 #endif
74 
75 #include "git2/types.h"
76 #include "git2/errors.h"
77 #include "thread-utils.h"
78 #include "integer.h"
79 
80 #include <regex.h>
81 
82 #define DEFAULT_BUFSIZE 65536
83 #define FILEIO_BUFSIZE DEFAULT_BUFSIZE
84 #define FILTERIO_BUFSIZE DEFAULT_BUFSIZE
85 #define NETIO_BUFSIZE DEFAULT_BUFSIZE
86 
87 /**
88  * Check a pointer allocation result, returning -1 if it failed.
89  */
90 #define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
91 
92 /**
93  * Check a buffer allocation result, returning -1 if it failed.
94  */
95 #define GITERR_CHECK_ALLOC_BUF(buf) if ((void *)(buf) == NULL || git_buf_oom(buf)) { return -1; }
96 
97 /**
98  * Check a return value and propagate result if non-zero.
99  */
100 #define GITERR_CHECK_ERROR(code) \
101 	do { int _err = (code); if (_err) return _err; } while (0)
102 
103 /**
104  * Set the error message for this thread, formatting as needed.
105  */
106 
107 void giterr_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
108 
109 /**
110  * Set the error message for a regex failure, using the internal regex
111  * error code lookup and return a libgit error code.
112  */
113 int giterr_set_regex(const regex_t *regex, int error_code);
114 
115 /**
116  * Set error message for user callback if needed.
117  *
118  * If the error code in non-zero and no error message is set, this
119  * sets a generic error message.
120  *
121  * @return This always returns the `error_code` parameter.
122  */
giterr_set_after_callback_function(int error_code,const char * action)123 GIT_INLINE(int) giterr_set_after_callback_function(
124 	int error_code, const char *action)
125 {
126 	if (error_code) {
127 		const git_error *e = giterr_last();
128 		if (!e || !e->message)
129 			giterr_set(e ? e->klass : GITERR_CALLBACK,
130 				"%s callback returned %d", action, error_code);
131 	}
132 	return error_code;
133 }
134 
135 #ifdef GIT_WIN32
136 #define giterr_set_after_callback(code) \
137 	giterr_set_after_callback_function((code), __FUNCTION__)
138 #else
139 #define giterr_set_after_callback(code) \
140 	giterr_set_after_callback_function((code), __func__)
141 #endif
142 
143 /**
144  * Gets the system error code for this thread.
145  */
146 int giterr_system_last(void);
147 
148 /**
149  * Sets the system error code for this thread.
150  */
151 void giterr_system_set(int code);
152 
153 /**
154  * Structure to preserve libgit2 error state
155  */
156 typedef struct {
157 	int error_code;
158 	unsigned int oom : 1;
159 	git_error error_msg;
160 } git_error_state;
161 
162 /**
163  * Capture current error state to restore later, returning error code.
164  * If `error_code` is zero, this does not clear the current error state.
165  * You must either restore this error state, or free it.
166  */
167 extern int giterr_state_capture(git_error_state *state, int error_code);
168 
169 /**
170  * Restore error state to a previous value, returning saved error code.
171  */
172 extern int giterr_state_restore(git_error_state *state);
173 
174 /** Free an error state. */
175 extern void giterr_state_free(git_error_state *state);
176 
177 /**
178  * Check a versioned structure for validity
179  */
giterr__check_version(const void * structure,unsigned int expected_max,const char * name)180 GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
181 {
182 	unsigned int actual;
183 
184 	if (!structure)
185 		return 0;
186 
187 	actual = *(const unsigned int*)structure;
188 	if (actual > 0 && actual <= expected_max)
189 		return 0;
190 
191 	giterr_set(GITERR_INVALID, "invalid version %d on %s", actual, name);
192 	return -1;
193 }
194 #define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1
195 
196 /**
197  * Initialize a structure with a version.
198  */
git__init_structure(void * structure,size_t len,unsigned int version)199 GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version)
200 {
201 	memset(structure, 0, len);
202 	*((int*)structure) = version;
203 }
204 #define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)
205 
206 #define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \
207 	TYPE _tmpl = TPL; \
208 	GITERR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE);	\
209 	memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0)
210 
211 
212 /** Check for additive overflow, setting an error if would occur. */
213 #define GIT_ADD_SIZET_OVERFLOW(out, one, two) \
214 	(git__add_sizet_overflow(out, one, two) ? (giterr_set_oom(), 1) : 0)
215 
216 /** Check for additive overflow, setting an error if would occur. */
217 #define GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize) \
218 	(git__multiply_sizet_overflow(out, nelem, elsize) ? (giterr_set_oom(), 1) : 0)
219 
220 /** Check for additive overflow, failing if it would occur. */
221 #define GITERR_CHECK_ALLOC_ADD(out, one, two) \
222 	if (GIT_ADD_SIZET_OVERFLOW(out, one, two)) { return -1; }
223 
224 #define GITERR_CHECK_ALLOC_ADD3(out, one, two, three) \
225 	if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \
226 		GIT_ADD_SIZET_OVERFLOW(out, *(out), three)) { return -1; }
227 
228 #define GITERR_CHECK_ALLOC_ADD4(out, one, two, three, four) \
229 	if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \
230 		GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \
231 		GIT_ADD_SIZET_OVERFLOW(out, *(out), four)) { return -1; }
232 
233 #define GITERR_CHECK_ALLOC_ADD5(out, one, two, three, four, five) \
234 	if (GIT_ADD_SIZET_OVERFLOW(out, one, two) || \
235 		GIT_ADD_SIZET_OVERFLOW(out, *(out), three) || \
236 		GIT_ADD_SIZET_OVERFLOW(out, *(out), four) || \
237 		GIT_ADD_SIZET_OVERFLOW(out, *(out), five)) { return -1; }
238 
239 /** Check for multiplicative overflow, failing if it would occur. */
240 #define GITERR_CHECK_ALLOC_MULTIPLY(out, nelem, elsize) \
241 	if (GIT_MULTIPLY_SIZET_OVERFLOW(out, nelem, elsize)) { return -1; }
242 
243 /* NOTE: other giterr functions are in the public errors.h header file */
244 
245 #include "util.h"
246 
247 #endif
248