Hi

Attached is the patch, which removes use of undefined grub_assert_fail
function for catching bad-type-cast errors, with a better version
__attribute__((error("msg"))) gcc extension.  With this extension, gcc
can give the exact location of the bad type cast at compile time.

Unfortunately, I couldn't test this on NetBSD, where old technique was
reported failing as GCC optimizations didn't happen.  Can Grégoire
Sutr or anybody else try this out on NetBSD and confirm?


thanks,
-- 
bvk.chaitanya
=== modified file 'include/grub/handler.h'
--- include/grub/handler.h      2009-03-01 17:51:44 +0000
+++ include/grub/handler.h      2009-12-22 13:34:38 +0000
@@ -55,6 +55,6 @@
     GRUB_FIELD_MATCH (ptr, grub_handler_t, name) && \
     GRUB_FIELD_MATCH (ptr, grub_handler_t, init) && \
     GRUB_FIELD_MATCH (ptr, grub_handler_t, fini)) ? \
-   (grub_handler_t) ptr : grub_assert_fail ())
+   (grub_handler_t) ptr : grub_bad_type_cast ())
 
 #endif /* ! GRUB_HANDLER_HEADER */

=== modified file 'include/grub/list.h'
--- include/grub/list.h 2009-11-09 14:25:03 +0000
+++ include/grub/list.h 2009-12-22 13:33:08 +0000
@@ -22,6 +22,7 @@
 
 #include <grub/symbol.h>
 #include <grub/types.h>
+#include <grub/misc.h>
 
 struct grub_list
 {
@@ -39,31 +40,27 @@
 void EXPORT_FUNC(grub_list_insert) (grub_list_t *head, grub_list_t item,
                                    grub_list_test_t test);
 
-/* This function doesn't exist, so if assertion is false for some reason, the
-   linker would fail.  */
-#ifdef APPLE_CC
-/* This approach fails with Apple's gcc. Use grub_abort.  */
-#include <grub/misc.h>
-static inline void *
-grub_assert_fail (void)
+static inline void *
+grub_bad_type_cast (void) __attribute__ ((error("bad type cast between 
incompatible grub types")));
+
+static inline void *
+grub_bad_type_cast (void)
 {
-       grub_abort ();
-       return 0;
+  grub_fatal ("bad type case between incompatible grub types detected");
+  grub_abort ();
+  return 0;
 }
-#else
-extern void* grub_assert_fail (void);
-#endif
 
 #define GRUB_FIELD_MATCH(ptr, type, field) \
   ((char *) &(ptr)->field == (char *) &((type) (ptr))->field)
 
 #define GRUB_AS_LIST(ptr) \
   (GRUB_FIELD_MATCH (ptr, grub_list_t, next) ? \
-   (grub_list_t) ptr : grub_assert_fail ())
+   (grub_list_t) ptr : grub_bad_type_cast ())
 
 #define GRUB_AS_LIST_P(pptr) \
   (GRUB_FIELD_MATCH (*pptr, grub_list_t, next) ? \
-   (grub_list_t *) (void *) pptr : grub_assert_fail ())
+   (grub_list_t *) (void *) pptr : grub_bad_type_cast ())
 
 struct grub_named_list
 {
@@ -78,12 +75,12 @@
 #define GRUB_AS_NAMED_LIST(ptr) \
   ((GRUB_FIELD_MATCH (ptr, grub_named_list_t, next) && \
     GRUB_FIELD_MATCH (ptr, grub_named_list_t, name))? \
-   (grub_named_list_t) ptr : grub_assert_fail ())
+   (grub_named_list_t) ptr : grub_bad_type_cast ())
 
 #define GRUB_AS_NAMED_LIST_P(pptr) \
   ((GRUB_FIELD_MATCH (*pptr, grub_named_list_t, next) && \
     GRUB_FIELD_MATCH (*pptr, grub_named_list_t, name))? \
-   (grub_named_list_t *) (void *) pptr : grub_assert_fail ())
+   (grub_named_list_t *) (void *) pptr : grub_bad_type_cast ())
 
 #define GRUB_PRIO_LIST_PRIO_MASK       0xff
 #define GRUB_PRIO_LIST_FLAG_ACTIVE     0x100
@@ -111,12 +108,12 @@
   ((GRUB_FIELD_MATCH (ptr, grub_prio_list_t, next) && \
     GRUB_FIELD_MATCH (ptr, grub_prio_list_t, name) && \
     GRUB_FIELD_MATCH (ptr, grub_prio_list_t, prio))? \
-   (grub_prio_list_t) ptr : grub_assert_fail ())
+   (grub_prio_list_t) ptr : grub_bad_type_cast ())
 
 #define GRUB_AS_PRIO_LIST_P(pptr) \
   ((GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, next) && \
     GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, name) && \
     GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, prio))? \
-   (grub_prio_list_t *) (void *) pptr : grub_assert_fail ())
+   (grub_prio_list_t *) (void *) pptr : grub_bad_type_cast ())
 
 #endif /* ! GRUB_LIST_HEADER */

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to