summaryrefslogtreecommitdiff
path: root/lib/ccan/cast
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ccan/cast')
-rw-r--r--lib/ccan/cast/LICENSE165
-rw-r--r--lib/ccan/cast/_info84
-rw-r--r--lib/ccan/cast/cast.h129
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_const.c29
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_const2.c29
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_const3.c29
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_signed-const.c22
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_signed-sizesame.c29
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_signed.c17
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_static-2.c23
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_static-3.c21
-rw-r--r--lib/ccan/cast/test/compile_fail-cast_static.c17
-rw-r--r--lib/ccan/cast/test/compile_ok-cast_void.c12
13 files changed, 606 insertions, 0 deletions
diff --git a/lib/ccan/cast/LICENSE b/lib/ccan/cast/LICENSE
new file mode 100644
index 0000000000..cca7fc278f
--- /dev/null
+++ b/lib/ccan/cast/LICENSE
@@ -0,0 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+ 0. Additional Definitions.
+
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
diff --git a/lib/ccan/cast/_info b/lib/ccan/cast/_info
new file mode 100644
index 0000000000..5f82a05b8f
--- /dev/null
+++ b/lib/ccan/cast/_info
@@ -0,0 +1,84 @@
+#include <string.h>
+#include "config.h"
+
+/**
+ * cast - routines for safer casting.
+ *
+ * Often you want to cast in a limited way, such as removing a const or
+ * switching between integer types. However, normal casts will work on
+ * almost any type, making them dangerous when the code changes.
+ *
+ * These C++-inspired macros serve two purposes: they make it clear the
+ * exact reason for the cast, and they also (with some compilers) cause
+ * errors when misused.
+ *
+ * Based on Jan Engelhardt's libHX macros: http://libhx.sourceforge.net/
+ *
+ * Author: Jan Engelhardt
+ * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
+ * License: LGPL
+ *
+ * Example:
+ * // Given "test" contains "3 t's in 'test string'
+ * #include <ccan/cast/cast.h>
+ * #include <stdint.h>
+ * #include <stdio.h>
+ *
+ * // Find char @orig in @str, if @repl, replace them. Return number.
+ * static size_t find_chars(char *str, char orig, char repl)
+ * {
+ * size_t i, count = 0;
+ * for (i = 0; str[i]; i++) {
+ * if (str[i] == orig) {
+ * count++;
+ * if (repl)
+ * str[i] = repl;
+ * }
+ * }
+ * return count;
+ * }
+ *
+ * // Terrible hash function.
+ * static uint64_t hash_string(const unsigned char *str)
+ * {
+ * size_t i;
+ * uint64_t hash = 0;
+ * for (i = 0; str[i]; i++)
+ * hash += str[i];
+ * return hash;
+ * }
+ *
+ * int main(int argc, char *argv[])
+ * {
+ * uint64_t hash;
+ *
+ * // find_chars wants a non-const string, but doesn't
+ * // need it if repl == 0.
+ * printf("%zu %c's in 'test string'\n",
+ * find_chars(cast_const(char *, "test string"),
+ * argv[1][0], 0),
+ * argv[1][0]);
+ *
+ * // hash_string wants an unsigned char.
+ * hash = hash_string(cast_signed(unsigned char *, argv[1]));
+ *
+ * // Need a long long to hand to printf.
+ * printf("Hash of '%s' = %llu\n", argv[1],
+ * cast_static(unsigned long long, hash));
+ * return 0;
+ * }
+ *
+ */
+int main(int argc, char *argv[])
+{
+ /* Expect exactly one argument */
+ if (argc != 2)
+ return 1;
+
+ if (strcmp(argv[1], "depends") == 0) {
+ printf("ccan/build_assert\n");
+ return 0;
+ }
+
+ return 1;
+}
diff --git a/lib/ccan/cast/cast.h b/lib/ccan/cast/cast.h
new file mode 100644
index 0000000000..daebd85723
--- /dev/null
+++ b/lib/ccan/cast/cast.h
@@ -0,0 +1,129 @@
+#ifndef CCAN_CAST_H
+#define CCAN_CAST_H
+#include "config.h"
+#include <stdint.h>
+#include <ccan/build_assert/build_assert.h>
+
+/**
+ * cast_signed - cast a (const) char * to/from (const) signed/unsigned char *.
+ * @type: some char * variant.
+ * @expr: expression (of some char * variant) to cast.
+ *
+ * Some libraries insist on an unsigned char in various places; cast_signed
+ * makes sure (with suitable compiler) that the expression you are casting
+ * only differs in signed/unsigned, not in type or const-ness.
+ */
+#define cast_signed(type, expr) \
+ ((type)(expr) \
+ + BUILD_ASSERT_OR_ZERO(cast_sign_compatible(type, (expr))))
+
+/**
+ * cast_const - remove a const qualifier from a pointer.
+ * @type: some pointer type.
+ * @expr: expression to cast.
+ *
+ * This ensures that you are only removing the const qualifier from an
+ * expression. The expression must otherwise match @type.
+ *
+ * If @type is a pointer to a pointer, you must use cast_const2 (etc).
+ *
+ * Example:
+ * // Dumb open-coded strstr variant.
+ * static char *find_needle(const char *haystack)
+ * {
+ * size_t i;
+ * for (i = 0; i < strlen(haystack); i++)
+ * if (memcmp("needle", haystack+i, strlen("needle")) == 0)
+ * return cast_const(char *, haystack+i);
+ * return NULL;
+ * }
+ */
+#define cast_const(type, expr) \
+ ((type)((intptr_t)(expr) \
+ + BUILD_ASSERT_OR_ZERO(cast_const_compat1((expr), type))))
+
+/**
+ * cast_const2 - remove a const qualifier from a pointer to a pointer.
+ * @type: some pointer to pointer type.
+ * @expr: expression to cast.
+ *
+ * This ensures that you are only removing the const qualifier from an
+ * expression. The expression must otherwise match @type.
+ */
+#define cast_const2(type, expr) \
+ ((type)((intptr_t)(expr) \
+ + BUILD_ASSERT_OR_ZERO(cast_const_compat2((expr), type))))
+
+/**
+ * cast_const3 - remove a const from a pointer to a pointer to a pointer..
+ * @type: some pointer to pointer to pointer type.
+ * @expr: expression to cast.
+ *
+ * This ensures that you are only removing the const qualifier from an
+ * expression. The expression must otherwise match @type.
+ */
+#define cast_const3(type, expr) \
+ ((type)((intptr_t)(expr) \
+ + BUILD_ASSERT_OR_ZERO(cast_const_compat3((expr), type))))
+
+
+/**
+ * cast_static - explicit mimic of implicit cast.
+ * @type: some type.
+ * @expr: expression to cast.
+ *
+ * This ensures that the cast is not to or from a pointer: it can only be
+ * an implicit cast, such as a pointer to a similar const pointer, or between
+ * integral types.
+ */
+#if HAVE_COMPOUND_LITERALS
+#define cast_static(type, expr) \
+ ((struct { type x; }){(expr)}.x)
+#else
+#define cast_static(type, expr) \
+ ((type)(expr))
+#endif
+
+/* Herein lies the gcc magic to evoke compile errors. */
+#if HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
+#define cast_sign_compatible(t, e) \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(__typeof__(t), char *) || \
+ __builtin_types_compatible_p(__typeof__(t), signed char *) || \
+ __builtin_types_compatible_p(__typeof__(t), unsigned char *), \
+ /* if type is not const qualified */ \
+ __builtin_types_compatible_p(__typeof__(e), char *) || \
+ __builtin_types_compatible_p(__typeof__(e), signed char *) || \
+ __builtin_types_compatible_p(__typeof__(e), unsigned char *), \
+ /* and if it is... */ \
+ __builtin_types_compatible_p(__typeof__(e), const char *) || \
+ __builtin_types_compatible_p(__typeof__(e), const signed char *) || \
+ __builtin_types_compatible_p(__typeof__(e), const unsigned char *) ||\
+ __builtin_types_compatible_p(__typeof__(e), char *) || \
+ __builtin_types_compatible_p(__typeof__(e), signed char *) || \
+ __builtin_types_compatible_p(__typeof__(e), unsigned char *) \
+ )
+
+#define cast_const_strip1(expr) \
+ __typeof__(*(struct { int z; __typeof__(expr) x; }){0}.x)
+#define cast_const_strip2(expr) \
+ __typeof__(**(struct { int z; __typeof__(expr) x; }){0}.x)
+#define cast_const_strip3(expr) \
+ __typeof__(***(struct { int z; __typeof__(expr) x; }){0}.x)
+#define cast_const_compat1(expr, type) \
+ __builtin_types_compatible_p(cast_const_strip1(expr), \
+ cast_const_strip1(type))
+#define cast_const_compat2(expr, type) \
+ __builtin_types_compatible_p(cast_const_strip2(expr), \
+ cast_const_strip2(type))
+#define cast_const_compat3(expr, type) \
+ __builtin_types_compatible_p(cast_const_strip3(expr), \
+ cast_const_strip3(type))
+#else
+#define cast_sign_compatible(type, expr) \
+ (sizeof(*(type)0) == 1 && sizeof(*(expr)) == 1)
+#define cast_const_compat1(expr, type) (1)
+#define cast_const_compat2(expr, type) (1)
+#define cast_const_compat3(expr, type) (1)
+#endif
+#endif /* CCAN_CAST_H */
diff --git a/lib/ccan/cast/test/compile_fail-cast_const.c b/lib/ccan/cast/test/compile_fail-cast_const.c
new file mode 100644
index 0000000000..277f3de1c4
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_const.c
@@ -0,0 +1,29 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+ char c;
+};
+
+int main(int argc, char *argv[])
+{
+ char *uc;
+ const
+#ifdef FAIL
+ struct char_struct
+#else
+ char
+#endif
+ *p = NULL;
+
+ uc = cast_const(char *, p);
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_const2.c b/lib/ccan/cast/test/compile_fail-cast_const2.c
new file mode 100644
index 0000000000..e671e88eda
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_const2.c
@@ -0,0 +1,29 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+ char c;
+};
+
+int main(int argc, char *argv[])
+{
+ char **uc;
+ const
+#ifdef FAIL
+ struct char_struct
+#else
+ char
+#endif
+ **p = NULL;
+
+ uc = cast_const2(char **, p);
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_const3.c b/lib/ccan/cast/test/compile_fail-cast_const3.c
new file mode 100644
index 0000000000..e958e2dde5
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_const3.c
@@ -0,0 +1,29 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+ char c;
+};
+
+int main(int argc, char *argv[])
+{
+ char ***uc;
+ const
+#ifdef FAIL
+ struct char_struct
+#else
+ char
+#endif
+ ***p = NULL;
+
+ uc = cast_const3(char ***, p);
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_signed-const.c b/lib/ccan/cast/test/compile_fail-cast_signed-const.c
new file mode 100644
index 0000000000..9971dc8eb3
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_signed-const.c
@@ -0,0 +1,22 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+ unsigned char *uc;
+#ifdef FAIL
+ const
+#endif
+ char
+ *p = NULL;
+
+ uc = cast_signed(unsigned char *, p);
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_const can only use size"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_signed-sizesame.c b/lib/ccan/cast/test/compile_fail-cast_signed-sizesame.c
new file mode 100644
index 0000000000..2bc40b2f46
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_signed-sizesame.c
@@ -0,0 +1,29 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+/* Note: this *isn't* sizeof(char) on all platforms. */
+struct char_struct {
+ char c;
+};
+
+int main(int argc, char *argv[])
+{
+ unsigned char *uc;
+#ifdef FAIL
+ struct char_struct
+#else
+ char
+#endif
+ *p = NULL;
+
+ uc = cast_signed(unsigned char *, p);
+
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_signed can only use size"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_signed.c b/lib/ccan/cast/test/compile_fail-cast_signed.c
new file mode 100644
index 0000000000..66bcc0a1b5
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_signed.c
@@ -0,0 +1,17 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+ unsigned char *uc;
+#ifdef FAIL
+ int
+#else
+ char
+#endif
+ *p = NULL;
+
+ uc = cast_signed(unsigned char *, p);
+ (void) uc; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
diff --git a/lib/ccan/cast/test/compile_fail-cast_static-2.c b/lib/ccan/cast/test/compile_fail-cast_static-2.c
new file mode 100644
index 0000000000..8a12025384
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_static-2.c
@@ -0,0 +1,23 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+ char *c;
+#ifdef FAIL
+ long
+#else
+ char
+#endif
+ *p = 0;
+
+ c = cast_static(char *, p);
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_COMPOUND_LITERALS
+#error "Unfortunately we don't fail if cast_static is a noop"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_static-3.c b/lib/ccan/cast/test/compile_fail-cast_static-3.c
new file mode 100644
index 0000000000..6296b75276
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_static-3.c
@@ -0,0 +1,21 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+ char *c;
+#ifdef FAIL
+ const
+#endif
+ char *p = 0;
+
+ c = cast_static(char *, p);
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
+
+#ifdef FAIL
+#if !HAVE_COMPOUND_LITERALS
+#error "Unfortunately we don't fail if cast_static is a noop"
+#endif
+#endif
diff --git a/lib/ccan/cast/test/compile_fail-cast_static.c b/lib/ccan/cast/test/compile_fail-cast_static.c
new file mode 100644
index 0000000000..0f9e478047
--- /dev/null
+++ b/lib/ccan/cast/test/compile_fail-cast_static.c
@@ -0,0 +1,17 @@
+#include <ccan/cast/cast.h>
+#include <stdlib.h>
+
+int main(int argc, char *argv[])
+{
+ char c;
+#ifdef FAIL
+ char *
+#else
+ long
+#endif
+ x = 0;
+
+ c = cast_static(char, x);
+ (void) c; /* Suppress unused-but-set-variable warning. */
+ return 0;
+}
diff --git a/lib/ccan/cast/test/compile_ok-cast_void.c b/lib/ccan/cast/test/compile_ok-cast_void.c
new file mode 100644
index 0000000000..c649d283b3
--- /dev/null
+++ b/lib/ccan/cast/test/compile_ok-cast_void.c
@@ -0,0 +1,12 @@
+#include <ccan/cast/cast.h>
+
+static void *remove_void(const void *p)
+{
+ return cast_const(void *, p);
+}
+
+int main(void)
+{
+ void *p = remove_void("foo");
+ return !p;
+}