[Spice-devel] [PATCH 3/3] Protect C template files from direct inclusion

Christophe Fergeau cfergeau at redhat.com
Tue May 13 05:51:37 PDT 2014


Since these files ABI is dependent on #defines which are set when the
file is included, these checks make sure these files do not get included
in 'unexpected' places.
The goal is to move spice-common client code to only include these files
from one file with the necessary #defines, and to stop having the needed
defines in the global CPPFLAGS. Requiring some #define to be set before
inclusion will ensure these files don't get included from unexpected
locations.
---
 common/lz.c                           | 2 ++
 common/quic.c                         | 3 +++
 common/templates/canvas_base.c        | 4 ++++
 common/templates/canvas_base.h        | 4 ++++
 common/templates/gdi_canvas.c         | 5 +++++
 common/templates/gdi_canvas.h         | 6 ++++++
 common/templates/gl_canvas.c          | 5 +++++
 common/templates/gl_canvas.h          | 6 ++++++
 common/templates/lz_compress_tmpl.c   | 4 ++++
 common/templates/lz_decompress_tmpl.c | 4 ++++
 common/templates/quic_family_tmpl.c   | 4 ++++
 common/templates/quic_rgb_tmpl.c      | 4 ++++
 common/templates/quic_tmpl.c          | 4 ++++
 common/templates/sw_canvas.c          | 5 +++++
 common/templates/sw_canvas.h          | 6 ++++++
 15 files changed, 66 insertions(+)

diff --git a/common/lz.c b/common/lz.c
index 9b78d57..e61edc2 100644
--- a/common/lz.c
+++ b/common/lz.c
@@ -54,6 +54,8 @@
 #define HASH_SIZE (1 << HASH_LOG)
 #define HASH_MASK (HASH_SIZE - 1)
 
