Implement lttng_strncpy safe string copy
[lttng-tools.git] / src / common / macros.h
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef _MACROS_H
20 #define _MACROS_H
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 /*
26 * Takes a pointer x and transform it so we can use it to access members
27 * without a function call. Here an example:
28 *
29 * #define GET_SIZE(x) LTTNG_REF(x)->size
30 *
31 * struct { int size; } s;
32 *
33 * printf("size : %d\n", GET_SIZE(&s));
34 *
35 * For this example we can't use something like this for compatibility purpose
36 * since this will fail:
37 *
38 * #define GET_SIZE(x) x->size;
39 *
40 * This is mostly use for the compatibility layer of lttng-tools. See
41 * poll/epoll for a good example. Since x can be on the stack or allocated
42 * memory using malloc(), we must use generic accessors for compat in order to
43 * *not* use a function to access members and not the variable name.
44 */
45 #define LTTNG_REF(x) ((typeof(*x) *)(x))
46
47 /*
48 * Memory allocation zeroed
49 */
50 static inline
51 void *zmalloc(size_t len)
52 {
53 return calloc(1, len);
54 }
55
56 #ifndef ARRAY_SIZE
57 #define ARRAY_SIZE(array) (sizeof(array) / (sizeof((array)[0])))
58 #endif
59
60 #ifndef max
61 #define max(a, b) ((a) > (b) ? (a) : (b))
62 #endif
63
64 #ifndef max_t
65 #define max_t(type, a, b) ((type) max(a, b))
66 #endif
67
68 #ifndef min
69 #define min(a, b) ((a) < (b) ? (a) : (b))
70 #endif
71
72 #ifndef LTTNG_PACKED
73 #define LTTNG_PACKED __attribute__((__packed__))
74 #endif
75
76 #ifndef LTTNG_HIDDEN
77 #define LTTNG_HIDDEN __attribute__((visibility("hidden")))
78 #endif
79
80 /*
81 * lttng_strncpy returns 0 on success, or nonzero on failure.
82 * It checks that the @src string fits into @dst_len before performing
83 * the copy. On failure, no copy has been performed.
84 *
85 * dst_len includes the string's trailing NULL.
86 */
87 static inline
88 int lttng_strncpy(char *dst, const char *src, size_t dst_len)
89 {
90 if (strnlen(src, dst_len) == dst_len) {
91 /* Fail since copying would result in truncation. */
92 return -1;
93 }
94 strncpy(dst, src, dst_len);
95 /*
96 * Be extra careful and put final \0 at the end after strncpy(),
97 * even though we checked the length before. This makes Coverity
98 * happy.
99 */
100 dst[dst_len - 1] = '\0';
101 return 0;
102 }
103
104 #endif /* _MACROS_H */
This page took 0.030788 seconds and 4 git commands to generate.