[Mesa-dev] [PATCH 3/5] compiler/list: add and use for_range_list macro

Nicolai Hähnle nhaehnle at gmail.com
Sat May 7 22:05:06 UTC 2016


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

This macro avoids undefined downcasting of list sentinels that crashes gcc's
ubsan.
---
 src/compiler/glsl/list.h                | 8 ++++++++
 src/compiler/glsl/opt_tree_grafting.cpp | 5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/compiler/glsl/list.h b/src/compiler/glsl/list.h
index 12389aa..f05d437 100644
--- a/src/compiler/glsl/list.h
+++ b/src/compiler/glsl/list.h
@@ -719,6 +719,14 @@ inline void exec_node::insert_before(exec_list *before)
 /**
  * Iterate over a range [begin, end) of nodes.
  */
+#define for_range_list(__type, __node, __begin, __end) \
+   for (__type *(__node), **__flag = &(__node); __flag; __flag = NULL) \
+      for (exec_node *__cur = (__begin),                               \
+                     *__end_stored = (__end);                          \
+           __cur != __end_stored &&                                    \
+           (((__node) = (__type *) __cur) || true);                    \
+           __cur = __cur->next)
+
 #define for_range_list_safe(__type, __node, __begin, __end) \
    for (__type *(__node), **__flag = &(__node); __flag; __flag = NULL) \
       for (struct exec_node *__cur = (__begin),                        \
diff --git a/src/compiler/glsl/opt_tree_grafting.cpp b/src/compiler/glsl/opt_tree_grafting.cpp
index 47fca7d..539ed57 100644
--- a/src/compiler/glsl/opt_tree_grafting.cpp
+++ b/src/compiler/glsl/opt_tree_grafting.cpp
@@ -323,10 +323,7 @@ try_tree_grafting(ir_assignment *start,
       fprintf(stderr, "\n");
    }
 
-   for (ir_instruction *ir = (ir_instruction *)start->next;
-	ir != bb_last->next;
-	ir = (ir_instruction *)ir->next) {
-
+   for_range_list(ir_instruction, ir, start->next, bb_last->next) {
       if (debug) {
 	 fprintf(stderr, "- ");
 	 ir->fprint(stderr);
-- 
2.7.4



More information about the mesa-dev mailing list