+/* Must be defined before including any of the lz_*_tmpl.c files */
+#define SPICE_COMMON_LZ_IMPLEMENTATION
 
 typedef struct LzImageSegment LzImageSegment;
 struct LzImageSegment {
diff --git a/common/quic.c b/common/quic.c
index 42c711b..e7f6bc4 100644
--- a/common/quic.c
+++ b/common/quic.c
@@ -27,6 +27,9 @@
 #include "spice_common.h"
 #include "bitops.h"
 
+/* Must be defined before including any of the quic*_tmpl.c files */
+#define SPICE_COMMON_QUIC_IMPLEMENTATION
+
 #define RLE
 #define RLE_STAT
 #define PRED_1
diff --git a/common/templates/canvas_base.c b/common/templates/canvas_base.c
index c07ecad..ea44886 100644
--- a/common/templates/canvas_base.c
+++ b/common/templates/canvas_base.c
@@ -20,6 +20,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_CANVAS_INTERNAL
+#error "canvas_base.c is internal to spice-common"
+#endif
+
 #include <stdarg.h>
 #include <stdlib.h>
 #include <setjmp.h>
diff --git a/common/templates/canvas_base.h b/common/templates/canvas_base.h
index 63e0fa6..41d9408 100644
--- a/common/templates/canvas_base.h
+++ b/common/templates/canvas_base.h
@@ -29,6 +29,10 @@
 #include <windows.h>
 #endif
 
+#ifndef SPICE_COMMON_CANVAS_INTERNAL
+#error "canvas_base.h is internal to spice-common"
+#endif
+
 SPICE_BEGIN_DECLS
 
 typedef void (*spice_destroy_fn_t)(void *data);
diff --git a/common/templates/gdi_canvas.c b/common/templates/gdi_canvas.c
index deb7649..ee0da88 100644
--- a/common/templates/gdi_canvas.c
+++ b/common/templates/gdi_canvas.c
@@ -22,10 +22,15 @@
 #include <config.h>
 #endif
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "gdi_canvas.c should only be used by canvas implementations"
+#endif
+
 #include <windows.h>
 #include <wingdi.h>
 #include "gdi_canvas.h"
 #define GDI_CANVAS
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "canvas_base.c"
 #include "rop3.h"
 #include "rect.h"
diff --git a/common/templates/gdi_canvas.h b/common/templates/gdi_canvas.h
index 42a5ab0..e561e4f 100644
--- a/common/templates/gdi_canvas.h
+++ b/common/templates/gdi_canvas.h
@@ -22,8 +22,14 @@
 #include <stdint.h>
 #include <spice/macros.h>
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "gdi_canvas.h should only be used by canvas implementations"
+#endif
+
 #include "common/pixman_utils.h"
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "common/templates/canvas_base.h"
+#undef SPICE_COMMON_CANVAS_INTERNAL
 #include "common/region.h"
 
 SPICE_BEGIN_DECLS
diff --git a/common/templates/gl_canvas.c b/common/templates/gl_canvas.c
index fe152ef..7f68eb9 100644
--- a/common/templates/gl_canvas.c
+++ b/common/templates/gl_canvas.c
@@ -19,6 +19,10 @@
 #include <config.h>
 #endif
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "gl_canvas.c should only be used by canvas implementations"
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -30,6 +34,7 @@
 #include "glc.h"
 
 #define GL_CANVAS
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "canvas_base.c"
 
 typedef struct GLCanvas GLCanvas;
diff --git a/common/templates/gl_canvas.h b/common/templates/gl_canvas.h
index e880c65..75a64b6 100644
--- a/common/templates/gl_canvas.h
+++ b/common/templates/gl_canvas.h
@@ -21,8 +21,14 @@
 
 #include <spice/macros.h>
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "gl_canvas.h should only be used by canvas implementations"
+#endif
+
 #include "common/glc.h"
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "common/templates/canvas_base.h"
+#undef SPICE_COMMON_CANVAS_INTERNAL
 #include "common/region.h"
 
 SPICE_BEGIN_DECLS
diff --git a/common/templates/lz_compress_tmpl.c b/common/templates/lz_compress_tmpl.c
index b5d0049..de8920f 100644
--- a/common/templates/lz_compress_tmpl.c
+++ b/common/templates/lz_compress_tmpl.c
@@ -44,6 +44,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_LZ_IMPLEMENTATION
+#error "lz_compress_tmpl.c should only be used by QUIC implementations"
+#endif
+
 #define DJB2_START 5381;
 #define DJB2_HASH(hash, c) (hash = ((hash << 5) + hash) ^ (c)) //|{hash = ((hash << 5) + hash) + c;}
 
diff --git a/common/templates/lz_decompress_tmpl.c b/common/templates/lz_decompress_tmpl.c
index 04a5121..cf38664 100644
--- a/common/templates/lz_decompress_tmpl.c
+++ b/common/templates/lz_decompress_tmpl.c
@@ -63,6 +63,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_LZ_IMPLEMENTATION
+#error "lz_decompress_tmpl.c should only be used by QUIC implementations"
+#endif
+
 #if !defined(LZ_RGB_ALPHA)
 #define COPY_PIXEL(p, out) (*out++ = p)
 #define COPY_REF_PIXEL(ref, out) (*out++ = *ref++)
diff --git a/common/templates/quic_family_tmpl.c b/common/templates/quic_family_tmpl.c
index 12ef62f..5ef3938 100644
--- a/common/templates/quic_family_tmpl.c
+++ b/common/templates/quic_family_tmpl.c
@@ -19,6 +19,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_QUIC_IMPLEMENTATION
+#error "quic_family_tmpl.c should only be used by QUIC implementations"
+#endif
+
 #ifdef QUIC_FAMILY_8BPC
 #undef QUIC_FAMILY_8BPC
 #define FNAME(name) name##_8bpc
diff --git a/common/templates/quic_rgb_tmpl.c b/common/templates/quic_rgb_tmpl.c
index 19cc348..620484b 100644
--- a/common/templates/quic_rgb_tmpl.c
+++ b/common/templates/quic_rgb_tmpl.c
@@ -19,6 +19,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_QUIC_IMPLEMENTATION
+#error "quic_rgb_tmpl.c should only be used by QUIC implementations"
+#endif
+
 #ifdef QUIC_RGB32
 #undef QUIC_RGB32
 #define PIXEL rgb32_pixel_t
diff --git a/common/templates/quic_tmpl.c b/common/templates/quic_tmpl.c
index b1ddbc4..4cd608e 100644
--- a/common/templates/quic_tmpl.c
+++ b/common/templates/quic_tmpl.c
@@ -19,6 +19,10 @@
 #include <config.h>
 #endif
 
+#ifndef SPICE_COMMON_QUIC_IMPLEMENTATION
+#error "quic_tmpl.c should only be used by QUIC implementations"
+#endif
+
 #ifdef ONE_BYTE
 #undef ONE_BYTE
 #define FNAME(name) quic_one_##name
diff --git a/common/templates/sw_canvas.c b/common/templates/sw_canvas.c
index 3778d89..1a9005e 100644
--- a/common/templates/sw_canvas.c
+++ b/common/templates/sw_canvas.c
@@ -22,10 +22,15 @@
 #include <config.h>
 #endif
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "sw_canvas.c should only be used by canvas implementations"
+#endif
+
 #include <math.h>
 #include "common/templates/sw_canvas.h"
 #define CANVAS_USE_PIXMAN
 #define CANVAS_SINGLE_INSTANCE
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "common/templates/canvas_base.c"
 #include "common/rect.h"
 #include "common/region.h"
diff --git a/common/templates/sw_canvas.h b/common/templates/sw_canvas.h
index 1b15778..31aa383 100644
--- a/common/templates/sw_canvas.h
+++ b/common/templates/sw_canvas.h
@@ -22,9 +22,15 @@
 #include <stdint.h>
 #include <spice/macros.h>
 
+#if !defined(SW_CANVAS_IMAGE_CACHE) && !defined(SW_CANVAS_CACHE)
+#error "sw_canvas.h should only be used by canvas implementations"
+#endif
+
 #include "common/draw.h"
 #include "common/pixman_utils.h"
+#define SPICE_COMMON_CANVAS_INTERNAL
 #include "common/templates/canvas_base.h"
+#undef SPICE_COMMON_CANVAS_INTERNAL
 #include "common/region.h"
 
 SPICE_BEGIN_DECLS
-- 
1.9.0



More information about the Spice-devel mailing list