Skip to content

Commit 122133d

Browse files
lib/: Use the comma operator to perform lvalue conversion
Compound literals are lvalues, and thus somewhat dangerous. Their address can be taken, and they can be assigned to. We were using statement expressions to perform lvalue conversion on compound literals, transforming them to rvalues, and thus removing their dangers. However, statement expressions are non-standard, and quite complex within the compiler, so it would be interesting to use simpler compiler features to achieve the same. The comma operator also performs lvalue conversion, and we can use a dummy (void)0 expression to introduce it. This is significantly simpler, and is more portable than the statement expression (it is valid all the way back to ANSI C89). By using a simpler feature, we have a smaller risk of running into a compiler bug. Suggested-by: Martin Uecker <uecker@tugraz.at> Cc: Christopher Bazley <chris.bazley@arm.com> Cc: Kees Cook <kees@kernel.org> Cc: Richard Russon <rich@flatcap.org> Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent 6d98370 commit 122133d

7 files changed

Lines changed: 23 additions & 21 deletions

File tree

lib/alloc/calloc.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
// calloc_T - calloc type-safe
1818
#define calloc_T(n, T) calloc_T_(n, typeas(T))
1919
#define calloc_T_(n, T) \
20-
({ \
21-
(T *){calloc(n, sizeof(T))}; \
22-
})
20+
( \
21+
(void)0, \
22+
(T *){calloc(n, sizeof(T))} \
23+
)
2324

2425

2526
// xcalloc_T - exit-on-error calloc type-safe

lib/alloc/malloc.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
// malloc_T - malloc type-safe
1919
#define malloc_T(n, T) malloc_T_(n, typeas(T))
2020
#define malloc_T_(n, T) \
21-
({ \
22-
(T *){mallocarray(n, sizeof(T))}; \
23-
})
21+
( \
22+
(void)0, \
23+
(T *){mallocarray(n, sizeof(T))} \
24+
)
2425

2526

2627
// xmalloc_T - exit-on-error malloc type-safe

lib/alloc/realloc.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
// realloc_T - realloc type-safe
1818
#define realloc_T(p, n, T) realloc_T_(p, n, typeas(T))
1919
#define realloc_T_(p, n, T) \
20-
({ \
21-
_Generic(p, T *: (void)0); \
22-
(T *){reallocarray_(p, n, sizeof(T))}; \
23-
})
20+
( \
21+
_Generic(p, T *: (void)0), \
22+
(T *){reallocarray_(p, n, sizeof(T))} \
23+
)
2424

2525
#define reallocarray_(p, n, size) reallocarray(p, (n) ?: 1, (size) ?: 1)
2626

lib/alloc/reallocf.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
// reallocf_T - realloc free-on-error type-safe
1919
#define reallocf_T(p, n, T) reallocf_T_(p, n, typeas(T))
2020
#define reallocf_T_(p, n, T) \
21-
({ \
22-
_Generic(p, T *: (void)0); \
23-
(T *){reallocarrayf_(p, n, sizeof(T))}; \
24-
})
21+
( \
22+
_Generic(p, T *: (void)0), \
23+
(T *){reallocarrayf_(p, n, sizeof(T))} \
24+
)
2525

2626
#define reallocarrayf_(p, n, size) reallocarrayf(p, (n) ?: 1, (size) ?: 1)
2727

lib/search/l/lfind.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
// lfind_T - linear find type-safe
1919
#define lfind_T(T, ...) lfind_T_(typeas(T), __VA_ARGS__)
2020
#define lfind_T_(T, k, a, n, cmp) \
21-
({ \
22-
_Generic(k, T *: (void)0, const T *: (void)0); \
23-
_Generic(a, T *: (void)0, const T *: (void)0); \
24-
(T *){lfind_(k, a, n, sizeof(T), cmp)}; \
25-
})
21+
( \
22+
_Generic(k, T *: (void)0, const T *: (void)0), \
23+
_Generic(a, T *: (void)0, const T *: (void)0), \
24+
(T *){lfind_(k, a, n, sizeof(T), cmp)} \
25+
)
2626

2727
#define LFIND(T, ...) lfind_T(T, __VA_ARGS__, CMP(T))
2828

lib/sizeof.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#define typeas(T) typeof((T){0})
1919

20-
#define ssizeof(x) ({(ssize_t){sizeof(x)};})
20+
#define ssizeof(x) ((void)0, (ssize_t){sizeof(x)})
2121
#define memberof(T, member) ((T){}.member)
2222
#define WIDTHOF(x) (sizeof(x) * CHAR_BIT)
2323

lib/string/strerrno.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
// strerrno - string errno
16-
#define strerrno() ({(const char *){strerror(errno)};})
16+
#define strerrno() ((void)0, (const char *){strerror(errno)})
1717

1818

1919
#endif // include guard

0 commit comments

Comments
 (0)