Add `%null-pointer' to `(system foreign)'.
authorLudovic Courtès <ludo@gnu.org>
Wed, 17 Mar 2010 23:27:38 +0000 (00:27 +0100)
committerLudovic Courtès <ludo@gnu.org>
Wed, 17 Mar 2010 23:28:14 +0000 (00:28 +0100)
* libguile/foreign.c (sym_null, null_pointer): New variables.
  (scm_foreign_to_bytevector): Raise an error when PTR is NULL.
  (scm_init_foreign): Define SYM_NULL.

* module/system/foreign.scm (%null-pointer): New exported binding.

libguile/foreign.c
module/system/foreign.scm

index b462ee9..52ed10c 100644 (file)
@@ -51,6 +51,14 @@ SCM_SYMBOL (sym_size_t, "size_t");
 /* that's for pointers, you know. */
 SCM_SYMBOL (sym_asterisk, "*");
 
+SCM_SYMBOL (sym_null, "%null-pointer");
+
+/* The cell representing the null pointer.  */
+static const scm_t_bits null_pointer[2] =
+  {
+    scm_tc7_foreign | (SCM_FOREIGN_TYPE_VOID << 8UL),
+    0
+  };
 
 static SCM cif_to_procedure (SCM cif, SCM func_ptr);
 
@@ -231,7 +239,10 @@ SCM_DEFINE (scm_foreign_to_bytevector, "foreign->bytevector", 1, 3, 0,
 
   SCM_VALIDATE_FOREIGN_TYPED (1, foreign, VOID);
   ptr = SCM_FOREIGN_POINTER (foreign, scm_t_int8);
-  
+
+  if (SCM_UNLIKELY (ptr == NULL))
+    scm_misc_error (FUNC_NAME, "null pointer dereference", SCM_EOL);
+
   if (SCM_UNBNDP (uvec_type))
     btype = SCM_ARRAY_ELEMENT_TYPE_VU8;
   else
@@ -1039,6 +1050,8 @@ scm_init_foreign (void)
 # error unsupported sizeof (size_t)
 #endif
              );
+
+  scm_define (sym_null, PTR2SCM (&null_pointer));
 }
 
 void
index 7966dd9..9f389f2 100644 (file)
@@ -25,6 +25,7 @@
             uint16 int16
             uint32 int32
             uint64 int64
+            %null-pointer
 
             sizeof alignof